Exemplo n.º 1
0
        /// <summary>
        /// Build all proxies
        /// </summary>
        public void Build()
        {
            // Collect all types that are ServiceContract interfaces
            var serviceContractIntfTypes = assemblies.SelectMany(x => x.MainModule.GetTypes()).Where(x => x.IsInterface && x.HasServiceContractAttribute());
            var proxyBuilders = serviceContractIntfTypes.Select(x => new ProxyClassBuilder(x)).ToList();

            // Build structures
            var context = new ProxySerializationContext();
            proxyBuilders.ForEach(x => x.Create(context));
            context.Create(context);

            // Generate code
            var code = new StringBuilder(128 * 1024);
            using (var codeWriter = new StringWriter(code))
            {
                var codeGenerator = new CodeGenerator();
                proxyBuilders.ForEach(x => x.Generate(codeGenerator));
                context.Generate(codeGenerator);
                codeGenerator.Generate(codeWriter);
            }

            // Load existing C# code
            var existingCode = File.Exists(generatedSourcePath) ? File.ReadAllText(generatedSourcePath) : string.Empty;
            var generatedCode = code.ToString();
            if (existingCode != generatedCode)
            {
                // Ensure directory exists
                Directory.CreateDirectory(Path.GetDirectoryName(generatedSourcePath));

                // Write C#
                File.WriteAllText(generatedSourcePath, generatedCode);
            }
        }
        /// <summary>
        /// Create the entire proxy method as C# code.
        /// </summary>
        public override void Generate(CodeTypeDeclaration declaringType, CodeGenerator generator)
        {
            // Prepare method
            var methodDecl = CreateProxyMethodDeclaration();

            // Add code
            methodDecl.Statements.Add(new CodeThrowExceptionStatement(new CodeObjectCreateExpression(typeof (NotSupportedException))));

            // Add method to declaring type
            declaringType.Members.Add(methodDecl);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Create the entire serialization method as C# code.
 /// </summary>
 public void Generate(CodeTypeDeclaration declaringType, CodeGenerator generator)
 {
     // Add generate method to declaring type
     if (type.FullName == "System.IO.Stream")
     {
         declaringType.Members.Add(GeneratePassthroughStream());
     }
     else if (IsComplexType() || IsEnum())
     {
         declaringType.Members.AddRange(GenerateComplexType());
     }
     //else: this should be a known value type, which are implemented in the base-class.
 }
Exemplo n.º 4
0
        /// <summary>
        /// Create the entire (de)serialization code as C# code.
        /// </summary>
        public void Generate(CodeGenerator generator)
        {
            // Prepare type
            var typeDecl = new CodeTypeDeclaration(SerializationHelperTypeName);
            typeDecl.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            typeDecl.BaseTypes.Add("Dot42.Internal.SerializationHelperBase");

            // Generate method code
            typeSerializerBuilders.Values.ForEach(x => x.Generate(typeDecl, generator));
            typeDeserializerBuilders.Values.ForEach(x => x.Generate(typeDecl, generator));

            // Add type
            generator.Add(typeDecl);
        }
        /// <summary>
        /// Create the entire proxy method as C# code.
        /// </summary>
        public override void Generate(CodeTypeDeclaration declaringType, CodeGenerator generator)
        {
            // Prepare method
            var methodDecl = CreateProxyMethodDeclaration();

            // Add code
            methodDecl.Statements.Add(new CodeVariableDeclarationStatement(new CodeTypeReference(typeof(UriTemplate)), "uriTemplate", new CodeObjectCreateExpression(typeof(UriTemplate),
                                                                           new CodePrimitiveExpression(uriTemplate))));

            methodDecl.Statements.Add(new CodeVariableDeclarationStatement(new CodeTypeReference(typeof(NameValueCollection)), "nameValues", new CodeObjectCreateExpression(typeof(NameValueCollection))));

            foreach (var inputArgument in inputArguments)
            {
                methodDecl.Statements.Add(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("nameValues"), "Add", new CodePrimitiveExpression(inputArgument), new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(inputArgument), "ToString()")));
            }

            methodDecl.Statements.Add(new CodeVariableDeclarationStatement(new CodeTypeReference(typeof(Uri)), "uri", new CodeMethodInvokeExpression( new CodeTypeReferenceExpression("uriTemplate"), "BindByName", new CodeTypeReferenceExpression("baseAddress"), new CodeTypeReferenceExpression("nameValues"))));

            methodDecl.Statements.Add(new CodeVariableDeclarationStatement(new CodeTypeReference(typeof(Stream)), "inStream", new CodeSnippetExpression("null")));
            methodDecl.Statements.Add(new CodeVariableDeclarationStatement(new CodeTypeReference(typeof(Stream)), "outStream", new CodeSnippetExpression("null")));

            var codeTryCatchFinallyStatement = new CodeTryCatchFinallyStatement();
            methodDecl.Statements.Add(codeTryCatchFinallyStatement);

            if (typeSerializerBuilder!=null)
            {
                var arrayPostfix = serializerTypeIsArray ? "Array" : "";
                var serializeCode = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("SerializationHelper"), typeSerializerBuilder.MethodName + arrayPostfix,
                    new CodeTypeReferenceExpression(toSerializeArgument), new CodeTypeReferenceExpression("SerializationHelper.SerializationFormat." + serializationFormat.ToString()), new CodeTypeReferenceExpression("System.ServiceModel.Web.WebMessageFormat." + requestFormat.ToString()));
                codeTryCatchFinallyStatement.TryStatements.Add(new CodeAssignStatement(new CodeTypeReferenceExpression("inStream"), serializeCode));
            }

            var webCode = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("WebHelper"), "WebInvoke",
                new CodeTypeReferenceExpression("uri"), new CodeTypeReferenceExpression("WebHelper.Verb." + webMode.ToString()), new CodeTypeReferenceExpression("inStream"), new CodeTypeReferenceExpression("System.ServiceModel.Web.WebMessageFormat." + requestFormat.ToString()), new CodeSnippetExpression(typeDeserializerBuilder != null ? "true" : "false"));
            codeTryCatchFinallyStatement.TryStatements.Add(new CodeAssignStatement(new CodeTypeReferenceExpression("outStream"), webCode));

            if (typeDeserializerBuilder != null)
            {
                var arrayPostfix = deserializerTypeIsArray ? "Array" : "";
                codeTryCatchFinallyStatement.TryStatements.Add(new CodeMethodReturnStatement(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("SerializationHelper"), typeDeserializerBuilder.MethodName + arrayPostfix, new CodeTypeReferenceExpression("outStream"),
                    new CodeTypeReferenceExpression("SerializationHelper.SerializationFormat." + serializationFormat.ToString()), new CodeTypeReferenceExpression("System.ServiceModel.Web.WebMessageFormat." + responseFormat.ToString()))));
            }

            codeTryCatchFinallyStatement.FinallyStatements.Add(new CodeConditionStatement( new CodeSnippetExpression("inStream != null"),new CodeExpressionStatement(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("inStream"),"Dispose"))));
            codeTryCatchFinallyStatement.FinallyStatements.Add(new CodeConditionStatement(new CodeSnippetExpression("outStream != null"), new CodeExpressionStatement(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("outStream"), "Dispose"))));

            // Add method to declaring type
            declaringType.Members.Add(methodDecl);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Create the entire proxy class as C# code.
        /// </summary>
        public void Generate(CodeGenerator generator)
        {
            // Prepare type
            var typeDecl = new CodeTypeDeclaration(proxyTypeName);
            typeDecl.Attributes = MemberAttributes.Public | MemberAttributes.Final;
            typeDecl.BaseTypes.Add("Dot42.Internal.WcfProxyBase");
            typeDecl.BaseTypes.Add(interfaceType.FullName);
            typeDecl.CustomAttributes.Add(new CodeAttributeDeclaration("Dot42.Include", new CodeAttributeArgument("ApplyToMembers", new CodePrimitiveExpression(true))));

            var methodDecl = new CodeConstructor();
            methodDecl.Attributes = MemberAttributes.Public | MemberAttributes.Final;
            methodDecl.Parameters.Add(new CodeParameterDeclarationExpression(typeof(System.Uri), "baseAddress"));
            typeDecl.Members.Add(methodDecl);

            methodDecl.Statements.Add( new CodeAssignStatement(
                new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "baseAddress"), new CodeTypeReferenceExpression("baseAddress")));

            // Generate method code
            methodBuilders.ForEach(x => x.Generate(typeDecl, generator));

            generator.Add(typeDecl);
        }
Exemplo n.º 7
0
 /// <summary>
 /// Create the entire proxy method as C# code.
 /// </summary>
 public abstract void Generate(CodeTypeDeclaration declaringType, CodeGenerator generator);