//void Unbind(Type type); private static StackObject *Unbind_Types(ILIntepreter intp, StackObject *esp, IList <object> mStack, CLRMethod method, bool isNewObj) { var genericArguments = method.GenericArguments; if (method.ParameterCount != 1) { throw new EntryPointNotFoundException(); } ILRuntime.Runtime.Enviorment.AppDomain domain = intp.AppDomain; StackObject *ptr_of_this_method; StackObject *__ret = ILIntepreter.Minus(esp, 3); ptr_of_this_method = ILIntepreter.Minus(esp, 1); Type type = (System.Type) typeof(System.Type).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, domain, mStack)); intp.Free(ptr_of_this_method); var tService = XILUtil.GetCatLibServiceName(type); XCore.MainInstance.Services.Unbind(tService); return(esp); }
//bool TryGetBuildInService(Type type, out object service); private static StackObject *TryGetBuildInService_Type(ILIntepreter intp, StackObject *esp, IList <object> mStack, CLRMethod method, bool isNewObj) { var genericArguments = method.GenericArguments; if (method.ParameterCount != 2) { throw new EntryPointNotFoundException(); } var ptrOfThisMethod = ILIntepreter.Minus(esp, 1); ptrOfThisMethod = ILIntepreter.GetObjectAndResolveReference(ptrOfThisMethod); var service = (object)typeof(object).CheckCLRTypes(StackObject.ToObject(ptrOfThisMethod, intp.AppDomain, mStack)); ptrOfThisMethod = ILIntepreter.Minus(esp, 2); ptrOfThisMethod = ILIntepreter.GetObjectAndResolveReference(ptrOfThisMethod); var _type = (Type)typeof(Type).CheckCLRTypes(StackObject.ToObject(ptrOfThisMethod, intp.AppDomain, mStack)); var tService = XILUtil.GetCatLibServiceName(_type); intp.Free(ptrOfThisMethod); var result = XCore.MainInstance.Services.TryGetBuildInService(tService, out service); ptrOfThisMethod = ILIntepreter.Minus(esp, 1); XILUtil.SetValue(ptrOfThisMethod, mStack, intp.AppDomain, service); return(ILIntepreter.PushObject(ILIntepreter.Minus(esp, 1), mStack, result)); }
//object Get(Type type, params object[] userParams); private static StackObject *Get_Type_ArrObject(ILIntepreter __intp, StackObject *__esp, IList <object> __mStack, CLRMethod __method, bool isNewObj) { ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain; StackObject *ptr_of_this_method; StackObject *__ret = ILIntepreter.Minus(__esp, 3); ptr_of_this_method = ILIntepreter.Minus(__esp, 1); System.Object[] @userParams = (System.Object[]) typeof(System.Object[]).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack)); __intp.Free(ptr_of_this_method); ptr_of_this_method = ILIntepreter.Minus(__esp, 2); System.Type @type = (System.Type) typeof(System.Type).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack)); __intp.Free(ptr_of_this_method); ptr_of_this_method = ILIntepreter.Minus(__esp, 3); TinaX.Container.IServiceContainer instance_of_this_method = (TinaX.Container.IServiceContainer) typeof(TinaX.Container.IServiceContainer).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack)); __intp.Free(ptr_of_this_method); string serviceName = XILUtil.GetCatLibServiceName(@type); var result_of_this_method = instance_of_this_method.Get(serviceName, @userParams); object obj_result_of_this_method = result_of_this_method; if (obj_result_of_this_method is CrossBindingAdaptorType) { return(ILIntepreter.PushObject(__ret, __mStack, ((CrossBindingAdaptorType)obj_result_of_this_method).ILInstance, true)); } return(ILIntepreter.PushObject(__ret, __mStack, result_of_this_method, true)); }
public object CreateInstanceAndInject(Type type) { var ctors = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance); //找到可注入的ctor foreach (var ctor in ctors) { var ctor_params = ctor.GetParameters(); List <object> param_objs = new List <object>(); bool flag = true; foreach (var param in ctor_params) { if (m_XServices.TryGet(XILUtil.GetCatLibServiceName(param.ParameterType), out var _service)) { param_objs.Add(_service); } else { flag = false; break; } } if (flag) //有可用的构造函数 { //this.CreateInstance(type, param_objs); var result = ctor.Invoke(param_objs.ToArray()); this.InjectObject(result); return(result); } } throw new XException("[TinaX.Core] Create instance failed: No valid constructors, Type:" + type.FullName); }
public void InjectObject(object target) { BindingFlags _bindFlag = BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly; Type actualType = target.GetActualType(); foreach (var field in actualType.GetFields(_bindFlag)) { string service_name = XILUtil.GetCatLibServiceName(field); var attr = field.GetCustomAttribute <InjectAttribute>(true); if (attr == null) { continue; } if (m_XServices.TryGet(service_name, out var _service)) { field.SetValue(target, _service); continue; } if (attr.Nullable) { continue; } else { throw new ServiceNotFoundException(field.FieldType); //抛异常 } } foreach (var property in actualType.GetProperties(_bindFlag)) { string service_name = XILUtil.GetCatLibServiceName(property); var attr = property.GetCustomAttribute <InjectAttribute>(true); if (attr == null) { continue; } if (m_XServices.TryGet(service_name, out var _service)) { property.SetValue(target, _service); continue; } if (attr.Nullable) { continue; } else { throw new ServiceNotFoundException(property.PropertyType); //抛异常 } } }
internal static StackObject *Type2ServiceName_Type(ILIntepreter __intp, StackObject *__esp, IList <object> __mStack, CLRMethod __method, bool isNewObj) { ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain; StackObject *ptr_of_this_method; StackObject *__ret = ILIntepreter.Minus(__esp, 1); ptr_of_this_method = ILIntepreter.Minus(__esp, 1); System.Type type = (System.Type) typeof(System.Type).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack)); __intp.Free(ptr_of_this_method); var result_of_this_method = XILUtil.GetCatLibServiceName(type); return(ILIntepreter.PushObject(__ret, __mStack, result_of_this_method)); }