1
1
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 ;
2
7
3
8
namespace ServiceStack . Text . TestsConsole
4
9
{
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
6
20
{
7
21
public int PropValue { get ; set ; }
8
22
public string PropRef { get ; set ; }
@@ -13,48 +27,60 @@ public struct T
13
27
14
28
class Program
15
29
{
30
+ public class IncludeExclude
31
+ {
32
+ public int Id { get ; set ; }
33
+ public string Name { get ; set ; }
34
+ }
35
+
16
36
static void Main ( string [ ] args )
17
37
{
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
+
19
45
20
- // var i1 = GetPropInt(t );
21
- // var s1 = GetPropString(t );
46
+ var type = typeof ( KeyValuePair < string , string > ) ;
47
+ var pi = type . GetProperty ( "Key" ) ;
22
48
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 ) ) ;
25
51
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 ) ) ;
28
54
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 ) ) ;
30
57
31
- var oTuple = ( object ) tuple ;
32
- var value = GetValueTupleItem2 ( oTuple ) ;
33
58
34
- value . PrintDump ( ) ;
59
+ dt . CreateType ( ) ;
60
+ da . Save ( "dyn.dll" ) ;
35
61
}
36
62
37
63
static object GetPropInt ( object instance )
38
64
{
39
- var t = ( T ) instance ;
65
+ var t = ( TValue ) instance ;
40
66
return t . PropValue ;
41
67
}
42
68
43
69
static object GetPropString ( object instance )
44
70
{
45
- var t = ( T ) instance ;
71
+ var t = ( TValue ) instance ;
46
72
return t . PropRef ;
47
73
}
48
74
49
75
static object GetFieldInt ( object instance )
50
76
{
51
- var t = ( T ) instance ;
77
+ var t = ( TValue ) instance ;
52
78
return t . FieldValue ;
53
79
}
54
80
55
81
static object GetFieldString ( object instance )
56
82
{
57
- return ( ( T ) instance ) . FieldRef ;
83
+ return ( ( TValue ) instance ) . FieldRef ;
58
84
}
59
85
60
86
static object GetValueTupleItem1 ( object instance )
@@ -66,5 +92,15 @@ static object GetValueTupleItem2(object instance)
66
92
{
67
93
return ( ( ValueTuple < int , string > ) instance ) . Item2 ;
68
94
}
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
+ }
69
105
}
70
106
}
0 commit comments