/// <summary> /// 创建子类代理 /// </summary> /// <param name="targetType">target Type</param> /// <param name="args">构造函数参数</param> /// <param name="enableAop">enableAop</param> /// <param name="aopType">aopType</param> /// <returns>代理实例</returns> public object GetProxy(Type targetType, object[] args, bool enableAop, Type[] aopType) { object result = null; if (targetType == null) { throw new ArgumentNullException("targetType"); } Type proxyType = targetType; if (!ProxyUtil.IsProxyType(targetType)) { proxyType = this.GetClassProxyType(targetType); } else { targetType = proxyType.BaseType; } if (args != null && args.Length > 0) { result = Activator.CreateInstance(proxyType, args); } else { result = Activator.CreateInstance(proxyType); } var proxy = result as IProxy; proxy.SetTargetType(targetType); List <Func <IAop> > funcList = new List <Func <IAop> >(); if (enableAop && this.AopFunc != null) { var attrs = targetType.GetCustomAttributes(typeof(AopAttribute), false); foreach (var o in attrs) { var attr = o as AopAttribute; if (attr.AopType != null && typeof(IAop).IsAssignableFrom(attr.AopType)) { funcList.Add(() => this.AopFunc(attr.AopType)); } } if (aopType != null) { foreach (var t in aopType) { if (t != null && typeof(IAop).IsAssignableFrom(t)) { funcList.Add(() => this.AopFunc(t)); } } } } proxy.SetAopFunc(funcList.ToArray()); return(result); }
/// <summary> /// 创建接口代理 /// </summary> /// <param name="interfaceType">接口 Type</param> /// <param name="target">target</param> /// <param name="enableAop">enableAop</param> /// <param name="aopType">aopType</param> /// <returns></returns> public object GetProxy(Type interfaceType, object target, bool enableAop, Type[] aopType) { object result = target; if (interfaceType == null) { throw new ArgumentNullException("targetType"); } if (target == null) { throw new ArgumentNullException("target"); } if (!interfaceType.IsInterface) { throw new ArgumentException(interfaceType.FullName + "不是interface!", "interfaceType"); } Type targetType = target.GetType(); if (!interfaceType.IsAssignableFrom(targetType)) { throw new ArgumentException("target 不是" + interfaceType.FullName + "类型", "target"); } if (!ProxyUtil.IsProxyType(targetType)) { var proxyType = this.GetProxyType(interfaceType); result = Activator.CreateInstance(proxyType, new object[] { target }); var proxy = result as IProxy; proxy.SetTargetType(targetType); List <Func <IAop> > funcList = new List <Func <IAop> >(); if (enableAop && this.AopFunc != null) { var attrs = targetType.GetCustomAttributes(typeof(AopAttribute), false); foreach (var o in attrs) { var attr = o as AopAttribute; if (attr.AopType != null && typeof(IAop).IsAssignableFrom(attr.AopType)) { funcList.Add(() => this.AopFunc(attr.AopType)); } } if (aopType != null) { foreach (var t in aopType) { if (t != null && typeof(IAop).IsAssignableFrom(t)) { funcList.Add(() => this.AopFunc(t)); } } } } proxy.SetAopFunc(funcList.ToArray()); } return(result); }
public bool Register(Type targetType) { if (targetType == null) { throw new ArgumentNullException("targetType"); } if (!ProxyUtil.IsProxyType(targetType)) { var proxyType = this.GetClassProxyType(targetType); return(proxyType != null); } return(true); }