.netcore 下aspectcore 的使用
动态代理:
static void Main(string[] args) { Console.WriteLine("Hello World!"); using (IProxyGenerator proxyGenerator = new ProxyGeneratorBuilder().Build()) { var p = proxyGenerator.CreateClassProxy<Test>(); p.myPrint(); var p3 = proxyGenerator.CreateClassProxy<Test2>(); p3.myPrint(); } Console.Read(); Console.WriteLine("Hello World!********************"); }
红色的p,类型是 AspectCore.DynamicGeneratedqgb.Test
这个类型是动态产生的。
ProxyGeneratorUtils类下的方法 private Type CreateClassProxyInternal(string name, Type serviceType, Type implType, Type[] additionalInterfaces, IAspectValidator aspectValidator)
private Type CreateClassProxyInternal(string name, Type serviceType, Type implType, Type[] additionalInterfaces, IAspectValidator aspectValidator) { var interfaces = additionalInterfaces.Distinct().ToArray();
//这个类可以被编译成一个 Type. 下面能动态生成构造函数,和方法。 var typeDesc = TypeBuilderUtils.DefineType(_moduleBuilder, name, serviceType, implType, interfaces); typeDesc.Properties[typeof(IAspectValidator).Name] = aspectValidator; //define constructor ConstructorBuilderUtils.DefineClassProxyConstructors(serviceType, implType, typeDesc); //define methods MethodBuilderUtils.DefineClassProxyMethods(serviceType, implType, additionalInterfaces, typeDesc); PropertyBuilderUtils.DefineClassProxyProperties(serviceType, implType, additionalInterfaces, typeDesc); return typeDesc.Compile(); }