Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 92d6767

Browse files
committed
Change Program to generate dynamic .dll to inspect generated IL
1 parent 8c1ad40 commit 92d6767

File tree

1 file changed

+52
-16
lines changed

1 file changed

+52
-16
lines changed
Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Linq.Expressions;
4+
using System.Reflection;
5+
using System.Reflection.Emit;
6+
using ServiceStack.Reflection;
27

38
namespace ServiceStack.Text.TestsConsole
49
{
5-
public struct T
10+
public struct TValue
11+
{
12+
public int PropValue { get; set; }
13+
public string PropRef { get; set; }
14+
15+
public int FieldValue;
16+
public string FieldRef;
17+
}
18+
19+
public class TRef
620
{
721
public int PropValue { get; set; }
822
public string PropRef { get; set; }
@@ -13,48 +27,60 @@ public struct T
1327

1428
class Program
1529
{
30+
public class IncludeExclude
31+
{
32+
public int Id { get; set; }
33+
public string Name { get; set; }
34+
}
35+
1636
static void Main(string[] args)
1737
{
18-
var t = new T { PropValue = 1, PropRef = "foo", FieldValue = 2, FieldRef = "bar" };
38+
var da = AppDomain.CurrentDomain.DefineDynamicAssembly(
39+
new AssemblyName("dyn"), // call it whatever you want
40+
AssemblyBuilderAccess.Save);
41+
42+
var dm = da.DefineDynamicModule("dyn_mod", "dyn.dll");
43+
var dt = dm.DefineType("dyn_type");
44+
1945

20-
//var i1 = GetPropInt(t);
21-
//var s1 = GetPropString(t);
46+
var type = typeof(KeyValuePair<string,string>);
47+
var pi = type.GetProperty("Key");
2248

23-
//var i2 = GetFieldInt(t);
24-
//var s2 = GetFieldString(t);
49+
var lambdaValueType = PropertyInvoker.GetExpressionLambda<KeyValuePair<string,string>>(pi);
50+
lambdaValueType.CompileToMethod(dt.DefineMethod("KVP", MethodAttributes.Public | MethodAttributes.Static));
2551

26-
//$"PropValue: ${i1}, PropRef: ${s1}".Print();
27-
//$"FieldValue: ${i2}, FieldRef: ${s2}".Print();
52+
var lambdaRefType = PropertyInvoker.GetExpressionLambda<TRef>(typeof(TRef).GetProperty("PropRef"));
53+
lambdaRefType.CompileToMethod(dt.DefineMethod("TRef_PropRef", MethodAttributes.Public | MethodAttributes.Static));
2854

29-
var tuple = ((int i, string s))new ValueTuple<int,string>(1,"foo");
55+
var lambdaRefType2 = PropertyInvoker.GetExpressionLambda<IncludeExclude>(typeof(IncludeExclude).GetProperty("Id"));
56+
lambdaRefType2.CompileToMethod(dt.DefineMethod("IncludeExclude_Id", MethodAttributes.Public | MethodAttributes.Static));
3057

31-
var oTuple = (object) tuple;
32-
var value = GetValueTupleItem2(oTuple);
3358

34-
value.PrintDump();
59+
dt.CreateType();
60+
da.Save("dyn.dll");
3561
}
3662

3763
static object GetPropInt(object instance)
3864
{
39-
var t = (T)instance;
65+
var t = (TValue)instance;
4066
return t.PropValue;
4167
}
4268

4369
static object GetPropString(object instance)
4470
{
45-
var t = (T)instance;
71+
var t = (TValue)instance;
4672
return t.PropRef;
4773
}
4874

4975
static object GetFieldInt(object instance)
5076
{
51-
var t = (T)instance;
77+
var t = (TValue)instance;
5278
return t.FieldValue;
5379
}
5480

5581
static object GetFieldString(object instance)
5682
{
57-
return ((T)instance).FieldRef;
83+
return ((TValue)instance).FieldRef;
5884
}
5985

6086
static object GetValueTupleItem1(object instance)
@@ -66,5 +92,15 @@ static object GetValueTupleItem2(object instance)
6692
{
6793
return ((ValueTuple<int, string>)instance).Item2;
6894
}
95+
96+
static object GetKVP(KeyValuePair<string, string> entry)
97+
{
98+
return entry.Key;
99+
}
100+
101+
static object GetKVPT<K,V>(KeyValuePair<K,V> entry)
102+
{
103+
return entry.Key;
104+
}
69105
}
70106
}

0 commit comments

Comments
 (0)