public static void CreateHandlerType(ContractInfo contractMembers, out Dictionary <PropertyInfo, string> delegateToMethodsMap, out Type generatedType) { var interlocutorType = typeof(IInterlocutor); var typeCount = Interlocked.Increment(ref _exemmplarCounter); var typeBuilder = EmitHelper.CreateTypeBuilder(contractMembers.ContractInterfaceType.Name + "_" + typeCount); delegateToMethodsMap = new Dictionary <PropertyInfo, string>(); const string interlocutorFieldName = "_interlocutor"; var outputApiFieldInfo = typeBuilder.DefineField( interlocutorFieldName, typeof(IInterlocutor), FieldAttributes.Private); EmitHelper.ImplementPublicConstructor(typeBuilder, new[] { outputApiFieldInfo }); var sayMehodInfo = interlocutorType.GetMethod("Say", new[] { typeof(int), typeof(object[]) }); foreach (var property in contractMembers.GetProperties()) { MethodBuilder metbuilder; #region generates delegate handlerMethod {property.Value.Name}DelegateHandler var delegateInfo = ReflectionHelper.GetDelegateInfoOrNull(property.Value.PropertyType); if (delegateInfo == null) { //the property is not an delegate throw new InvalidContractMemeberException(property.Value, contractMembers.ContractInterfaceType); } Type[] parameterTypes = delegateInfo.ParameterTypes; Type returnType = delegateInfo.ReturnType; if (!parameterTypes.Any() && returnType == typeof(void)) { metbuilder = typeBuilder.DefineMethod(property.Value.Name + "DelegateHandler", MethodAttributes.Public); } else { metbuilder = typeBuilder.DefineMethod(property.Value.Name + "DelegateHandler", MethodAttributes.Public, returnType, parameterTypes); } MethodInfo askOrSayMethodInfo = null; if (returnType == typeof(void)) { askOrSayMethodInfo = sayMehodInfo; } else { askOrSayMethodInfo = interlocutorType .GetMethod("Ask", new[] { typeof(int), typeof(object[]) }) .MakeGenericMethod(returnType); } EmitHelper.GenerateSayOrAskMethodBody( messageTypeId: property.Key, interlocutorSayOrAskMethodInfo: askOrSayMethodInfo, interlocutorFieldInfo: outputApiFieldInfo, methodBuilder: metbuilder, callParameters: parameterTypes); #endregion delegateToMethodsMap.Add(property.Value, metbuilder.Name); } generatedType = typeBuilder.CreateType(); }
private static TypeBuilder CreateProxyTypeBuilder <T>() { var typeCount = Interlocked.Increment(ref _exemmplarCounter); return(EmitHelper.CreateTypeBuilder(typeof(T).Name + "_" + typeCount)); }