/// <summary> /// 新增容器中的外派方法,外派方法可被容器外部通过 Delive 方法调用 /// </summary> public void Add <InterfaceT>(ResolveDelegate <InterfaceT> target, bool onlyOnce = false) { int statu = 0; MethodInfo method = target.Method; object[] attributes = method.GetCustomAttributes(callableAttrType, false); int max = attributes.Length; string resolverName, alias; CallableAttribute attribute; IResolvable resolvable; for (int i = 0; i < max; i++) { attribute = attributes[i] as CallableAttribute; resolverName = attribute.ResolverName; resolverName = string.IsNullOrEmpty(resolverName) ? method.Name : resolverName; alias = attribute.Alias; resolvable = ServersHolder.GetResolvable(ref alias, out statu); if (statu == 0) { "error".Log(resolvable == default, "Resolvable is null, alias is " + alias); resolvable.SetResolver(resolverName, target, out statu, onlyOnce); } } }
public InterfaceT Resolve <InterfaceT>(string alias, string resolverName = "") { int resultError; InterfaceT result = default; IResolvable resolvable = ServersHolder.GetResolvable(ref alias, out resultError); if (resultError == 0) { IResolverHandler resolverHandler = resolvable.GetResolver <InterfaceT>(Resolvable.RESOLVER_DEF, out _); resolverHandler.InvokeResolver(); result = (InterfaceT)resolverHandler.ResolverParam; resolverHandler = resolvable.GetResolver <InterfaceT>(Resolvable.RESOLVER_CRT, out _); resolverHandler.SetParam(ref result); resolverHandler.InvokeResolver(); result = (InterfaceT)resolverHandler.ResolverParam; if ((result != default) && !string.IsNullOrEmpty(resolverName)) { resolverHandler = resolvable.GetResolver <InterfaceT>(resolverName, out _); resolverHandler.SetParam(ref result); resolverHandler.InvokeResolver(); if (resolverHandler.OnlyOnce) { resolvable.RemoveResolver <InterfaceT>(resolverName, out _); } } } return(result); }
/// <summary> /// 撤销已定义在解析器中的一个解析器函数 /// </summary> public int RevokeResolver <InterfaceT>(string alias, string resolverName, ResolveDelegate <InterfaceT> target) { int statu = 0; IResolvable resolvable = ServersHolder.GetResolvable(ref alias, out statu); if (statu == 0) { "error".Log(resolvable == default, "Resolvable is null when MakeResolver, alias is " + alias); resolvable.RevokeResolver(resolverName, target); } return(statu); }
public int MakeResolver <InterfaceT>(string alias, string resolverName, ResolveDelegate <InterfaceT> target) { int statu = 0; IResolvable resolvable = ServersHolder.GetResolvable(ref alias, out statu); if (statu == 0) { Tester.Instance.Log(TesterBaseApp.Instance, TesterBaseApp.LOG, resolvable == default, "error: Resolvable is null when MakeResolver, alias is " + alias); resolvable.SetResolver(resolverName, target, out statu, false, true); } return(statu); }
/// <summary> /// 通过接口及别名解析出一个对象的实例并根据解析器名调用解析器函数 /// </summary> /// <typeparam name="InterfaceT">接口</typeparam> /// <param name="alias">别名</param> /// <param name="resolverName">解析器名,用于将解析得到的对象传入对应的加工函数,不传此参表示仅获取实例</param> /// <param name="customResolver">定制的解析器函数,在传入加工函数前预处理解析得到的对象</param> /// <returns>需要获得的实例对象</returns> public InterfaceT Resolve <InterfaceT>(string alias, string resolverName = "", ResolveDelegate <InterfaceT> customResolver = default) { int resultError; InterfaceT result = default; IResolvable resolvable = ServersHolder.GetResolvable(ref alias, out resultError); "error:Resolvable is null, alias is {0}".Log(resolvable == default, alias); if (resultError == 0) { IResolverHandler resolverHandler; resolverHandler = resolvable.GetResolver <InterfaceT>(Resolvable.RESOLVER_CRT, out _); resolverHandler.InvokeResolver(); result = (InterfaceT)resolverHandler.ResolverParam; resolverHandler = resolvable.GetResolver <InterfaceT>(Resolvable.RESOLVER_INIT, out _); resolverHandler.SetParam(ref result); resolverHandler.InvokeResolver(); result = (InterfaceT)resolverHandler.ResolverParam; if (!result.Equals(default))