@@ -2416,6 +2416,66 @@ internal void ILGenForCreateInstance(CodeGenerator ilg, Type type, Type cast, bo
2416
2416
}
2417
2417
}
2418
2418
2419
+ Label labelReturn = ilg . DefineLabel ( ) ;
2420
+ Label labelEndIf = ilg . DefineLabel ( ) ;
2421
+
2422
+ // TypeInfo typeInfo = type.GetTypeInfo();
2423
+ // typeInfo not declared explicitly
2424
+ ilg . Ldc ( type ) ;
2425
+ MethodInfo getTypeInfoMehod = typeof ( IntrospectionExtensions ) . GetMethod (
2426
+ "GetTypeInfo" ,
2427
+ CodeGenerator . StaticBindingFlags ,
2428
+ new [ ] { typeof ( Type ) }
2429
+ ) ;
2430
+ ilg . Call ( getTypeInfoMehod ) ;
2431
+
2432
+ // IEnumerator<ConstructorInfo> e = typeInfo.DeclaredConstructors.GetEnumerator();
2433
+ LocalBuilder enumerator = ilg . DeclareLocal ( typeof ( IEnumerator < > ) . MakeGenericType ( typeof ( ConstructorInfo ) ) , "e" ) ;
2434
+ MethodInfo getDeclaredConstructors = typeof ( TypeInfo ) . GetMethod ( "get_DeclaredConstructors" ) ;
2435
+ MethodInfo getEnumerator = typeof ( IEnumerable < > ) . MakeGenericType ( typeof ( ConstructorInfo ) ) . GetMethod ( "GetEnumerator" ) ;
2436
+ ilg . Call ( getDeclaredConstructors ) ;
2437
+ ilg . Call ( getEnumerator ) ;
2438
+ ilg . Stloc ( enumerator ) ;
2439
+
2440
+ ilg . WhileBegin ( ) ;
2441
+ // ConstructorInfo constructorInfo = e.Current();
2442
+ MethodInfo enumeratorCurrent = typeof ( IEnumerator ) . GetMethod ( "get_Current" ) ;
2443
+ ilg . Ldloc ( enumerator ) ;
2444
+ ilg . Call ( enumeratorCurrent ) ;
2445
+ LocalBuilder constructorInfo = ilg . DeclareLocal ( typeof ( ConstructorInfo ) , "constructorInfo" ) ;
2446
+ ilg . Stloc ( constructorInfo ) ;
2447
+
2448
+ // if (!constructorInfo.IsStatic && constructorInfo.GetParameters.Length() == 0)
2449
+ ilg . Ldloc ( constructorInfo ) ;
2450
+ MethodInfo constructorIsStatic = typeof ( ConstructorInfo ) . GetMethod ( "get_IsStatic" ) ;
2451
+ ilg . Call ( constructorIsStatic ) ;
2452
+ ilg . Brtrue ( labelEndIf ) ;
2453
+ ilg . Ldloc ( constructorInfo ) ;
2454
+ MethodInfo constructorGetParameters = typeof ( ConstructorInfo ) . GetMethod ( "GetParameters" ) ;
2455
+ ilg . Call ( constructorGetParameters ) ;
2456
+ ilg . Ldlen ( ) ;
2457
+ ilg . Ldc ( 0 ) ;
2458
+ ilg . Cne ( ) ;
2459
+ ilg . Brtrue ( labelEndIf ) ;
2460
+
2461
+ // constructorInfo.Invoke(null);
2462
+ MethodInfo constructorInvoke = typeof ( ConstructorInfo ) . GetMethod ( "Invoke" , new Type [ ] { typeof ( object [ ] ) } ) ;
2463
+ ilg . Ldloc ( constructorInfo ) ;
2464
+ ilg . Load ( null ) ;
2465
+ ilg . Call ( constructorInvoke ) ;
2466
+ ilg . Br ( labelReturn ) ;
2467
+
2468
+ ilg . MarkLabel ( labelEndIf ) ;
2469
+ ilg . WhileBeginCondition ( ) ; // while (e.MoveNext())
2470
+ MethodInfo IEnumeratorMoveNext = typeof ( IEnumerator ) . GetMethod (
2471
+ "MoveNext" ,
2472
+ CodeGenerator . InstanceBindingFlags ,
2473
+ Array . Empty < Type > ( ) ) ;
2474
+ ilg . Ldloc ( enumerator ) ;
2475
+ ilg . Call ( IEnumeratorMoveNext ) ;
2476
+ ilg . WhileEndCondition ( ) ;
2477
+ ilg . WhileEnd ( ) ;
2478
+
2419
2479
MethodInfo Activator_CreateInstance = typeof ( Activator ) . GetMethod (
2420
2480
"CreateInstance" ,
2421
2481
CodeGenerator . StaticBindingFlags ,
@@ -2425,6 +2485,7 @@ internal void ILGenForCreateInstance(CodeGenerator ilg, Type type, Type cast, bo
2425
2485
ilg . Call ( Activator_CreateInstance ) ;
2426
2486
if ( cast != null )
2427
2487
ilg . ConvertValue ( Activator_CreateInstance . ReturnType , cast ) ;
2488
+ ilg . MarkLabel ( labelReturn ) ;
2428
2489
}
2429
2490
2430
2491
internal void WriteLocalDecl ( string variableName , SourceInfo initValue )
0 commit comments