@@ -29,6 +29,7 @@ public class DomainTemplateBuilder
2929 internal static readonly MethodInfo _GetLifetimeStrategy = typeof ( DomainTemplateAgent ) . GetMethod ( "GetLifetimeStrategy" , BindingFlags . NonPublic | BindingFlags . Instance ) ! ;
3030 internal static readonly ConstructorInfo _TransientDomainContextConstructor = typeof ( TransientDomainContext ) . GetConstructor ( new Type [ ] { typeof ( IDomainContext ) } ) ! ;
3131 internal static readonly MethodInfo _GetTypeFromHandleMethod = typeof ( Type ) . GetMethod ( "GetTypeFromHandle" , new Type [ 1 ] { typeof ( RuntimeTypeHandle ) } ) ! ;
32+ internal static readonly MethodInfo _SetContext = typeof ( IDomainContextAccessor ) . GetProperty ( "Context" ) ! . GetSetMethod ( ) ! ;
3233
3334 public static ModuleBuilder Module { get ; }
3435
@@ -77,11 +78,11 @@ public class DomainTemplateBuilder<TDomainService, T> : DomainTemplateBuilder, I
7778 //internal static readonly MethodInfo _GetDomainService = typeof(DomainTemplateAgent<TDomainService>).GetProperty(nameof(DomainTemplateAgent<TDomainService>.Service))!.GetGetMethod()!;
7879 internal static readonly ConstructorInfo _AgentConstructor = typeof ( DomainTemplateAgent ) . GetConstructor ( new Type [ ] { typeof ( IDomainContext ) } ) ! ;
7980 internal static readonly MethodInfo _GetValueProvider = typeof ( IDomainContext ) . GetProperty ( nameof ( IDomainContext . ValueProvider ) ) ! . GetGetMethod ( ) ! ;
80- private static Dictionary < string , FromAttribute [ ] > _FromAttributes ;
81+ private static Dictionary < string , FromAttribute ? [ ] > _FromAttributes ;
8182 private static Dictionary < string , ParameterInfo [ ] > _ParameterInfos ;
8283 private static TypeBuilder _AgentBuilder ;
8384
84- public static FromAttribute GetAttribute ( string method , int parameterIndex )
85+ public static FromAttribute ? GetAttribute ( string method , int parameterIndex )
8586 {
8687 var values = _FromAttributes [ method ] ;
8788 return values [ parameterIndex ] ;
@@ -98,7 +99,7 @@ static DomainTemplateBuilder()
9899 var serviceType = typeof ( TDomainService ) ;
99100 var type = typeof ( T ) ;
100101 _FromAttributes = serviceType . GetMethods ( ) . ToDictionary ( x => x . Name ,
101- x => x . GetParameters ( ) . Select ( y => ( FromAttribute ) y . GetCustomAttributes ( ) . FirstOrDefault ( z => z is FromAttribute ) ) . ToArray ( ) ) ;
102+ x => x . GetParameters ( ) . Select ( y => ( FromAttribute ? ) y . GetCustomAttributes ( ) . FirstOrDefault ( z => z is FromAttribute ) ) . ToArray ( ) ) ;
102103 _ParameterInfos = serviceType . GetMethods ( ) . ToDictionary ( x => x . Name , x => x . GetParameters ( ) ) ;
103104
104105 if ( ! type . IsInterface )
@@ -176,13 +177,15 @@ static DomainTemplateBuilder()
176177 //Set each parameter value into values. values.Add(parameter name, parameter value)
177178 for ( int i = 0 ; i < parameters . Length ; i ++ )
178179 {
180+ if ( parameters [ i ] . Name == null )
181+ continue ;
179182 ilGenerator . Emit ( OpCodes . Ldloc , values ) ;
180- ilGenerator . Emit ( OpCodes . Ldstr , parameters [ i ] . Name ) ;
183+ ilGenerator . Emit ( OpCodes . Ldstr , parameters [ i ] . Name ! ) ;
181184 ilGenerator . Emit ( OpCodes . Ldarg_S , ( byte ) ( i + 1 ) ) ;
182185 if ( parameters [ i ] . ParameterType . IsValueType )
183186 ilGenerator . Emit ( OpCodes . Box , parameters [ i ] . ParameterType ) ;
184187 ilGenerator . Emit ( OpCodes . Call , _DictionaryAdd ) ;
185- parameterNames . Add ( parameters [ i ] . Name ) ;
188+ parameterNames . Add ( parameters [ i ] . Name ! ) ;
186189 }
187190
188191 var valueAttributes = method . GetCustomAttributes < DomainValueAttribute > ( ) ;
@@ -262,11 +265,19 @@ static DomainTemplateBuilder()
262265 ilGenerator . Emit ( OpCodes . Brfalse_S , scopeLabel ) ;
263266 ilGenerator . Emit ( OpCodes . Newobj , _TransientDomainContextConstructor ) ;
264267 ilGenerator . MarkLabel ( scopeLabel ) ;
268+ ilGenerator . Emit ( OpCodes . Dup ) ;
265269 ilGenerator . Emit ( OpCodes . Stloc , domainContextLocal ) ;
266270
271+ //domainContext.GetService<IDomainContextAccessor>().Context = domainContext;
272+ ilGenerator . Emit ( OpCodes . Ldtoken , typeof ( IDomainContextAccessor ) ) ;
273+ ilGenerator . Emit ( OpCodes . Call , _GetTypeFromHandleMethod ) ;
274+ ilGenerator . Emit ( OpCodes . Callvirt , _GetService ) ;
275+ ilGenerator . Emit ( OpCodes . Ldloc , domainContextLocal ) ;
276+ ilGenerator . Emit ( OpCodes . Callvirt , _SetContext ) ;
277+
267278 var valueProviderLocal = ilGenerator . DeclareLocal ( typeof ( IValueProvider ) ) ;
268279
269- //var valueProvider = (IValueProvider) domainContext.GetService(typeof(IValueProvider)) ;
280+ //var valueProvider = domainContext.ValueProvider ;
270281 ilGenerator . Emit ( OpCodes . Ldloc , domainContextLocal ) ;
271282 ilGenerator . Emit ( OpCodes . Callvirt , _GetValueProvider ) ;
272283 ilGenerator . Emit ( OpCodes . Stloc , valueProviderLocal ) ;
0 commit comments