internal static void AddCallbackImplementation(CodeTypeDeclaration codeClass, string callbackName, string handlerName, string handlerArgs, bool methodHasOutParameters)
 {
     CodeFlags[] parameterFlags = new CodeFlags[1];
     CodeMemberMethod method = AddMethod(codeClass, callbackName, parameterFlags, new string[] { typeof(object).FullName }, new string[] { "arg" }, typeof(void).FullName, null, (CodeFlags) 0);
     CodeEventReferenceExpression left = new CodeEventReferenceExpression(new CodeThisReferenceExpression(), handlerName);
     CodeBinaryOperatorExpression condition = new CodeBinaryOperatorExpression(left, CodeBinaryOperatorType.IdentityInequality, new CodePrimitiveExpression(null));
     CodeStatement[] trueStatements = new CodeStatement[2];
     trueStatements[0] = new CodeVariableDeclarationStatement(typeof(InvokeCompletedEventArgs), "invokeArgs", new CodeCastExpression(typeof(InvokeCompletedEventArgs), new CodeArgumentReferenceExpression("arg")));
     CodeVariableReferenceExpression targetObject = new CodeVariableReferenceExpression("invokeArgs");
     CodeObjectCreateExpression expression4 = new CodeObjectCreateExpression();
     if (methodHasOutParameters)
     {
         expression4.CreateType = new CodeTypeReference(handlerArgs);
         expression4.Parameters.Add(new CodePropertyReferenceExpression(targetObject, "Results"));
     }
     else
     {
         expression4.CreateType = new CodeTypeReference(typeof(AsyncCompletedEventArgs));
     }
     expression4.Parameters.Add(new CodePropertyReferenceExpression(targetObject, "Error"));
     expression4.Parameters.Add(new CodePropertyReferenceExpression(targetObject, "Cancelled"));
     expression4.Parameters.Add(new CodePropertyReferenceExpression(targetObject, "UserState"));
     trueStatements[1] = new CodeExpressionStatement(new CodeDelegateInvokeExpression(new CodeEventReferenceExpression(new CodeThisReferenceExpression(), handlerName), new CodeExpression[] { new CodeThisReferenceExpression(), expression4 }));
     method.Statements.Add(new CodeConditionStatement(condition, trueStatements, new CodeStatement[0]));
 }
        internal static void AddCallbackImplementation(CodeTypeDeclaration codeClass, string callbackName, string handlerName, string handlerArgs, bool methodHasOutParameters)
        {
            CodeFlags[]                  parameterFlags = new CodeFlags[1];
            CodeMemberMethod             method         = AddMethod(codeClass, callbackName, parameterFlags, new string[] { typeof(object).FullName }, new string[] { "arg" }, typeof(void).FullName, null, (CodeFlags)0);
            CodeEventReferenceExpression left           = new CodeEventReferenceExpression(new CodeThisReferenceExpression(), handlerName);
            CodeBinaryOperatorExpression condition      = new CodeBinaryOperatorExpression(left, CodeBinaryOperatorType.IdentityInequality, new CodePrimitiveExpression(null));

            CodeStatement[] trueStatements = new CodeStatement[2];
            trueStatements[0] = new CodeVariableDeclarationStatement(typeof(InvokeCompletedEventArgs), "invokeArgs", new CodeCastExpression(typeof(InvokeCompletedEventArgs), new CodeArgumentReferenceExpression("arg")));
            CodeVariableReferenceExpression targetObject = new CodeVariableReferenceExpression("invokeArgs");
            CodeObjectCreateExpression      expression4  = new CodeObjectCreateExpression();

            if (methodHasOutParameters)
            {
                expression4.CreateType = new CodeTypeReference(handlerArgs);
                expression4.Parameters.Add(new CodePropertyReferenceExpression(targetObject, "Results"));
            }
            else
            {
                expression4.CreateType = new CodeTypeReference(typeof(AsyncCompletedEventArgs));
            }
            expression4.Parameters.Add(new CodePropertyReferenceExpression(targetObject, "Error"));
            expression4.Parameters.Add(new CodePropertyReferenceExpression(targetObject, "Cancelled"));
            expression4.Parameters.Add(new CodePropertyReferenceExpression(targetObject, "UserState"));
            trueStatements[1] = new CodeExpressionStatement(new CodeDelegateInvokeExpression(new CodeEventReferenceExpression(new CodeThisReferenceExpression(), handlerName), new CodeExpression[] { new CodeThisReferenceExpression(), expression4 }));
            method.Statements.Add(new CodeConditionStatement(condition, trueStatements, new CodeStatement[0]));
        }
 internal static void GetCodeFlags(IList parameters, CodeFlags[] codeFlags, int start, int specifiedCount)
 {
     int num = 0;
     for (int i = 0; i < parameters.Count; i++)
     {
         codeFlags[(i + start) + num] = ((SoapParameter) parameters[i]).codeFlags;
         if (((SoapParameter) parameters[i]).mapping.CheckSpecified)
         {
             num++;
             codeFlags[(i + start) + num] = ((SoapParameter) parameters[i]).codeFlags;
         }
     }
 }
Example #4
0
        protected override CodeTypeDeclaration BeginClass()
        {
            MethodNames.Clear();
            ExtraCodeClasses.Clear();
            CodeAttributeDeclarationCollection metadata = new CodeAttributeDeclarationCollection();

            if (Style == ServiceDescriptionImportStyle.Client)
            {
                WebCodeGenerator.AddCustomAttribute(metadata, typeof(DebuggerStepThroughAttribute), new CodeExpression[0]);
                WebCodeGenerator.AddCustomAttribute(metadata, typeof(DesignerCategoryAttribute), new CodeExpression[] { new CodePrimitiveExpression("code") });
            }

            Type[] requiredTypes = new Type[] {
                typeof(SoapDocumentMethodAttribute),
                typeof(XmlAttributeAttribute),
                typeof(WebService),
                typeof(Object),
                typeof(DebuggerStepThroughAttribute),
                typeof(DesignerCategoryAttribute),
                typeof(TransactionOption),
            };
            WebCodeGenerator.AddImports(this.CodeNamespace, WebCodeGenerator.GetNamespacesForTypes(requiredTypes));
            CodeFlags flags = 0;

            if (Style == ServiceDescriptionImportStyle.Server)
            {
                flags = CodeFlags.IsAbstract;
            }
            else if (Style == ServiceDescriptionImportStyle.ServerInterface)
            {
                flags = CodeFlags.IsInterface;
            }
            CodeTypeDeclaration codeClass = WebCodeGenerator.CreateClass(this.ClassName, BaseClass.FullName,
                                                                         new string[0], metadata, CodeFlags.IsPublic | flags,
                                                                         ServiceImporter.CodeGenerator.Supports(GeneratorSupport.PartialTypes));

            codeClass.Comments.Add(new CodeCommentStatement(Res.GetString(Res.CodeRemarks), true));

            CodeConstructor ctor = WebCodeGenerator.AddConstructor(codeClass, new string[0], new string[0], null, CodeFlags.IsPublic);

            ctor.Comments.Add(new CodeCommentStatement(Res.GetString(Res.CodeRemarks), true));

            HttpAddressBinding httpAddressBinding = Port == null ? null : (HttpAddressBinding)Port.Extensions.Find(typeof(HttpAddressBinding));
            string             url = (httpAddressBinding != null) ? httpAddressBinding.Location : null;
            ServiceDescription serviceDescription = Binding.ServiceDescription;

            ProtocolImporterUtil.GenerateConstructorStatements(ctor, url, serviceDescription.AppSettingUrlKey, serviceDescription.AppSettingBaseUrl, false);

            codeClasses.Add(codeClass);
            return(codeClass);
        }
 internal static CodeFlags[] GetCodeFlags(IList parameters, int specifiedCount)
 {
     CodeFlags[] codeFlags = new CodeFlags[parameters.Count + specifiedCount];
     GetCodeFlags(parameters, codeFlags, 0, specifiedCount);
     return codeFlags;
 }
        internal static CodeTypeDeclaration CreateClass(string className, string baseClassName, string[] implementedInterfaceNames, CodeAttributeDeclarationCollection metadata, CodeFlags flags, bool isPartial) {
            CodeTypeDeclaration codeClass = new CodeTypeDeclaration(className);
            
            if (baseClassName != null && baseClassName.Length > 0)
                codeClass.BaseTypes.Add(baseClassName);
            foreach (string interfaceName in implementedInterfaceNames)
                codeClass.BaseTypes.Add(interfaceName);
            codeClass.IsStruct = (flags & CodeFlags.IsStruct) != 0;
            if ((flags & CodeFlags.IsPublic) != 0)
                codeClass.TypeAttributes |= TypeAttributes.Public;
            else
                codeClass.TypeAttributes &= ~TypeAttributes.Public;
            if ((flags & CodeFlags.IsAbstract) != 0)
                codeClass.TypeAttributes |= TypeAttributes.Abstract;
            else
                codeClass.TypeAttributes &= ~TypeAttributes.Abstract;

            if ((flags & CodeFlags.IsInterface) != 0)
                codeClass.IsInterface = true;
            else
                codeClass.IsPartial = isPartial;

            codeClass.CustomAttributes = metadata;
            codeClass.CustomAttributes.Add(GeneratedCodeAttribute);
            return codeClass;
        }
Example #7
0
        internal static CodeMemberField AddField(CodeTypeDeclaration codeClass, string fieldTypeName, string fieldName, CodeExpression initializer, CodeAttributeDeclarationCollection metadata, CodeFlags flags)
        {
            CodeMemberField field = new CodeMemberField(fieldTypeName, fieldName);

            field.InitExpression   = initializer;
            field.CustomAttributes = metadata;
            if ((flags & CodeFlags.IsPublic) != 0)
            {
                field.Attributes = (field.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            }
            // CONSIDER, stefanph: CodeDom doesn't support const. It should.
            codeClass.Members.Add(field);
            return(field);
        }
Example #8
0
 internal static CodeMemberMethod AddMethod(CodeTypeDeclaration codeClass, string methodName,
                                            CodeFlags[] parameterFlags, string[] parameterTypeNames, string[] parameterNames,
                                            string returnTypeName, CodeAttributeDeclarationCollection metadata, CodeFlags flags)
 {
     return(AddMethod(codeClass, methodName, parameterFlags,
                      parameterTypeNames, parameterNames, new CodeAttributeDeclarationCollection[0],
                      returnTypeName, metadata, flags));
 }
 protected override CodeMemberMethod GenerateMethod()
 {
     Message outputMessage;
     SoapBodyBinding binding2;
     MessageBinding output;
     bool flag;
     SoapOperationBinding binding5 = (SoapOperationBinding) base.OperationBinding.Extensions.Find(typeof(SoapOperationBinding));
     if (binding5 == null)
     {
         throw base.OperationBindingSyntaxException(System.Web.Services.Res.GetString("MissingSoapOperationBinding0"));
     }
     SoapBindingStyle soapBindingStyle = binding5.Style;
     if (soapBindingStyle == SoapBindingStyle.Default)
     {
         soapBindingStyle = this.SoapBinding.Style;
     }
     if (soapBindingStyle == SoapBindingStyle.Default)
     {
         soapBindingStyle = SoapBindingStyle.Document;
     }
     string[] parameterOrder = base.Operation.ParameterOrder;
     Message inputMessage = base.InputMessage;
     MessageBinding input = base.OperationBinding.Input;
     SoapBodyBinding soapBodyBinding = (SoapBodyBinding) base.OperationBinding.Input.Extensions.Find(typeof(SoapBodyBinding));
     if (soapBodyBinding == null)
     {
         base.UnsupportedOperationBindingWarning(System.Web.Services.Res.GetString("MissingSoapBodyInputBinding0"));
         return null;
     }
     if (base.Operation.Messages.Output != null)
     {
         outputMessage = base.OutputMessage;
         output = base.OperationBinding.Output;
         binding2 = (SoapBodyBinding) base.OperationBinding.Output.Extensions.Find(typeof(SoapBodyBinding));
         if (binding2 == null)
         {
             base.UnsupportedOperationBindingWarning(System.Web.Services.Res.GetString("MissingSoapBodyOutputBinding0"));
             return null;
         }
     }
     else
     {
         outputMessage = null;
         output = null;
         binding2 = null;
     }
     CodeAttributeDeclarationCollection metadata = new CodeAttributeDeclarationCollection();
     this.PrepareHeaders(input);
     if (output != null)
     {
         this.PrepareHeaders(output);
     }
     string name = null;
     string str = (!string.IsNullOrEmpty(input.Name) && (soapBindingStyle != SoapBindingStyle.Rpc)) ? input.Name : base.Operation.Name;
     str = XmlConvert.DecodeName(str);
     if (output != null)
     {
         name = (!string.IsNullOrEmpty(output.Name) && (soapBindingStyle != SoapBindingStyle.Rpc)) ? output.Name : (base.Operation.Name + "Response");
         name = XmlConvert.DecodeName(name);
     }
     this.GenerateExtensionMetadata(metadata);
     this.GenerateHeaders(metadata, soapBodyBinding.Use, soapBindingStyle == SoapBindingStyle.Rpc, input, output);
     MessagePart[] messageParts = this.GetMessageParts(inputMessage, soapBodyBinding);
     if (!this.CheckMessageStyles(base.MethodName, messageParts, soapBodyBinding, soapBindingStyle, out flag))
     {
         return null;
     }
     MessagePart[] parts = null;
     if (outputMessage != null)
     {
         bool flag2;
         parts = this.GetMessageParts(outputMessage, binding2);
         if (!this.CheckMessageStyles(base.MethodName, parts, binding2, soapBindingStyle, out flag2))
         {
             return null;
         }
         if (flag != flag2)
         {
             flag = false;
         }
     }
     bool flag3 = ((soapBindingStyle != SoapBindingStyle.Rpc) && flag) || ((soapBodyBinding.Use == SoapBindingUse.Literal) && (soapBindingStyle == SoapBindingStyle.Rpc));
     XmlMembersMapping request = this.ImportMessage(str, messageParts, soapBodyBinding, soapBindingStyle, flag);
     if (request == null)
     {
         return null;
     }
     XmlMembersMapping response = null;
     if (outputMessage != null)
     {
         response = this.ImportMessage(name, parts, binding2, soapBindingStyle, flag);
         if (response == null)
         {
             return null;
         }
     }
     string str3 = CodeIdentifier.MakeValid(XmlConvert.DecodeName(base.Operation.Name));
     if (base.ClassName == str3)
     {
         str3 = "Call" + str3;
     }
     string identifier = base.MethodNames.AddUnique(CodeIdentifier.MakeValid(XmlConvert.DecodeName(str3)), base.Operation);
     bool flag4 = str3 != identifier;
     CodeIdentifiers identifiers = new CodeIdentifiers(false);
     identifiers.AddReserved(identifier);
     SoapParameters parameters = new SoapParameters(request, response, parameterOrder, base.MethodNames);
     foreach (SoapParameter parameter in parameters.Parameters)
     {
         if ((parameter.IsOut || parameter.IsByRef) && !base.ServiceImporter.CodeGenerator.Supports(GeneratorSupport.ReferenceParameters))
         {
             base.UnsupportedOperationWarning(System.Web.Services.Res.GetString("CodeGenSupportReferenceParameters", new object[] { base.ServiceImporter.CodeGenerator.GetType().Name }));
             return null;
         }
         parameter.name = identifiers.AddUnique(parameter.name, null);
         if (parameter.mapping.CheckSpecified)
         {
             parameter.specifiedName = identifiers.AddUnique(parameter.name + "Specified", null);
         }
     }
     if ((base.Style != ServiceDescriptionImportStyle.Client) || flag4)
     {
         this.BeginMetadata();
         if (flag4)
         {
             this.AddMetadataProperty("MessageName", identifier);
         }
         this.EndMetadata(metadata, typeof(WebMethodAttribute), null);
     }
     this.BeginMetadata();
     if ((flag3 && (request.ElementName.Length > 0)) && (request.ElementName != identifier))
     {
         this.AddMetadataProperty("RequestElementName", request.ElementName);
     }
     if (request.Namespace != null)
     {
         this.AddMetadataProperty("RequestNamespace", request.Namespace);
     }
     if (response == null)
     {
         this.AddMetadataProperty("OneWay", true);
     }
     else
     {
         if ((flag3 && (response.ElementName.Length > 0)) && (response.ElementName != (identifier + "Response")))
         {
             this.AddMetadataProperty("ResponseElementName", response.ElementName);
         }
         if (response.Namespace != null)
         {
             this.AddMetadataProperty("ResponseNamespace", response.Namespace);
         }
     }
     if (soapBindingStyle == SoapBindingStyle.Rpc)
     {
         if (soapBodyBinding.Use != SoapBindingUse.Encoded)
         {
             this.AddMetadataProperty("Use", (CodeExpression) new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(SoapBindingUse).FullName), Enum.Format(typeof(SoapBindingUse), soapBodyBinding.Use, "G")));
         }
         this.EndMetadata(metadata, typeof(SoapRpcMethodAttribute), binding5.SoapAction);
     }
     else
     {
         this.AddMetadataProperty("Use", (CodeExpression) new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(SoapBindingUse).FullName), Enum.Format(typeof(SoapBindingUse), soapBodyBinding.Use, "G")));
         this.AddMetadataProperty("ParameterStyle", (CodeExpression) new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(SoapParameterStyle).FullName), Enum.Format(typeof(SoapParameterStyle), flag ? SoapParameterStyle.Wrapped : SoapParameterStyle.Bare, "G")));
         this.EndMetadata(metadata, typeof(SoapDocumentMethodAttribute), binding5.SoapAction);
     }
     base.IsEncodedBinding = base.IsEncodedBinding || (soapBodyBinding.Use == SoapBindingUse.Encoded);
     CodeAttributeDeclarationCollection[] parameterAttributes = new CodeAttributeDeclarationCollection[parameters.Parameters.Count + parameters.CheckSpecifiedCount];
     int index = 0;
     CodeAttributeDeclaration declaration = new CodeAttributeDeclaration(typeof(XmlIgnoreAttribute).FullName);
     foreach (SoapParameter parameter2 in parameters.Parameters)
     {
         parameterAttributes[index] = new CodeAttributeDeclarationCollection();
         if (soapBodyBinding.Use == SoapBindingUse.Encoded)
         {
             this.soapExporter.AddMappingMetadata(parameterAttributes[index], parameter2.mapping, parameter2.name != parameter2.mapping.MemberName);
         }
         else
         {
             string ns = (soapBindingStyle == SoapBindingStyle.Rpc) ? parameter2.mapping.Namespace : (parameter2.IsOut ? response.Namespace : request.Namespace);
             bool forceUseMemberName = parameter2.name != parameter2.mapping.MemberName;
             this.xmlExporter.AddMappingMetadata(parameterAttributes[index], parameter2.mapping, ns, forceUseMemberName);
             if (parameter2.mapping.CheckSpecified)
             {
                 index++;
                 parameterAttributes[index] = new CodeAttributeDeclarationCollection();
                 this.xmlExporter.AddMappingMetadata(parameterAttributes[index], parameter2.mapping, ns, parameter2.specifiedName != (parameter2.mapping.MemberName + "Specified"));
                 parameterAttributes[index].Add(declaration);
             }
         }
         if ((parameterAttributes[index].Count > 0) && !base.ServiceImporter.CodeGenerator.Supports(GeneratorSupport.ParameterAttributes))
         {
             base.UnsupportedOperationWarning(System.Web.Services.Res.GetString("CodeGenSupportParameterAttributes", new object[] { base.ServiceImporter.CodeGenerator.GetType().Name }));
             return null;
         }
         index++;
     }
     CodeFlags[] codeFlags = SoapParameter.GetCodeFlags(parameters.Parameters, parameters.CheckSpecifiedCount);
     string[] parameterTypeNames = SoapParameter.GetTypeFullNames(parameters.Parameters, parameters.CheckSpecifiedCount, base.ServiceImporter.CodeGenerator);
     string returnTypeName = (parameters.Return == null) ? typeof(void).FullName : WebCodeGenerator.FullTypeName(parameters.Return, base.ServiceImporter.CodeGenerator);
     CodeMemberMethod codeMethod = WebCodeGenerator.AddMethod(base.CodeTypeDeclaration, str3, codeFlags, parameterTypeNames, SoapParameter.GetNames(parameters.Parameters, parameters.CheckSpecifiedCount), parameterAttributes, returnTypeName, metadata, CodeFlags.IsPublic | ((base.Style == ServiceDescriptionImportStyle.Client) ? ((CodeFlags) 0) : CodeFlags.IsAbstract));
     codeMethod.Comments.Add(new CodeCommentStatement(System.Web.Services.Res.GetString("CodeRemarks"), true));
     if (parameters.Return != null)
     {
         if (soapBodyBinding.Use == SoapBindingUse.Encoded)
         {
             this.soapExporter.AddMappingMetadata(codeMethod.ReturnTypeCustomAttributes, parameters.Return, parameters.Return.ElementName != (identifier + "Result"));
         }
         else
         {
             this.xmlExporter.AddMappingMetadata(codeMethod.ReturnTypeCustomAttributes, parameters.Return, response.Namespace, parameters.Return.ElementName != (identifier + "Result"));
         }
         if ((codeMethod.ReturnTypeCustomAttributes.Count != 0) && !base.ServiceImporter.CodeGenerator.Supports(GeneratorSupport.ReturnTypeAttributes))
         {
             base.UnsupportedOperationWarning(System.Web.Services.Res.GetString("CodeGenSupportReturnTypeAttributes", new object[] { base.ServiceImporter.CodeGenerator.GetType().Name }));
             return null;
         }
     }
     string resultsName = identifiers.MakeUnique("results");
     if (base.Style == ServiceDescriptionImportStyle.Client)
     {
         bool flag6 = (base.ServiceImporter.CodeGenerationOptions & CodeGenerationOptions.GenerateOldAsync) != CodeGenerationOptions.None;
         bool flag7 = (((base.ServiceImporter.CodeGenerationOptions & CodeGenerationOptions.GenerateNewAsync) != CodeGenerationOptions.None) && base.ServiceImporter.CodeGenerator.Supports(GeneratorSupport.DeclareEvents)) && base.ServiceImporter.CodeGenerator.Supports(GeneratorSupport.DeclareDelegates);
         CodeExpression[] invokeParams = new CodeExpression[2];
         this.CreateInvokeParams(invokeParams, identifier, parameters.InParameters, parameters.InCheckSpecifiedCount);
         CodeMethodInvokeExpression invoke = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "Invoke", invokeParams);
         this.WriteReturnMappings(codeMethod, invoke, parameters, resultsName);
         if (flag6)
         {
             int num2 = parameters.InParameters.Count + parameters.InCheckSpecifiedCount;
             string[] typeFullNames = new string[num2 + 2];
             SoapParameter.GetTypeFullNames(parameters.InParameters, typeFullNames, 0, parameters.InCheckSpecifiedCount, base.ServiceImporter.CodeGenerator);
             typeFullNames[num2] = typeof(AsyncCallback).FullName;
             typeFullNames[num2 + 1] = typeof(object).FullName;
             string[] strArray4 = new string[num2 + 2];
             SoapParameter.GetNames(parameters.InParameters, strArray4, 0, parameters.InCheckSpecifiedCount);
             strArray4[num2] = "callback";
             strArray4[num2 + 1] = "asyncState";
             CodeFlags[] parameterFlags = new CodeFlags[num2 + 2];
             CodeMemberMethod method2 = WebCodeGenerator.AddMethod(base.CodeTypeDeclaration, "Begin" + identifier, parameterFlags, typeFullNames, strArray4, typeof(IAsyncResult).FullName, null, CodeFlags.IsPublic);
             method2.Comments.Add(new CodeCommentStatement(System.Web.Services.Res.GetString("CodeRemarks"), true));
             invokeParams = new CodeExpression[4];
             this.CreateInvokeParams(invokeParams, identifier, parameters.InParameters, parameters.InCheckSpecifiedCount);
             invokeParams[2] = new CodeArgumentReferenceExpression("callback");
             invokeParams[3] = new CodeArgumentReferenceExpression("asyncState");
             invoke = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "BeginInvoke", invokeParams);
             method2.Statements.Add(new CodeMethodReturnStatement(invoke));
             int num3 = parameters.OutParameters.Count + parameters.OutCheckSpecifiedCount;
             string[] strArray5 = new string[num3 + 1];
             SoapParameter.GetTypeFullNames(parameters.OutParameters, strArray5, 1, parameters.OutCheckSpecifiedCount, base.ServiceImporter.CodeGenerator);
             strArray5[0] = typeof(IAsyncResult).FullName;
             string[] strArray6 = new string[num3 + 1];
             SoapParameter.GetNames(parameters.OutParameters, strArray6, 1, parameters.OutCheckSpecifiedCount);
             strArray6[0] = "asyncResult";
             CodeFlags[] flagsArray3 = new CodeFlags[num3 + 1];
             for (int i = 0; i < num3; i++)
             {
                 flagsArray3[i + 1] = CodeFlags.IsOut;
             }
             CodeMemberMethod method3 = WebCodeGenerator.AddMethod(base.CodeTypeDeclaration, "End" + identifier, flagsArray3, strArray5, strArray6, (parameters.Return == null) ? typeof(void).FullName : WebCodeGenerator.FullTypeName(parameters.Return, base.ServiceImporter.CodeGenerator), null, CodeFlags.IsPublic);
             method3.Comments.Add(new CodeCommentStatement(System.Web.Services.Res.GetString("CodeRemarks"), true));
             CodeExpression expression2 = new CodeArgumentReferenceExpression("asyncResult");
             invoke = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "EndInvoke", new CodeExpression[] { expression2 });
             this.WriteReturnMappings(method3, invoke, parameters, resultsName);
         }
         if (!flag7)
         {
             return codeMethod;
         }
         string str8 = ProtocolImporter.MethodSignature(identifier, returnTypeName, codeFlags, parameterTypeNames);
         DelegateInfo info = (DelegateInfo) base.ExportContext[str8];
         if (info == null)
         {
             string handlerType = base.ClassNames.AddUnique(identifier + "CompletedEventHandler", identifier);
             string handlerArgs = base.ClassNames.AddUnique(identifier + "CompletedEventArgs", identifier);
             info = new DelegateInfo(handlerType, handlerArgs);
         }
         string handlerName = base.MethodNames.AddUnique(identifier + "Completed", identifier);
         string methodName = base.MethodNames.AddUnique(identifier + "Async", identifier);
         string callbackMember = base.MethodNames.AddUnique(identifier + "OperationCompleted", identifier);
         string callbackName = base.MethodNames.AddUnique("On" + identifier + "OperationCompleted", identifier);
         WebCodeGenerator.AddEvent(base.CodeTypeDeclaration.Members, info.handlerType, handlerName);
         WebCodeGenerator.AddCallbackDeclaration(base.CodeTypeDeclaration.Members, callbackMember);
         string[] names = SoapParameter.GetNames(parameters.InParameters, parameters.InCheckSpecifiedCount);
         string userState = ProtocolImporter.UniqueName("userState", names);
         CodeMemberMethod method4 = WebCodeGenerator.AddAsyncMethod(base.CodeTypeDeclaration, methodName, SoapParameter.GetTypeFullNames(parameters.InParameters, parameters.InCheckSpecifiedCount, base.ServiceImporter.CodeGenerator), names, callbackMember, callbackName, userState);
         invokeParams = new CodeExpression[4];
         this.CreateInvokeParams(invokeParams, identifier, parameters.InParameters, parameters.InCheckSpecifiedCount);
         invokeParams[2] = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), callbackMember);
         invokeParams[3] = new CodeArgumentReferenceExpression(userState);
         invoke = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "InvokeAsync", invokeParams);
         method4.Statements.Add(invoke);
         bool methodHasOutParameters = (parameters.Return != null) || (parameters.OutParameters.Count > 0);
         WebCodeGenerator.AddCallbackImplementation(base.CodeTypeDeclaration, callbackName, handlerName, info.handlerArgs, methodHasOutParameters);
         if (base.ExportContext[str8] != null)
         {
             return codeMethod;
         }
         WebCodeGenerator.AddDelegate(base.ExtraCodeClasses, info.handlerType, methodHasOutParameters ? info.handlerArgs : typeof(AsyncCompletedEventArgs).FullName);
         if (methodHasOutParameters)
         {
             int num5 = parameters.OutParameters.Count + parameters.OutCheckSpecifiedCount;
             string[] strArray8 = new string[num5 + 1];
             SoapParameter.GetTypeFullNames(parameters.OutParameters, strArray8, 1, parameters.OutCheckSpecifiedCount, base.ServiceImporter.CodeGenerator);
             strArray8[0] = (parameters.Return == null) ? null : WebCodeGenerator.FullTypeName(parameters.Return, base.ServiceImporter.CodeGenerator);
             string[] strArray9 = new string[num5 + 1];
             SoapParameter.GetNames(parameters.OutParameters, strArray9, 1, parameters.OutCheckSpecifiedCount);
             strArray9[0] = (parameters.Return == null) ? null : "Result";
             base.ExtraCodeClasses.Add(WebCodeGenerator.CreateArgsClass(info.handlerArgs, strArray8, strArray9, base.ServiceImporter.CodeGenerator.Supports(GeneratorSupport.PartialTypes)));
         }
         base.ExportContext[str8] = info;
     }
     return codeMethod;
 }
Example #10
0
        internal static CodeTypeDeclaration AddClass(CodeNamespace codeNamespace, string className, string baseClassName, string[] implementedInterfaceNames, CodeAttributeDeclarationCollection metadata, CodeFlags flags)
        {
            CodeTypeDeclaration codeClass = CreateClass(className, baseClassName, implementedInterfaceNames, metadata, flags);

            codeNamespace.Types.Add(codeClass);
            return(codeClass);
        }
 internal static CodeTypeDeclaration AddClass(CodeNamespace codeNamespace, string className, string baseClassName, string[] implementedInterfaceNames, CodeAttributeDeclarationCollection metadata, CodeFlags flags, bool isPartial)
 {
     CodeTypeDeclaration declaration = CreateClass(className, baseClassName, implementedInterfaceNames, metadata, flags, isPartial);
     codeNamespace.Types.Add(declaration);
     return declaration;
 }
        /// <include file='doc\SoapProtocolImporter.uex' path='docs/doc[@for="SoapProtocolImporter.GenerateMethod"]/*' />
        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        protected override CodeMemberMethod GenerateMethod() {
            Message requestMessage;
            Message responseMessage;
            string[] parameterOrder;
            SoapBodyBinding soapRequestBinding;
            SoapBodyBinding soapResponseBinding;
            MessageBinding requestBinding;
            MessageBinding responseBinding;

            SoapOperationBinding soapOperationBinding = (SoapOperationBinding)this.OperationBinding.Extensions.Find(typeof(SoapOperationBinding));
            if (soapOperationBinding == null) throw OperationBindingSyntaxException(Res.GetString(Res.MissingSoapOperationBinding0));

            SoapBindingStyle soapBindingStyle = soapOperationBinding.Style;
            if (soapBindingStyle == SoapBindingStyle.Default)
                soapBindingStyle = SoapBinding.Style;
            if (soapBindingStyle == SoapBindingStyle.Default)
                soapBindingStyle = SoapBindingStyle.Document;

            parameterOrder = this.Operation.ParameterOrder;

            requestMessage = this.InputMessage;
            requestBinding = this.OperationBinding.Input;
            soapRequestBinding = (SoapBodyBinding)this.OperationBinding.Input.Extensions.Find(typeof(SoapBodyBinding));
            if (soapRequestBinding == null) {
                UnsupportedOperationBindingWarning(Res.GetString(Res.MissingSoapBodyInputBinding0));
                return null;
            }

            if (this.Operation.Messages.Output != null) {
                responseMessage = this.OutputMessage;
                responseBinding = this.OperationBinding.Output;
                soapResponseBinding = (SoapBodyBinding)this.OperationBinding.Output.Extensions.Find(typeof(SoapBodyBinding));
                if (soapResponseBinding == null) {
                    UnsupportedOperationBindingWarning(Res.GetString(Res.MissingSoapBodyOutputBinding0));
                    return null;
                }
            }
            else {
                responseMessage = null;
                responseBinding = null;
                soapResponseBinding = null;
            }

            CodeAttributeDeclarationCollection metadata = new CodeAttributeDeclarationCollection();

            PrepareHeaders(requestBinding);
            if (responseBinding != null) PrepareHeaders(responseBinding);

            string requestMessageName;
            string responseMessageName = null;

            requestMessageName = !String.IsNullOrEmpty(requestBinding.Name) && soapBindingStyle != SoapBindingStyle.Rpc ? requestBinding.Name : this.Operation.Name; // per WSDL 1.1 sec 3.5
            requestMessageName = XmlConvert.DecodeName(requestMessageName);

            if (responseBinding != null) {
                responseMessageName = !String.IsNullOrEmpty(responseBinding.Name) && soapBindingStyle != SoapBindingStyle.Rpc ? responseBinding.Name : this.Operation.Name + "Response"; // per WSDL 1.1 sec 3.5
                responseMessageName = XmlConvert.DecodeName(responseMessageName);
            }

            GenerateExtensionMetadata(metadata);
            GenerateHeaders(metadata, soapRequestBinding.Use, soapBindingStyle == SoapBindingStyle.Rpc, requestBinding, responseBinding);

            MessagePart[] requestParts = GetMessageParts(requestMessage, soapRequestBinding);
            bool hasWrapper;
            if (!CheckMessageStyles(MethodName, requestParts, soapRequestBinding, soapBindingStyle, out hasWrapper))
                return null;

            MessagePart[] responseParts = null;
            if (responseMessage != null) {
                responseParts = GetMessageParts(responseMessage, soapResponseBinding);
                bool responseHasWrapper;
                if (!CheckMessageStyles(MethodName, responseParts, soapResponseBinding, soapBindingStyle, out responseHasWrapper)) 
                    return null;

                // since we're using a potentially inaccurate heuristic to determine whether there's a wrapper,
                // if we disagree about the request and response we should assume there isn't a wrapper.
                if (hasWrapper != responseHasWrapper)
                    hasWrapper = false;
            }

            bool wrapperNamesMatter = (soapBindingStyle != SoapBindingStyle.Rpc && hasWrapper) || (soapRequestBinding.Use == SoapBindingUse.Literal && soapBindingStyle == SoapBindingStyle.Rpc);

            XmlMembersMapping request = ImportMessage(requestMessageName, requestParts, soapRequestBinding, soapBindingStyle, hasWrapper);
            if (request == null) return null;

            XmlMembersMapping response = null;

            if (responseMessage != null) {
                response = ImportMessage(responseMessageName, responseParts, soapResponseBinding, soapBindingStyle, hasWrapper);
                if (response == null) return null;
            }

            string methodName = CodeIdentifier.MakeValid(XmlConvert.DecodeName(this.Operation.Name));
            if (ClassName == methodName) {
                methodName = "Call" + methodName;
            }
            string uniqueMethodName = MethodNames.AddUnique(CodeIdentifier.MakeValid(XmlConvert.DecodeName(methodName)), this.Operation);
            bool differentNames = methodName != uniqueMethodName;

            CodeIdentifiers localIdentifiers = new CodeIdentifiers(false);
            localIdentifiers.AddReserved(uniqueMethodName);

            SoapParameters parameters = new SoapParameters(request, response, parameterOrder, MethodNames);

            foreach (SoapParameter param in parameters.Parameters) {
                if ((param.IsOut || param.IsByRef) && !ServiceImporter.CodeGenerator.Supports(GeneratorSupport.ReferenceParameters)) {
                    UnsupportedOperationWarning(Res.GetString(Res.CodeGenSupportReferenceParameters, ServiceImporter.CodeGenerator.GetType().Name));
                    return null;
                }
                param.name = localIdentifiers.AddUnique(param.name, null);
                if (param.mapping.CheckSpecified)
                    param.specifiedName = localIdentifiers.AddUnique(param.name + "Specified", null);
            }

            if (!(Style == ServiceDescriptionImportStyle.Client) || differentNames) {
                BeginMetadata();
                if (differentNames) AddMetadataProperty("MessageName", uniqueMethodName);
                EndMetadata(metadata, typeof(WebMethodAttribute), null);
            }

            BeginMetadata();
            if (wrapperNamesMatter && request.ElementName.Length > 0 && request.ElementName != uniqueMethodName)
                AddMetadataProperty("RequestElementName", request.ElementName);
            if (request.Namespace != null)
                AddMetadataProperty("RequestNamespace", request.Namespace);
            if (response == null) {
                AddMetadataProperty("OneWay", true);                    
            }
            else {
                if (wrapperNamesMatter && response.ElementName.Length > 0 && response.ElementName != (uniqueMethodName + "Response"))
                    AddMetadataProperty("ResponseElementName", response.ElementName);
                if (response.Namespace != null)
                    AddMetadataProperty("ResponseNamespace", response.Namespace);                                     
            }

            if (soapBindingStyle == SoapBindingStyle.Rpc) {
                if (soapRequestBinding.Use != SoapBindingUse.Encoded) {
                    AddMetadataProperty("Use", new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(SoapBindingUse).FullName), Enum.Format(typeof(SoapBindingUse), soapRequestBinding.Use, "G")));
                }
                EndMetadata(metadata, typeof(SoapRpcMethodAttribute), soapOperationBinding.SoapAction);
            }
            else {
                AddMetadataProperty("Use", new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(SoapBindingUse).FullName), Enum.Format(typeof(SoapBindingUse), soapRequestBinding.Use, "G")));
                AddMetadataProperty("ParameterStyle", new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(SoapParameterStyle).FullName), Enum.Format(typeof(SoapParameterStyle), hasWrapper ? SoapParameterStyle.Wrapped : SoapParameterStyle.Bare, "G")));
                EndMetadata(metadata, typeof(SoapDocumentMethodAttribute), soapOperationBinding.SoapAction);
            }
            IsEncodedBinding = IsEncodedBinding || (soapRequestBinding.Use == SoapBindingUse.Encoded);

            CodeAttributeDeclarationCollection[] paramsMetadata = new CodeAttributeDeclarationCollection[parameters.Parameters.Count + parameters.CheckSpecifiedCount];
            int j = 0;
            CodeAttributeDeclaration ignoreAttribute = new CodeAttributeDeclaration(typeof(XmlIgnoreAttribute).FullName);
            foreach (SoapParameter parameter in parameters.Parameters) {
                paramsMetadata[j] = new CodeAttributeDeclarationCollection();
                if (soapRequestBinding.Use == SoapBindingUse.Encoded)
                    soapExporter.AddMappingMetadata(paramsMetadata[j], parameter.mapping, parameter.name != parameter.mapping.MemberName);
                else {
                    string ns = soapBindingStyle == SoapBindingStyle.Rpc ? parameter.mapping.Namespace : parameter.IsOut ? response.Namespace : request.Namespace;
                    bool forceUseMemberName = parameter.name != parameter.mapping.MemberName;
                    xmlExporter.AddMappingMetadata(paramsMetadata[j], parameter.mapping,  ns, forceUseMemberName);
                    if (parameter.mapping.CheckSpecified) {
                        j++;
                        paramsMetadata[j] = new CodeAttributeDeclarationCollection();
                        xmlExporter.AddMappingMetadata(paramsMetadata[j], parameter.mapping, ns, parameter.specifiedName != parameter.mapping.MemberName + "Specified");
                        paramsMetadata[j].Add(ignoreAttribute);
                    }
                }
                if (paramsMetadata[j].Count > 0  && !ServiceImporter.CodeGenerator.Supports(GeneratorSupport.ParameterAttributes)) {
                    UnsupportedOperationWarning(Res.GetString(Res.CodeGenSupportParameterAttributes, ServiceImporter.CodeGenerator.GetType().Name));
                    return null;
                }
                j++;
            }

            CodeFlags[] parameterFlags = SoapParameter.GetCodeFlags(parameters.Parameters, parameters.CheckSpecifiedCount);
            string[] parameterTypes = SoapParameter.GetTypeFullNames(parameters.Parameters, parameters.CheckSpecifiedCount, ServiceImporter.CodeGenerator);
            string returnType = parameters.Return == null ? typeof(void).FullName : WebCodeGenerator.FullTypeName(parameters.Return, ServiceImporter.CodeGenerator);

            CodeMemberMethod mainCodeMethod = WebCodeGenerator.AddMethod(this.CodeTypeDeclaration, methodName, 
                                        parameterFlags, 
                                        parameterTypes,
                                        SoapParameter.GetNames(parameters.Parameters, parameters.CheckSpecifiedCount),
                                        paramsMetadata,
                                        returnType,
                                        metadata, 
                                        CodeFlags.IsPublic | (Style == ServiceDescriptionImportStyle.Client ? 0 : CodeFlags.IsAbstract));

            mainCodeMethod.Comments.Add(new CodeCommentStatement(Res.GetString(Res.CodeRemarks), true));

            if (parameters.Return != null) {
                if (soapRequestBinding.Use == SoapBindingUse.Encoded)
                    soapExporter.AddMappingMetadata(mainCodeMethod.ReturnTypeCustomAttributes, parameters.Return, parameters.Return.ElementName != uniqueMethodName + "Result");
                else
                    xmlExporter.AddMappingMetadata(mainCodeMethod.ReturnTypeCustomAttributes, parameters.Return, response.Namespace, parameters.Return.ElementName != uniqueMethodName + "Result");

                if (mainCodeMethod.ReturnTypeCustomAttributes.Count != 0 && !ServiceImporter.CodeGenerator.Supports(GeneratorSupport.ReturnTypeAttributes)) {
                    UnsupportedOperationWarning(Res.GetString(Res.CodeGenSupportReturnTypeAttributes, ServiceImporter.CodeGenerator.GetType().Name));
                    return null;
                }
            }

            string resultsName = localIdentifiers.MakeUnique("results");

            if (Style == ServiceDescriptionImportStyle.Client) {
                bool oldAsync = (ServiceImporter.CodeGenerationOptions & CodeGenerationOptions.GenerateOldAsync) != 0;
                bool newAsync = (ServiceImporter.CodeGenerationOptions & CodeGenerationOptions.GenerateNewAsync) != 0 && 
                    ServiceImporter.CodeGenerator.Supports(GeneratorSupport.DeclareEvents) && 
                    ServiceImporter.CodeGenerator.Supports(GeneratorSupport.DeclareDelegates);
                CodeExpression[] invokeParams = new CodeExpression[2];
                CreateInvokeParams(invokeParams, uniqueMethodName, parameters.InParameters, parameters.InCheckSpecifiedCount);
                CodeMethodInvokeExpression invoke = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "Invoke", invokeParams);
                WriteReturnMappings(mainCodeMethod, invoke, parameters, resultsName);

                if (oldAsync) {
                    int inCount = parameters.InParameters.Count + parameters.InCheckSpecifiedCount;

                    string[] asyncParameterTypes = new string[inCount + 2];
                    SoapParameter.GetTypeFullNames(parameters.InParameters, asyncParameterTypes, 0, parameters.InCheckSpecifiedCount, ServiceImporter.CodeGenerator);
                    asyncParameterTypes[inCount] = typeof(AsyncCallback).FullName;
                    asyncParameterTypes[inCount + 1] = typeof(object).FullName;

                    string[] asyncParameterNames = new string[inCount + 2];
                    SoapParameter.GetNames(parameters.InParameters, asyncParameterNames, 0, parameters.InCheckSpecifiedCount);
                    asyncParameterNames[inCount] = "callback";
                    asyncParameterNames[inCount + 1] = "asyncState";

                    CodeFlags[] asyncParameterFlags = new CodeFlags[inCount + 2];

                    CodeMemberMethod beginCodeMethod = WebCodeGenerator.AddMethod(this.CodeTypeDeclaration, "Begin" + uniqueMethodName, 
                        asyncParameterFlags,
                        asyncParameterTypes, 
                        asyncParameterNames, 
                        typeof(IAsyncResult).FullName, 
                        null, 
                        CodeFlags.IsPublic);

                    beginCodeMethod.Comments.Add(new CodeCommentStatement(Res.GetString(Res.CodeRemarks), true));

                    invokeParams = new CodeExpression[4];
                    CreateInvokeParams(invokeParams, uniqueMethodName, parameters.InParameters, parameters.InCheckSpecifiedCount);
                    invokeParams[2] = new CodeArgumentReferenceExpression("callback");
                    invokeParams[3] = new CodeArgumentReferenceExpression("asyncState");

                    invoke = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "BeginInvoke", invokeParams);
                    beginCodeMethod.Statements.Add(new CodeMethodReturnStatement(invoke));

                    int outCount = parameters.OutParameters.Count + parameters.OutCheckSpecifiedCount;
                    string[] asyncReturnTypes = new string[outCount + 1];
                    SoapParameter.GetTypeFullNames(parameters.OutParameters, asyncReturnTypes, 1, parameters.OutCheckSpecifiedCount, ServiceImporter.CodeGenerator);
                    asyncReturnTypes[0] = typeof(IAsyncResult).FullName;

                    string[] asyncReturnNames = new string[outCount + 1];
                    SoapParameter.GetNames(parameters.OutParameters, asyncReturnNames, 1, parameters.OutCheckSpecifiedCount);
                    asyncReturnNames[0] = "asyncResult";

                    CodeFlags[] asyncReturnFlags = new CodeFlags[outCount + 1];
                    for (int i = 0; i < outCount; i++)
                        asyncReturnFlags[i + 1] = CodeFlags.IsOut;

                    CodeMemberMethod codeMethod = WebCodeGenerator.AddMethod(this.CodeTypeDeclaration, "End" + uniqueMethodName, 
                        asyncReturnFlags, 
                        asyncReturnTypes, 
                        asyncReturnNames, 
                        parameters.Return == null ? typeof(void).FullName : WebCodeGenerator.FullTypeName(parameters.Return, ServiceImporter.CodeGenerator), 
                        null, 
                        CodeFlags.IsPublic);

                    codeMethod.Comments.Add(new CodeCommentStatement(Res.GetString(Res.CodeRemarks), true));

                    CodeExpression invokeParam = new CodeArgumentReferenceExpression("asyncResult");
                    invoke = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "EndInvoke", new CodeExpression[] { invokeParam });

                    WriteReturnMappings(codeMethod, invoke, parameters, resultsName);
                }

                // new RAD Async pattern
                if (newAsync) {
                    string methodKey = MethodSignature(uniqueMethodName, returnType, parameterFlags, parameterTypes);
                    DelegateInfo delegateInfo = (DelegateInfo)ExportContext[methodKey];
                    if (delegateInfo == null) {
                        string handlerType = ClassNames.AddUnique(uniqueMethodName + "CompletedEventHandler", uniqueMethodName);
                        string handlerArgs = ClassNames.AddUnique(uniqueMethodName + "CompletedEventArgs", uniqueMethodName);
                        delegateInfo = new DelegateInfo(handlerType, handlerArgs);
                    }
                    string handlerName = MethodNames.AddUnique(uniqueMethodName + "Completed", uniqueMethodName);
                    string asyncName = MethodNames.AddUnique(uniqueMethodName + "Async", uniqueMethodName);
                    string callbackMember = MethodNames.AddUnique(uniqueMethodName + "OperationCompleted", uniqueMethodName);
                    string callbackName = MethodNames.AddUnique("On" + uniqueMethodName + "OperationCompleted", uniqueMethodName);

                    // public event xxxCompletedEventHandler xxxCompleted;
                    WebCodeGenerator.AddEvent(this.CodeTypeDeclaration.Members, delegateInfo.handlerType, handlerName);

                    // private SendOrPostCallback xxxOperationCompleted;
                    WebCodeGenerator.AddCallbackDeclaration(this.CodeTypeDeclaration.Members, callbackMember);

                    // create the pair of xxxAsync methods
                    string[] inParamNames = SoapParameter.GetNames(parameters.InParameters, parameters.InCheckSpecifiedCount);
                    string userState = UniqueName("userState", inParamNames);
                    CodeMemberMethod asyncCodeMethod = WebCodeGenerator.AddAsyncMethod(this.CodeTypeDeclaration, asyncName, 
                        SoapParameter.GetTypeFullNames(parameters.InParameters, parameters.InCheckSpecifiedCount, ServiceImporter.CodeGenerator), inParamNames, callbackMember, callbackName, userState);

                    // Generate InvokeAsync call
                    invokeParams = new CodeExpression[4];
                    CreateInvokeParams(invokeParams, uniqueMethodName, parameters.InParameters, parameters.InCheckSpecifiedCount);
                    invokeParams[2] = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), callbackMember);
                    invokeParams[3] = new CodeArgumentReferenceExpression(userState);
                        
                    invoke = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "InvokeAsync", invokeParams);
                    asyncCodeMethod.Statements.Add(invoke);

                    //  private void On_xxx_OperationCompleted(object arg) {..}
                    bool methodHasOutParameters = parameters.Return != null || parameters.OutParameters.Count > 0;
                    WebCodeGenerator.AddCallbackImplementation(this.CodeTypeDeclaration, callbackName, handlerName, delegateInfo.handlerArgs, methodHasOutParameters);

                    if (ExportContext[methodKey] == null) {
                        // public delegate void xxxCompletedEventHandler(object sender, System.ComponentModel.AsyncCompletedEventArgs args);
                        WebCodeGenerator.AddDelegate(ExtraCodeClasses, delegateInfo.handlerType, methodHasOutParameters ? delegateInfo.handlerArgs : typeof(AsyncCompletedEventArgs).FullName);
                        
                        // Create strongly-typed Args class
                        if (methodHasOutParameters) {
                            int outCount = parameters.OutParameters.Count + parameters.OutCheckSpecifiedCount;
                            string[] asyncReturnTypes = new string[outCount + 1];
                            SoapParameter.GetTypeFullNames(parameters.OutParameters, asyncReturnTypes, 1, parameters.OutCheckSpecifiedCount, ServiceImporter.CodeGenerator);
                            asyncReturnTypes[0] = parameters.Return == null ? null : WebCodeGenerator.FullTypeName(parameters.Return, ServiceImporter.CodeGenerator);

                            string[] asyncReturnNames = new string[outCount + 1];
                            SoapParameter.GetNames(parameters.OutParameters, asyncReturnNames, 1, parameters.OutCheckSpecifiedCount);
                            asyncReturnNames[0] = parameters.Return == null ? null : "Result";
                            ExtraCodeClasses.Add(WebCodeGenerator.CreateArgsClass(delegateInfo.handlerArgs, asyncReturnTypes, asyncReturnNames, ServiceImporter.CodeGenerator.Supports(GeneratorSupport.PartialTypes)));
                        }
                        ExportContext[methodKey] = delegateInfo;
                    }
                }
            }
            return mainCodeMethod;
        }
 internal static CodeTypeDeclaration CreateClass(string className, string baseClassName, string[] implementedInterfaceNames, CodeAttributeDeclarationCollection metadata, CodeFlags flags, bool isPartial)
 {
     CodeTypeDeclaration declaration = new CodeTypeDeclaration(className);
     if ((baseClassName != null) && (baseClassName.Length > 0))
     {
         declaration.BaseTypes.Add(baseClassName);
     }
     foreach (string str in implementedInterfaceNames)
     {
         declaration.BaseTypes.Add(str);
     }
     declaration.IsStruct = (flags & CodeFlags.IsStruct) != ((CodeFlags) 0);
     if ((flags & CodeFlags.IsPublic) != ((CodeFlags) 0))
     {
         declaration.TypeAttributes |= TypeAttributes.Public;
     }
     else
     {
         declaration.TypeAttributes &= ~TypeAttributes.Public;
     }
     if ((flags & CodeFlags.IsAbstract) != ((CodeFlags) 0))
     {
         declaration.TypeAttributes |= TypeAttributes.Abstract;
     }
     else
     {
         declaration.TypeAttributes &= ~TypeAttributes.Abstract;
     }
     if ((flags & CodeFlags.IsInterface) != ((CodeFlags) 0))
     {
         declaration.IsInterface = true;
     }
     else
     {
         declaration.IsPartial = isPartial;
     }
     declaration.CustomAttributes = metadata;
     declaration.CustomAttributes.Add(GeneratedCodeAttribute);
     return declaration;
 }
        internal static CodeConstructor AddConstructor(CodeTypeDeclaration codeClass, string[] parameterTypeNames, string[] parameterNames, CodeAttributeDeclarationCollection metadata, CodeFlags flags) {
            CodeConstructor ctor = new CodeConstructor();

            if ((flags & CodeFlags.IsPublic) != 0)
                ctor.Attributes = (ctor.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            if ((flags & CodeFlags.IsAbstract) != 0)
                ctor.Attributes |= MemberAttributes.Abstract;

            ctor.CustomAttributes = metadata;

            Debug.Assert(parameterTypeNames.Length == parameterNames.Length, "invalid set of parameters");
            for (int i = 0; i < parameterTypeNames.Length; i++) {
                CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression(parameterTypeNames[i], parameterNames[i]);
                ctor.Parameters.Add(param);
            }
            codeClass.Members.Add(ctor);
            return ctor;
        }
 internal static CodeMemberMethod AddMethod(CodeTypeDeclaration codeClass, string methodName, CodeFlags[] parameterFlags, string[] parameterTypeNames, string[] parameterNames, CodeAttributeDeclarationCollection[] parameterAttributes, string returnTypeName, CodeAttributeDeclarationCollection metadata, CodeFlags flags)
 {
     CodeMemberMethod method = new CodeMemberMethod {
         Name = methodName,
         ReturnType = new CodeTypeReference(returnTypeName),
         CustomAttributes = metadata
     };
     if ((flags & CodeFlags.IsPublic) != ((CodeFlags) 0))
     {
         method.Attributes = (method.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
     }
     if ((flags & CodeFlags.IsAbstract) != ((CodeFlags) 0))
     {
         method.Attributes = (method.Attributes & ~MemberAttributes.ScopeMask) | MemberAttributes.Abstract;
     }
     if ((flags & CodeFlags.IsNew) != ((CodeFlags) 0))
     {
         method.Attributes = (method.Attributes & ~MemberAttributes.VTableMask) | MemberAttributes.New;
     }
     for (int i = 0; i < parameterNames.Length; i++)
     {
         CodeParameterDeclarationExpression expression = new CodeParameterDeclarationExpression(parameterTypeNames[i], parameterNames[i]);
         if ((parameterFlags[i] & CodeFlags.IsByRef) != ((CodeFlags) 0))
         {
             expression.Direction = FieldDirection.Ref;
         }
         else if ((parameterFlags[i] & CodeFlags.IsOut) != ((CodeFlags) 0))
         {
             expression.Direction = FieldDirection.Out;
         }
         if (i < parameterAttributes.Length)
         {
             expression.CustomAttributes = parameterAttributes[i];
         }
         method.Parameters.Add(expression);
     }
     codeClass.Members.Add(method);
     return method;
 }
 internal static CodeMemberMethod AddMethod(CodeTypeDeclaration codeClass, string methodName, CodeFlags[] parameterFlags, string[] parameterTypeNames, string[] parameterNames, string returnTypeName, CodeAttributeDeclarationCollection metadata, CodeFlags flags)
 {
     return AddMethod(codeClass, methodName, parameterFlags, parameterTypeNames, parameterNames, new CodeAttributeDeclarationCollection[0], returnTypeName, metadata, flags);
 }
 internal static CodeTypeMember AddMember(CodeTypeDeclaration codeClass, string typeName, string memberName, CodeExpression initializer, CodeAttributeDeclarationCollection metadata, CodeFlags flags, CodeGenerationOptions options)
 {
     CodeTypeMember member;
     bool flag = (options & CodeGenerationOptions.GenerateProperties) != CodeGenerationOptions.None;
     string name = flag ? MakeFieldName(memberName) : memberName;
     CodeMemberField field = new CodeMemberField(typeName, name) {
         InitExpression = initializer
     };
     if (flag)
     {
         codeClass.Members.Add(field);
         member = CreatePropertyDeclaration(field, memberName, typeName);
     }
     else
     {
         member = field;
     }
     member.CustomAttributes = metadata;
     if ((flags & CodeFlags.IsPublic) != ((CodeFlags) 0))
     {
         member.Attributes = (field.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
     }
     codeClass.Members.Add(member);
     return member;
 }
        internal static CodeMemberMethod AddMethod(CodeTypeDeclaration codeClass, string methodName, CodeFlags[] parameterFlags, string[] parameterTypeNames, string[] parameterNames, CodeAttributeDeclarationCollection[] parameterAttributes, string returnTypeName, CodeAttributeDeclarationCollection metadata, CodeFlags flags)
        {
            CodeMemberMethod method = new CodeMemberMethod {
                Name             = methodName,
                ReturnType       = new CodeTypeReference(returnTypeName),
                CustomAttributes = metadata
            };

            if ((flags & CodeFlags.IsPublic) != ((CodeFlags)0))
            {
                method.Attributes = (method.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            }
            if ((flags & CodeFlags.IsAbstract) != ((CodeFlags)0))
            {
                method.Attributes = (method.Attributes & ~MemberAttributes.ScopeMask) | MemberAttributes.Abstract;
            }
            if ((flags & CodeFlags.IsNew) != ((CodeFlags)0))
            {
                method.Attributes = (method.Attributes & ~MemberAttributes.VTableMask) | MemberAttributes.New;
            }
            for (int i = 0; i < parameterNames.Length; i++)
            {
                CodeParameterDeclarationExpression expression = new CodeParameterDeclarationExpression(parameterTypeNames[i], parameterNames[i]);
                if ((parameterFlags[i] & CodeFlags.IsByRef) != ((CodeFlags)0))
                {
                    expression.Direction = FieldDirection.Ref;
                }
                else if ((parameterFlags[i] & CodeFlags.IsOut) != ((CodeFlags)0))
                {
                    expression.Direction = FieldDirection.Out;
                }
                if (i < parameterAttributes.Length)
                {
                    expression.CustomAttributes = parameterAttributes[i];
                }
                method.Parameters.Add(expression);
            }
            codeClass.Members.Add(method);
            return(method);
        }
 internal static CodeConstructor AddConstructor(CodeTypeDeclaration codeClass, string[] parameterTypeNames, string[] parameterNames, CodeAttributeDeclarationCollection metadata, CodeFlags flags)
 {
     CodeConstructor constructor = new CodeConstructor();
     if ((flags & CodeFlags.IsPublic) != ((CodeFlags) 0))
     {
         constructor.Attributes = (constructor.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
     }
     if ((flags & CodeFlags.IsAbstract) != ((CodeFlags) 0))
     {
         constructor.Attributes |= MemberAttributes.Abstract;
     }
     constructor.CustomAttributes = metadata;
     for (int i = 0; i < parameterTypeNames.Length; i++)
     {
         CodeParameterDeclarationExpression expression = new CodeParameterDeclarationExpression(parameterTypeNames[i], parameterNames[i]);
         constructor.Parameters.Add(expression);
     }
     codeClass.Members.Add(constructor);
     return constructor;
 }
        internal static CodeTypeDeclaration CreateClass(string className, string baseClassName, string[] implementedInterfaceNames, CodeAttributeDeclarationCollection metadata, CodeFlags flags, bool isPartial)
        {
            CodeTypeDeclaration declaration = new CodeTypeDeclaration(className);

            if ((baseClassName != null) && (baseClassName.Length > 0))
            {
                declaration.BaseTypes.Add(baseClassName);
            }
            foreach (string str in implementedInterfaceNames)
            {
                declaration.BaseTypes.Add(str);
            }
            declaration.IsStruct = (flags & CodeFlags.IsStruct) != ((CodeFlags)0);
            if ((flags & CodeFlags.IsPublic) != ((CodeFlags)0))
            {
                declaration.TypeAttributes |= TypeAttributes.Public;
            }
            else
            {
                declaration.TypeAttributes &= ~TypeAttributes.Public;
            }
            if ((flags & CodeFlags.IsAbstract) != ((CodeFlags)0))
            {
                declaration.TypeAttributes |= TypeAttributes.Abstract;
            }
            else
            {
                declaration.TypeAttributes &= ~TypeAttributes.Abstract;
            }
            if ((flags & CodeFlags.IsInterface) != ((CodeFlags)0))
            {
                declaration.IsInterface = true;
            }
            else
            {
                declaration.IsPartial = isPartial;
            }
            declaration.CustomAttributes = metadata;
            declaration.CustomAttributes.Add(GeneratedCodeAttribute);
            return(declaration);
        }
Example #21
0
 public Code(CodeType type, ushort value)
 {
     this.Type = type;
     this.Value = value;
     this.Flags = CodeFlags.IsLiteral;
 }
        internal static CodeConstructor AddConstructor(CodeTypeDeclaration codeClass, string[] parameterTypeNames, string[] parameterNames, CodeAttributeDeclarationCollection metadata, CodeFlags flags)
        {
            CodeConstructor constructor = new CodeConstructor();

            if ((flags & CodeFlags.IsPublic) != ((CodeFlags)0))
            {
                constructor.Attributes = (constructor.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            }
            if ((flags & CodeFlags.IsAbstract) != ((CodeFlags)0))
            {
                constructor.Attributes |= MemberAttributes.Abstract;
            }
            constructor.CustomAttributes = metadata;
            for (int i = 0; i < parameterTypeNames.Length; i++)
            {
                CodeParameterDeclarationExpression expression = new CodeParameterDeclarationExpression(parameterTypeNames[i], parameterNames[i]);
                constructor.Parameters.Add(expression);
            }
            codeClass.Members.Add(constructor);
            return(constructor);
        }
Example #23
0
 public Code(Instruction instr)
 {
     this.Type = CodeType.Instruction;
     this.Value = instr.Value;
     this.Flags = CodeFlags.None;
 }
Example #24
0
        internal static CodeMemberMethod AddMethod(CodeTypeDeclaration codeClass, string methodName,
                                                   CodeFlags[] parameterFlags, string[] parameterTypeNames, string[] parameterNames,
                                                   CodeAttributeDeclarationCollection[] parameterAttributes, string returnTypeName, CodeAttributeDeclarationCollection metadata, CodeFlags flags)
        {
            CodeMemberMethod method = new CodeMemberMethod();

            method.Name             = methodName;
            method.ReturnType       = new CodeTypeReference(returnTypeName);
            method.CustomAttributes = metadata;

            if ((flags & CodeFlags.IsPublic) != 0)
            {
                method.Attributes = (method.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            }
            if ((flags & CodeFlags.IsAbstract) != 0)
            {
                method.Attributes = (method.Attributes & ~MemberAttributes.ScopeMask) | MemberAttributes.Abstract;
            }

            Debug.Assert(parameterFlags.Length == parameterTypeNames.Length && parameterTypeNames.Length == parameterNames.Length, "invalid set of parameters");
            for (int i = 0; i < parameterNames.Length; i++)
            {
                CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression(parameterTypeNames[i], parameterNames[i]);

                if ((parameterFlags[i] & CodeFlags.IsByRef) != 0)
                {
                    param.Direction = FieldDirection.Ref;
                }
                else if ((parameterFlags[i] & CodeFlags.IsOut) != 0)
                {
                    param.Direction = FieldDirection.Out;
                }

                if (i < parameterAttributes.Length)
                {
                    param.CustomAttributes = parameterAttributes[i];
                }
                method.Parameters.Add(param);
            }
            codeClass.Members.Add(method);
            return(method);
        }
Example #25
0
 public Code(ushort value)
 {
     this.Type = CodeType.Literal;
     this.Value = value;
     this.Flags = CodeFlags.None;
 }
Example #26
0
        internal static CodeTypeDeclaration CreateClass(string className, string baseClassName, string[] implementedInterfaceNames, CodeAttributeDeclarationCollection metadata, CodeFlags flags)
        {
            CodeTypeDeclaration codeClass = new CodeTypeDeclaration(className);

            if (baseClassName != null && baseClassName.Length > 0)
            {
                codeClass.BaseTypes.Add(baseClassName);
            }
            foreach (string interfaceName in implementedInterfaceNames)
            {
                codeClass.BaseTypes.Add(interfaceName);
            }
            codeClass.IsStruct = (flags & CodeFlags.IsStruct) != 0;
            if ((flags & CodeFlags.IsPublic) != 0)
            {
                codeClass.TypeAttributes |= TypeAttributes.Public;
            }
            else
            {
                codeClass.TypeAttributes &= ~TypeAttributes.Public;
            }
            if ((flags & CodeFlags.IsAbstract) != 0)
            {
                codeClass.TypeAttributes |= TypeAttributes.Abstract;
            }
            else
            {
                codeClass.TypeAttributes &= ~TypeAttributes.Abstract;
            }

            codeClass.CustomAttributes = metadata;
            return(codeClass);
        }
Example #27
0
 public static bool HasFlagFast(this CodeFlags value, CodeFlags flag) => (value & flag) != 0;
Example #28
0
        internal static CodeConstructor AddConstructor(CodeTypeDeclaration codeClass, string[] parameterTypeNames, string[] parameterNames, CodeAttributeDeclarationCollection metadata, CodeFlags flags)
        {
            CodeConstructor ctor = new CodeConstructor();

            if ((flags & CodeFlags.IsPublic) != 0)
            {
                ctor.Attributes = (ctor.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            }
            if ((flags & CodeFlags.IsAbstract) != 0)
            {
                ctor.Attributes = MemberAttributes.Abstract;
            }

            ctor.CustomAttributes = metadata;

            Debug.Assert(parameterTypeNames.Length == parameterNames.Length, "invalid set of parameters");
            for (int i = 0; i < parameterTypeNames.Length; i++)
            {
                CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression(parameterTypeNames[i], parameterNames[i]);
                ctor.Parameters.Add(param);
            }
            codeClass.Members.Add(ctor);
            return(ctor);
        }
Example #29
0
 internal static CodeFlags[] GetCodeFlags(IList parameters, int specifiedCount)
 {
     CodeFlags[] codeFlags = new CodeFlags[parameters.Count + specifiedCount];
     GetCodeFlags(parameters, codeFlags, 0, specifiedCount);
     return(codeFlags);
 }
        internal static string MethodSignature(string methodName, string returnType, CodeFlags[] parameterFlags, string[] parameterTypes) {
            Debug.Assert(parameterFlags.Length == parameterTypes.Length, "parameterFlags.Length !=  parameterTypes.Length");
            StringBuilder sb = new StringBuilder();
            sb.Append(returnType);
            sb.Append(" ");
            sb.Append(methodName);
            sb.Append(" (");
            for (int i = 0; i < parameterTypes.Length; i++) {
                if ((parameterFlags[i] & CodeFlags.IsByRef) != 0)
                    sb.Append("ref ");
                else if ((parameterFlags[i] & CodeFlags.IsOut) != 0)
                    sb.Append("out ");

                sb.Append(parameterTypes[i]);
                if (i > 0)
                    sb.Append(",");
            }
            sb.Append(")");
            return sb.ToString();
        }
        internal static CodeTypeMember AddMember(CodeTypeDeclaration codeClass, string typeName, string memberName, CodeExpression initializer, CodeAttributeDeclarationCollection metadata, CodeFlags flags, CodeGenerationOptions options)
        {
            CodeTypeMember  member;
            bool            flag  = (options & CodeGenerationOptions.GenerateProperties) != CodeGenerationOptions.None;
            string          name  = flag ? MakeFieldName(memberName) : memberName;
            CodeMemberField field = new CodeMemberField(typeName, name)
            {
                InitExpression = initializer
            };

            if (flag)
            {
                codeClass.Members.Add(field);
                member = CreatePropertyDeclaration(field, memberName, typeName);
            }
            else
            {
                member = field;
            }
            member.CustomAttributes = metadata;
            if ((flags & CodeFlags.IsPublic) != ((CodeFlags)0))
            {
                member.Attributes = (field.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            }
            codeClass.Members.Add(member);
            return(member);
        }
Example #32
0
 public Code(Label value)
 {
     this.Type = CodeType.Label;
     this.Value = value.Index;
     this.Flags = CodeFlags.None;
 }
        internal static CodeMemberMethod AddMethod(CodeTypeDeclaration codeClass, string methodName, 
            CodeFlags[] parameterFlags, string[] parameterTypeNames, string[] parameterNames, 
            CodeAttributeDeclarationCollection[] parameterAttributes, string returnTypeName, CodeAttributeDeclarationCollection metadata, CodeFlags flags) {

            CodeMemberMethod method = new CodeMemberMethod();
            method.Name = methodName;
            method.ReturnType = new CodeTypeReference(returnTypeName);
            method.CustomAttributes = metadata;

            if ((flags & CodeFlags.IsPublic) != 0)
                method.Attributes = (method.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
            if ((flags & CodeFlags.IsAbstract) != 0)
                method.Attributes = (method.Attributes & ~MemberAttributes.ScopeMask) | MemberAttributes.Abstract;

            if ((flags & CodeFlags.IsNew) != 0)
                method.Attributes = (method.Attributes & ~MemberAttributes.VTableMask) | MemberAttributes.New;

            Debug.Assert(parameterFlags.Length == parameterTypeNames.Length && parameterTypeNames.Length == parameterNames.Length, "invalid set of parameters");
            for (int i = 0; i < parameterNames.Length; i++) {
                CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression(parameterTypeNames[i], parameterNames[i]);
                
                if ((parameterFlags[i] & CodeFlags.IsByRef) != 0)
                    param.Direction = FieldDirection.Ref;
                else if ((parameterFlags[i] & CodeFlags.IsOut) != 0)
                    param.Direction = FieldDirection.Out;

                if (i < parameterAttributes.Length) {
                    param.CustomAttributes = parameterAttributes[i];
                }
                method.Parameters.Add(param);
            }
            codeClass.Members.Add(method);
            return method;
        }
Example #34
0
 public void Negate()
 {
     this.Flags ^= CodeFlags.Negate;
 }
 internal static string MethodSignature(string methodName, string returnType, CodeFlags[] parameterFlags, string[] parameterTypes)
 {
     StringBuilder builder = new StringBuilder();
     builder.Append(returnType);
     builder.Append(" ");
     builder.Append(methodName);
     builder.Append(" (");
     for (int i = 0; i < parameterTypes.Length; i++)
     {
         if ((parameterFlags[i] & CodeFlags.IsByRef) != ((CodeFlags) 0))
         {
             builder.Append("ref ");
         }
         else if ((parameterFlags[i] & CodeFlags.IsOut) != ((CodeFlags) 0))
         {
             builder.Append("out ");
         }
         builder.Append(parameterTypes[i]);
         if (i > 0)
         {
             builder.Append(",");
         }
     }
     builder.Append(")");
     return builder.ToString();
 }