10
10
using JetBrains . Annotations ;
11
11
using System . Linq . Dynamic . Core . Extensions ;
12
12
13
- #if NETSTANDARD1_0
14
- using BindingFlags = System . Reflection . BindingFlags ;
15
- #endif
16
-
17
13
namespace System . Linq . Dynamic . Core
18
14
{
19
15
/// <summary>
20
16
/// http://stackoverflow.com/questions/29413942/c-sharp-anonymous-object-with-properties-from-dictionary
21
17
/// </summary>
22
18
internal static class DynamicClassFactory
23
19
{
20
+ // EmptyTypes is used to indicate that we are looking for someting without any parameters.
21
+ private readonly static Type [ ] EmptyTypes = new Type [ 0 ] ;
22
+
24
23
private static readonly ConcurrentDictionary < string , Type > GeneratedTypes = new ConcurrentDictionary < string , Type > ( ) ;
25
24
26
25
private static readonly ModuleBuilder ModuleBuilder ;
27
26
28
27
// Some objects we cache
29
- private static readonly CustomAttributeBuilder CompilerGeneratedAttributeBuilder = new CustomAttributeBuilder ( typeof ( CompilerGeneratedAttribute ) . GetConstructor ( Type . EmptyTypes ) , new object [ 0 ] ) ;
28
+ private static readonly CustomAttributeBuilder CompilerGeneratedAttributeBuilder = new CustomAttributeBuilder ( typeof ( CompilerGeneratedAttribute ) . GetConstructor ( EmptyTypes ) , new object [ 0 ] ) ;
30
29
private static readonly CustomAttributeBuilder DebuggerBrowsableAttributeBuilder = new CustomAttributeBuilder ( typeof ( DebuggerBrowsableAttribute ) . GetConstructor ( new [ ] { typeof ( DebuggerBrowsableState ) } ) , new object [ ] { DebuggerBrowsableState . Never } ) ;
31
- private static readonly CustomAttributeBuilder DebuggerHiddenAttributeBuilder = new CustomAttributeBuilder ( typeof ( DebuggerHiddenAttribute ) . GetConstructor ( Type . EmptyTypes ) , new object [ 0 ] ) ;
30
+ private static readonly CustomAttributeBuilder DebuggerHiddenAttributeBuilder = new CustomAttributeBuilder ( typeof ( DebuggerHiddenAttribute ) . GetConstructor ( EmptyTypes ) , new object [ 0 ] ) ;
32
31
33
- private static readonly ConstructorInfo ObjectCtor = typeof ( object ) . GetConstructor ( Type . EmptyTypes ) ;
32
+ private static readonly ConstructorInfo ObjectCtor = typeof ( object ) . GetConstructor ( EmptyTypes ) ;
34
33
#if DNXCORE50 || DOTNET5_4 || NETSTANDARD1_0
35
34
private static readonly MethodInfo ObjectToString = typeof ( object ) . GetMethod ( "ToString" , BindingFlags . Instance | BindingFlags . Public ) ;
36
35
#else
37
- private static readonly MethodInfo ObjectToString = typeof ( object ) . GetMethod ( "ToString" , BindingFlags . Instance | BindingFlags . Public , null , Type . EmptyTypes , null ) ;
36
+ private static readonly MethodInfo ObjectToString = typeof ( object ) . GetMethod ( "ToString" , BindingFlags . Instance | BindingFlags . Public , null , EmptyTypes , null ) ;
38
37
#endif
39
38
40
- private static readonly ConstructorInfo StringBuilderCtor = typeof ( StringBuilder ) . GetConstructor ( Type . EmptyTypes ) ;
39
+ private static readonly ConstructorInfo StringBuilderCtor = typeof ( StringBuilder ) . GetConstructor ( EmptyTypes ) ;
41
40
#if DNXCORE50 || DOTNET5_4 || NETSTANDARD1_0
42
41
private static readonly MethodInfo StringBuilderAppendString = typeof ( StringBuilder ) . GetMethod ( "Append" , new [ ] { typeof ( string ) } ) ;
43
42
private static readonly MethodInfo StringBuilderAppendObject = typeof ( StringBuilder ) . GetMethod ( "Append" , new [ ] { typeof ( object ) } ) ;
@@ -48,14 +47,14 @@ internal static class DynamicClassFactory
48
47
49
48
private static readonly Type EqualityComparer = typeof ( EqualityComparer < > ) ;
50
49
51
- #if DNXCORE50 || DOTNET5_4 || NETSTANDARD1_0
50
+
52
51
private static readonly Type EqualityComparerGenericArgument = EqualityComparer . GetGenericArguments ( ) [ 0 ] ;
52
+ #if DNXCORE50 || DOTNET5_4 || NETSTANDARD1_0
53
53
private static readonly MethodInfo EqualityComparerDefault = EqualityComparer . GetMethod ( "get_Default" , BindingFlags . Static | BindingFlags . Public ) ;
54
54
private static readonly MethodInfo EqualityComparerEquals = EqualityComparer . GetMethod ( "Equals" , new [ ] { EqualityComparerGenericArgument , EqualityComparerGenericArgument } ) ;
55
55
private static readonly MethodInfo EqualityComparerGetHashCode = EqualityComparer . GetMethod ( "GetHashCode" , new [ ] { EqualityComparerGenericArgument } ) ;
56
56
#else
57
- private static readonly Type EqualityComparerGenericArgument = EqualityComparer . GetGenericArguments ( ) [ 0 ] ;
58
- private static readonly MethodInfo EqualityComparerDefault = EqualityComparer . GetMethod ( "get_Default" , BindingFlags . Static | BindingFlags . Public , null , Type . EmptyTypes , null ) ;
57
+ private static readonly MethodInfo EqualityComparerDefault = EqualityComparer . GetMethod ( "get_Default" , BindingFlags . Static | BindingFlags . Public , null , EmptyTypes , null ) ;
59
58
private static readonly MethodInfo EqualityComparerEquals = EqualityComparer . GetMethod ( "Equals" , BindingFlags . Instance | BindingFlags . Public , null , new [ ] { EqualityComparerGenericArgument , EqualityComparerGenericArgument } , null ) ;
60
59
private static readonly MethodInfo EqualityComparerGetHashCode = EqualityComparer . GetMethod ( "GetHashCode" , BindingFlags . Instance | BindingFlags . Public , null , new [ ] { EqualityComparerGenericArgument } , null ) ;
61
60
#endif
@@ -124,7 +123,7 @@ public static Type CreateType([NotNull] IList<DynamicProperty> properties)
124
123
}
125
124
126
125
// .ctor default
127
- ConstructorBuilder constructorDef = tb . DefineConstructor ( MethodAttributes . Public | MethodAttributes . HideBySig , CallingConventions . HasThis , Type . EmptyTypes ) ;
126
+ ConstructorBuilder constructorDef = tb . DefineConstructor ( MethodAttributes . Public | MethodAttributes . HideBySig , CallingConventions . HasThis , EmptyTypes ) ;
128
127
constructorDef . SetCustomAttribute ( DebuggerHiddenAttributeBuilder ) ;
129
128
130
129
ILGenerator ilgeneratorConstructorDef = constructorDef . GetILGenerator ( ) ;
@@ -177,7 +176,7 @@ public static Type CreateType([NotNull] IList<DynamicProperty> properties)
177
176
178
177
ilgeneratorConstructor . Emit ( OpCodes . Stfld , fields [ i ] ) ;
179
178
180
- PropertyBuilder property = tb . DefineProperty ( names [ i ] , PropertyAttributes . None , CallingConventions . HasThis , generics [ i ] , Type . EmptyTypes ) ;
179
+ PropertyBuilder property = tb . DefineProperty ( names [ i ] , PropertyAttributes . None , CallingConventions . HasThis , generics [ i ] , EmptyTypes ) ;
181
180
182
181
// getter
183
182
MethodBuilder getter = tb . DefineMethod ( $ "get_{ names [ i ] } ", MethodAttributes . Public | MethodAttributes . HideBySig | MethodAttributes . SpecialName , CallingConventions . HasThis , generics [ i ] , null ) ;
@@ -191,7 +190,7 @@ public static Type CreateType([NotNull] IList<DynamicProperty> properties)
191
190
// setter
192
191
MethodBuilder setter = tb . DefineMethod ( $ "set_{ names [ i ] } ", MethodAttributes . Public | MethodAttributes . HideBySig | MethodAttributes . SpecialName , CallingConventions . HasThis , null , new [ ] { generics [ i ] } ) ;
193
192
setter . SetCustomAttribute ( CompilerGeneratedAttributeBuilder ) ;
194
-
193
+
195
194
// workaround for https://github.com/dotnet/corefx/issues/7792
196
195
setter . DefineParameter ( 1 , ParameterAttributes . In , generics [ i ] . Name ) ;
197
196
@@ -204,7 +203,7 @@ public static Type CreateType([NotNull] IList<DynamicProperty> properties)
204
203
}
205
204
206
205
// ToString()
207
- MethodBuilder toString = tb . DefineMethod ( "ToString" , MethodAttributes . Public | MethodAttributes . Virtual | MethodAttributes . HideBySig , CallingConventions . HasThis , typeof ( string ) , Type . EmptyTypes ) ;
206
+ MethodBuilder toString = tb . DefineMethod ( "ToString" , MethodAttributes . Public | MethodAttributes . Virtual | MethodAttributes . HideBySig , CallingConventions . HasThis , typeof ( string ) , EmptyTypes ) ;
208
207
toString . SetCustomAttribute ( DebuggerHiddenAttributeBuilder ) ;
209
208
ILGenerator ilgeneratorToString = toString . GetILGenerator ( ) ;
210
209
ilgeneratorToString . DeclareLocal ( typeof ( StringBuilder ) ) ;
@@ -214,7 +213,8 @@ public static Type CreateType([NotNull] IList<DynamicProperty> properties)
214
213
// Equals
215
214
MethodBuilder equals = tb . DefineMethod ( "Equals" , MethodAttributes . Public | MethodAttributes . Virtual | MethodAttributes . HideBySig , CallingConventions . HasThis , typeof ( bool ) , new [ ] { typeof ( object ) } ) ;
216
215
equals . SetCustomAttribute ( DebuggerHiddenAttributeBuilder ) ;
217
- equals . DefineParameter ( 1 , ParameterAttributes . None , "value" ) ;
216
+ equals . DefineParameter ( 1 , ParameterAttributes . In , "value" ) ;
217
+
218
218
ILGenerator ilgeneratorEquals = equals . GetILGenerator ( ) ;
219
219
ilgeneratorEquals . DeclareLocal ( tb ) ;
220
220
ilgeneratorEquals . Emit ( OpCodes . Ldarg_1 ) ;
@@ -225,7 +225,7 @@ public static Type CreateType([NotNull] IList<DynamicProperty> properties)
225
225
Label equalsLabel = ilgeneratorEquals . DefineLabel ( ) ;
226
226
227
227
// GetHashCode()
228
- MethodBuilder getHashCode = tb . DefineMethod ( "GetHashCode" , MethodAttributes . Public | MethodAttributes . Virtual | MethodAttributes . HideBySig , CallingConventions . HasThis , typeof ( int ) , Type . EmptyTypes ) ;
228
+ MethodBuilder getHashCode = tb . DefineMethod ( "GetHashCode" , MethodAttributes . Public | MethodAttributes . Virtual | MethodAttributes . HideBySig , CallingConventions . HasThis , typeof ( int ) , EmptyTypes ) ;
229
229
getHashCode . SetCustomAttribute ( DebuggerHiddenAttributeBuilder ) ;
230
230
ILGenerator ilgeneratorGetHashCode = getHashCode . GetILGenerator ( ) ;
231
231
ilgeneratorGetHashCode . DeclareLocal ( typeof ( int ) ) ;
0 commit comments