private static GeneratedTypeInfo GenerateProxyType( [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type baseType, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type interfaceType) { // Parameter validation is deferred until the point we need to create the proxy. // This prevents unnecessary overhead revalidating cached proxy types. // The interface type must be an interface, not a class if (!interfaceType.IsInterface) { // "T" is the generic parameter seen via the public contract throw new ArgumentException(SR.Format(SR.InterfaceType_Must_Be_Interface, interfaceType.FullName), "T"); } // The base type cannot be sealed because the proxy needs to subclass it. if (baseType.IsSealed) { // "TProxy" is the generic parameter seen via the public contract throw new ArgumentException(SR.Format(SR.BaseType_Cannot_Be_Sealed, baseType.FullName), "TProxy"); } // The base type cannot be abstract if (baseType.IsAbstract) { throw new ArgumentException(SR.Format(SR.BaseType_Cannot_Be_Abstract, baseType.FullName), "TProxy"); } // The base type must have a public default ctor if (baseType.GetConstructor(Type.EmptyTypes) == null) { throw new ArgumentException(SR.Format(SR.BaseType_Must_Have_Default_Ctor, baseType.FullName), "TProxy"); } // Create a type that derives from 'baseType' provided by caller ProxyBuilder pb = s_proxyAssembly.CreateProxy("generatedProxy", baseType); foreach (Type t in interfaceType.GetInterfaces()) { pb.AddInterfaceImpl(t); } pb.AddInterfaceImpl(interfaceType); GeneratedTypeInfo generatedProxyType = pb.CreateType(); return(generatedProxyType); }
// Unconditionally generates a new proxy type derived from 'baseType' and implements 'interfaceType' private static Type GenerateProxyType(Type baseType, Type interfaceType) { // Parameter validation is deferred until the point we need to create the proxy. // This prevents unnecessary overhead revalidating cached proxy types. TypeInfo baseTypeInfo = baseType.GetTypeInfo(); // The interface type must be an interface, not a class if (!interfaceType.GetTypeInfo().IsInterface) { // "T" is the generic parameter seen via the public contract throw new ArgumentException($"{interfaceType.FullName}必须是一个接口", "T"); } // The base type cannot be sealed because the proxy needs to subclass it. if (baseTypeInfo.IsSealed) { // "TProxy" is the generic parameter seen via the public contract throw new ArgumentException($"{baseTypeInfo.FullName}不能是Sealed", "TProxy"); } // The base type cannot be abstract if (baseTypeInfo.IsAbstract) { throw new ArgumentException($"{baseTypeInfo.FullName}不能是Abstract", "TProxy"); } // The base type must have a public default ctor if (!baseTypeInfo.DeclaredConstructors.Any(c => c.IsPublic && c.GetParameters().Length == 0)) { throw new ArgumentException($"{baseType.FullName}必须要有无参构造函数", "TProxy"); } // Create a type that derives from 'baseType' provided by caller var name = $"{interfaceType.Name.TrimStart('I')}GeneratedProxy"; ProxyBuilder pb = s_proxyAssembly.CreateProxy(name, baseType); foreach (Type t in interfaceType.GetTypeInfo().ImplementedInterfaces) { pb.AddInterfaceImpl(t); } pb.AddInterfaceImpl(interfaceType); Type generatedProxyType = pb.CreateType(); return(generatedProxyType); }
// Unconditionally generates a new proxy type derived from 'baseType' and implements 'interfaceType' private static Type GenerateProxyType(Type baseType, Type interfaceType) { // Parameter validation is deferred until the point we need to create the proxy. // This prevents unnecessary overhead revalidating cached proxy types. TypeInfo baseTypeInfo = baseType.GetTypeInfo(); // The interface type must be an interface, not a class if (!interfaceType.GetTypeInfo().IsInterface) { // "T" is the generic parameter seen via the public contract throw new ArgumentException(String.Format(SR.InterfaceType_Must_Be_Interface, interfaceType.FullName), "T"); } // The base type cannot be sealed because the proxy needs to subclass it. if (baseTypeInfo.IsSealed) { // "TProxy" is the generic parameter seen via the public contract throw new ArgumentException(String.Format(SR.BaseType_Cannot_Be_Sealed, baseTypeInfo.FullName), "TProxy"); } // The base type cannot be abstract if (baseTypeInfo.IsAbstract) { throw new ArgumentException(String.Format(SR.BaseType_Cannot_Be_Abstract, baseType.FullName), "TProxy"); } // The base type must have a public default ctor if (!baseTypeInfo.DeclaredConstructors.Any(c => c.IsPublic && c.GetParameters().Length == 0)) { throw new ArgumentException(String.Format(SR.BaseType_Must_Have_Default_Ctor, baseType.FullName), "TProxy"); } // Create a type that derives from 'baseType' provided by caller ProxyBuilder pb = s_proxyAssembly.CreateProxy("generatedProxy", baseType); foreach (Type t in interfaceType.GetTypeInfo().ImplementedInterfaces) { pb.AddInterfaceImpl(t); } pb.AddInterfaceImpl(interfaceType); Type generatedProxyType = pb.CreateType(); return(generatedProxyType); }
Type GetProxyType() { // lock? if (this.proxyType == null) { lock (proxyTypes) { if (!proxyTypes.TryGetValue(this.proxiedType, out this.proxyType)) { if (proxyAssembly == null) { proxyAssembly = new ProxyAssembly(DynamicAssemblyName); } Type proxyBaseType; if (!proxyTypes.TryGetValue(this.baseType, out proxyBaseType)) { ProxyBuilder pbt = proxyAssembly.CreateProxy("proxyBase", typeof(object)); foreach (Type t in baseType.GetInterfaces()) { pbt.AddInterfaceImpl(t); } proxyTypes[this.baseType] = proxyBaseType = pbt.CreateType(); } ProxyBuilder pb = proxyAssembly.CreateProxy("proxy", proxyBaseType); foreach (Type t in this.proxiedType.GetInterfaces()) { pb.AddInterfaceImpl(t); } pb.AddInterfaceImpl(this.proxiedType); proxyTypes[this.proxiedType] = this.proxyType = pb.CreateType(); } } } return(proxyType); }
private static Type GenerateProxyType(Type baseType, Type interfaceType) { TypeInfo baseTypeInfo = baseType.GetTypeInfo(); if (!interfaceType.GetTypeInfo().IsInterface) { throw new Exception(); } if (baseTypeInfo.IsSealed) { throw new Exception(); } if (baseTypeInfo.IsAbstract) { throw new Exception(); } if (!baseTypeInfo.DeclaredConstructors.Any(c => c.IsPublic && c.GetParameters().Length == 0)) { throw new Exception(); } ProxyBuilder pb = s_proxyAssembly.CreateProxy("generatedProxy", baseType); foreach (Type t in interfaceType.GetTypeInfo().ImplementedInterfaces) { pb.AddInterfaceImpl(t); } pb.AddInterfaceImpl(interfaceType); Type generatedProxyType = pb.CreateType(); return(generatedProxyType); }