private Type GetProxyType(Type contractType) { Type clientBaseType = typeof(ClientBase <>).MakeGenericType( contractType); Type[] allTypes = ProxyAssembly.GetTypes(); Type proxyType = null; foreach (Type type in allTypes) { // Look for a proxy class that implements the service // contract and is derived from ClientBase<service contract> if (type.IsClass && contractType.IsAssignableFrom(type) && type.IsSubclassOf(clientBaseType)) { proxyType = type; break; } } if (proxyType == null) { throw new DynamicProxyException(string.Format( Constants.ErrorMessages.ProxyTypeNotFound, contractType.FullName)); } return(proxyType); }
/// <summary> /// Creates the instance. /// </summary> /// <param name="objTypeName">Name of the obj type.</param> /// <returns></returns> private object CreateInstance(string objTypeName) { try { foreach (Type ty in ProxyAssembly.GetTypes()) { if (ty.BaseType == typeof(SoapHttpClientProtocolExtended)) { if (objTypeName == null || objTypeName.Length == 0 || ty.Name == objTypeName) { objTypeName = ty.Name; break; } } } Type t = ass.GetType(CodeConstants.CODENAMESPACE + "." + objTypeName); return(Activator.CreateInstance(t)); } catch (Exception ex) { throw new ProxyTypeInstantiationException("An error occured while instantiating the proxy type.", ex); } catch { throw new ProxyTypeInstantiationException("An error occured while instantiating the proxy type."); } }
internal ProxyBuilder(ProxyAssembly assembly, TypeBuilder tb, Type proxyBaseType) { _assembly = assembly; _tb = tb; _proxyBaseType = proxyBaseType; _fields = new List <FieldBuilder>(); _fields.Add(tb.DefineField("invoke", typeof(Action <object[]>), FieldAttributes.Private)); }
internal ProxyBuilder(ProxyAssembly assembly, TypeBuilder tb, Type proxyBaseType) { _assembly = assembly; _tb = tb; _proxyBaseType = proxyBaseType; _fields = new List <FieldBuilder>(); _fields.Add(tb.DefineField("_handler", typeof(ProxyHandler), FieldAttributes.Private)); }
internal ProxyBuilder(ProxyAssembly assembly, TypeBuilder tb, Type proxyBaseType) { this.assembly = assembly; this.tb = tb; this.proxyBaseType = proxyBaseType; this.fields = new List <FieldBuilder> { tb.DefineField("proxy", typeof(object), FieldAttributes.Private), tb.DefineField("invoke", typeof(Action <object[]>), FieldAttributes.Private) }; }
internal ProxyBuilder( ProxyAssembly assembly, TypeBuilder tb, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type proxyBaseType) { _assembly = assembly; _tb = tb; _proxyBaseType = proxyBaseType; _fields = new List <FieldBuilder>(); _fields.Add(tb.DefineField("invoke", typeof(Action <object[]>), FieldAttributes.Private)); }
/// <summary> /// Инициализирует экземпляр <see cref="ProxyTypeGenerator"/> /// </summary> public ProxyTypeGenerator( TypeBuilder dynamicTypeBuilder, ProxyAssembly proxyAssembly, MethodLinkStore methodLinkStore) : base(dynamicTypeBuilder) { _proxyAssembly = proxyAssembly; _methodLinkStore = methodLinkStore; _invokeDelegateField = DynamicTypeBuilder.DefineField("invoke", typeof(Action <object[]>), FieldAttributes.Private); }
private Type GetProxyType(Type contractType) { Type clientBaseType = typeof(ClientBase <>).MakeGenericType(contractType); Type[] allTypes = ProxyAssembly.GetTypes(); Type proxyType = allTypes.FirstOrDefault(type => type.IsClass && contractType.IsAssignableFrom(type) && type.IsSubclassOf(clientBaseType)); if (proxyType == null) { throw new DynamicProxyException(string.Format(Constants.ErrorMessages.ProxyTypeNotFound, contractType.FullName)); } return(proxyType); }
internal ProxyBuilder( ProxyAssembly assembly, TypeBuilder tb, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type proxyBaseType) { _assembly = assembly; _tb = tb; _proxyBaseType = proxyBaseType; _fields = new List <FieldBuilder>(); _fields.Add(tb.DefineField("_methodInfos", typeof(MethodInfo[]), FieldAttributes.Private)); _methodInfos = new List <MethodInfo>(); _assembly.EnsureTypeIsVisible(proxyBaseType); }
private object CreateInstance(string objTypeName) { // check whether the type is already created or not if (objTypeName == "" || objTypeName == null) { foreach (Type ty in ProxyAssembly.GetTypes()) { if (ty.BaseType == typeof(SoapHttpClientProtocolEx)) { objTypeName = ty.Name; break; } } } Type t = ass.GetType("OW.Tools.WebServices.DynamicProxy." + objTypeName); return(Activator.CreateInstance(t)); }
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); }
/// <summary> /// Creates the instance. /// </summary> /// <returns></returns> private object CreateProxyInstance() { string objTypeName = null; try { foreach (Type ty in ProxyAssembly.GetTypes()) { if (ty.BaseType == typeof(SoapHttpClientProtocolExtended)) { objTypeName = ty.Name; break; } } Type t = ass.GetType(CodeConstants.CODENAMESPACE + "." + objTypeName); return(Activator.CreateInstance(t)); } catch (Exception ex) { throw new ProxyTypeInstantiationException("An error occured while instantiating the proxy type: " + ex.Message + ", " + ex.StackTrace, ex); } }