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