public List <MethodInfo> GetProxiableMethods(DynamicClassDescription clsDescription) { List <MethodInfo> proxiableMethods = clsDescription.MethodInfos ?? new List <MethodInfo>(); if (clsDescription.InterfaceTypes == null) { return(proxiableMethods); } foreach (Type parentType in clsDescription.InterfaceTypes) { GetProxiableMethods(ref proxiableMethods, parentType); } return(proxiableMethods); }
public List <PropertyInfo> GetProxiablePropertyMap(DynamicClassDescription clsDescription) { var list = clsDescription.PropertyInfos ?? new List <PropertyInfo>(); if (clsDescription.InterfaceTypes == null) { return(list); } foreach (var type in clsDescription.InterfaceTypes) { foreach (var propertyInfo in type.GetProperties()) { if (!list.Contains(propertyInfo)) { list.Add(propertyInfo); } } } return(list); }
// Uses Reflection Emit to create the proxy type. public Type CreateType(DynamicClassDescription clsDescription) { Type type; if (typeMap.TryGetValue(clsDescription.ClassKey, out type)) { return(type); } AssemblyName name = new AssemblyName(clsDescription.ClassKey); //AssemblyBuilder assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(name, AssemblyBuilderAccess.Run); AssemblyBuilder assembly = AssemblyBuilder.DefineDynamicAssembly(name, AssemblyBuilderAccess.Run); //ConstructorInfo securityTransparentCtor = typeof(SecurityTransparentAttribute).GetConstructor(Type.EmptyTypes); //CustomAttributeBuilder securityTransparentAttriBuilder = new CustomAttributeBuilder(securityTransparentCtor, new object[] { }); //assembly.SetCustomAttribute(securityTransparentAttriBuilder); ModuleBuilder module = assembly.DefineDynamicModule($"{clsDescription.ClassKey}.dll"); // define a proxy type in the main module TypeBuilder typeBuilder = module.DefineType(clsDescription.ClassName, TypeAttributes.Public); if (clsDescription.InterfaceTypes != null) { clsDescription.InterfaceTypes.ForEach(it => typeBuilder.AddInterfaceImplementation(it)); } // create a field to hold the proxy FieldBuilder proxyFieldBuilder = typeBuilder.DefineField("_proxy", typeof(RealProxyBase), FieldAttributes.InitOnly | FieldAttributes.Private); // Define constructors ILGenerator generator; // define a constructor ConstructorBuilder ctorBuilder = typeBuilder.DefineConstructor( MethodAttributes.Public, CallingConventions.Standard , new Type[] { typeof(RealProxyBase) } ); generator = ctorBuilder.GetILGenerator(); // invoke the object constructor generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Call, typeof(object).GetConstructor(Type.EmptyTypes)); // create a DynamicProxyHelper for this proxy generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldarg_1); generator.Emit(OpCodes.Stfld, proxyFieldBuilder); generator.Emit(OpCodes.Ret); // Define methods var proxiableMethods = GetProxiableMethods(clsDescription); var proxiableProps = GetProxiablePropertyMap(clsDescription); foreach (var propertyInfo in proxiableProps) { string getMethodName = "get_" + propertyInfo.Name; string setMethodName = "set_" + propertyInfo.Name; MethodInfo getMethod = (propertyInfo.CanRead) ? proxiableMethods.Find(it => getMethodName.Equals(it.Name)) : null; MethodInfo setMethod = (propertyInfo.CanWrite) ? proxiableMethods.Find(it => setMethodName.Equals(it.Name)) : null; GenProperty(typeBuilder, propertyInfo, proxyFieldBuilder, getMethod, setMethod); if (getMethod != null) { proxiableMethods.Remove(getMethod); } if (setMethod != null) { proxiableMethods.Remove(setMethod); } } foreach (MethodInfo methodInfo in proxiableMethods) { GenMethod(typeBuilder, methodInfo, proxyFieldBuilder); } type = typeBuilder.CreateType(); typeMap[clsDescription.ClassKey] = type; return(type); }
public RealProxyBase2(DynamicClassDescription clsDescription) { this.clsDescription = clsDescription; }