public void Add()
        {
            CodeNamespaceImport ni1 = new CodeNamespaceImport("A");
            CodeNamespaceImport ni2 = new CodeNamespaceImport("B");
            CodeNamespaceImport ni3 = new CodeNamespaceImport("b");
            CodeNamespaceImport ni4 = new CodeNamespaceImport("B");

            CodeNamespaceImportCollection coll = new CodeNamespaceImportCollection();

            coll.Add(ni1);
            Assert.AreEqual(1, coll.Count, "#1");
            Assert.AreEqual(0, ((IList)coll).IndexOf(ni1), "#2");

            coll.Add(ni2);
            Assert.AreEqual(2, coll.Count, "#3");
            Assert.AreEqual(1, ((IList)coll).IndexOf(ni2), "#4");

            coll.Add(ni3);
            Assert.AreEqual(2, coll.Count, "#5");
            Assert.AreEqual(-1, ((IList)coll).IndexOf(ni3), "#6");

            coll.Add(ni4);
            Assert.AreEqual(2, coll.Count, "#7");
            Assert.AreEqual(-1, ((IList)coll).IndexOf(ni4), "#8");
        }
        public void Add_SameNamespace_DoesntAdd()
        {
            var collection = new CodeNamespaceImportCollection();
            var value1 = new CodeNamespaceImport("Namespace");
            collection.Add(value1);

            collection.Add(value1);
            Assert.Equal(1, collection.Count);

            var value2 = new CodeNamespaceImport("Namespace");
            collection.Add(value2);
            Assert.Equal(1, collection.Count);
        }
Beispiel #3
0
        public void Add_SameNamespace_DoesntAdd()
        {
            var collection = new CodeNamespaceImportCollection();
            var value1     = new CodeNamespaceImport("Namespace");

            collection.Add(value1);

            collection.Add(value1);
            Assert.Equal(1, collection.Count);

            var value2 = new CodeNamespaceImport("Namespace");

            collection.Add(value2);
            Assert.Equal(1, collection.Count);
        }
Beispiel #4
0
        static CodeNamespaceImportCollection _ParseNamespaceImports(_PC pc)
        {
            var result = new CodeNamespaceImportCollection();

            while (ST.keyword == pc.SymbolId && "using" == pc.Value)
            {
                pc.Advance();
                _SkipComments(pc);
                if (pc.IsEnded)
                {
                    throw new ArgumentException("Unterminated using declaration", "input");
                }
                if (ST.identifier != pc.SymbolId)
                {
                    throw new ArgumentException("Expecting identifier in using declaration", "input");
                }
                var ns = _ParseNamespaceName(pc);
                if (pc.IsEnded)
                {
                    throw new ArgumentException("Unterminated using declaration", "input");
                }
                if (ST.semi != pc.SymbolId)
                {
                    throw new ArgumentException("Expecting ; in using declaration", "input");
                }
                pc.Advance();
                _SkipComments(pc);
                result.Add(new CodeNamespaceImport(ns));
            }
            return(result);
        }
Beispiel #5
0
        // CodeNamespaceImportCollection
        public void CodeNamespaceImportCollectionExample()
        {
            //<Snippet3>
            //<Snippet4>
            // Creates an empty CodeNamespaceImportCollection.
            CodeNamespaceImportCollection collection =
                new CodeNamespaceImportCollection();

            //</Snippet4>

            //<Snippet5>
            // Adds a CodeNamespaceImport to the collection.
            collection.Add(new CodeNamespaceImport("System"));
            //</Snippet5>

            //<Snippet6>
            // Adds an array of CodeNamespaceImport objects to the collection.
            CodeNamespaceImport[] Imports =
            {
                new CodeNamespaceImport("System"),
                new CodeNamespaceImport("System.Drawing")
            };
            collection.AddRange(Imports);
            //</Snippet6>

            //<Snippet7>
            // Retrieves the count of the items in the collection.
            int collectionCount = collection.Count;
            //</Snippet7>
            //</Snippet3>
        }
        public void Add()
        {
            var collection = new CodeNamespaceImportCollection();
            var value = new CodeNamespaceImport();

            collection.Add(value);
            Assert.Equal(1, collection.Count);
            Assert.Same(value, collection[0]);
        }
Beispiel #7
0
        public void Add()
        {
            var collection = new CodeNamespaceImportCollection();
            var value      = new CodeNamespaceImport();

            collection.Add(value);
            Assert.Equal(1, collection.Count);
            Assert.Same(value, collection[0]);
        }
Beispiel #8
0
        public void Constructor0_Deny_Unrestricted()
        {
            CodeNamespaceImportCollection coll = new CodeNamespaceImportCollection();

            coll.Add(cni);
            Assert.AreSame(cni, coll[0], "this[int]");
            coll[0] = cni;
            coll.Clear();
            coll.AddRange(array);
            Assert.IsNotNull(coll.GetEnumerator(), "GetEnumerator");
        }
Beispiel #9
0
 /// <summary>
 /// remove Imports which contain System
 /// these imports are part of the .Net platform so they can't be resolved in a normal
 /// python environment anyway
 /// </summary>
 private static void RemoveNetSystemImports(CodeCompileUnit e)
 {
     foreach (CodeNamespace ns in e.Namespaces)
     {
         CodeNamespaceImportCollection cleaned = new CodeNamespaceImportCollection();
         foreach (CodeNamespaceImport import in ns.Imports)
         {
             if (!import.Namespace.StartsWith("System") &&
                 !import.Namespace.StartsWith("NMF"))
             {
                 cleaned.Add(import);
             }
         }
         cleaned.Add(new CodeNamespaceImport("pyNMF"));
         cleaned.Add(new CodeNamespaceImport("pyNMF.collections.generic"));
         cleaned.Add(new CodeNamespaceImport("pyNMF.collections.object_model"));
         cleaned.Add(new CodeNamespaceImport("pyNMF.serialization"));
         ns.Imports.Clear();
         foreach (CodeNamespaceImport import in cleaned)
         {
             ns.Imports.Add(import);
         }
     }
 }
        private static void ImportFieldNamespaces(Type t, CodeNamespaceImportCollection imports)
        {
            imports.Add(new CodeNamespaceImport(t.Namespace));

            if (!t.IsGenericType)
            {
                return;
            }

            foreach (var param in t.GetGenericArguments())
            {
                if (!param.IsGenericParameter)
                {
                    ImportFieldNamespaces(param, imports);
                }
            }
        }
Beispiel #11
0
 //remove Imports which contain System
 //these imports are part of the .Net platform so they can't be resolved in a normal
 //python environment anyway
 public static void removeNetSystemImports(ref CodeCompileUnit e)
 {
     foreach (CodeNamespace ns in e.Namespaces)
     {
         CodeNamespaceImportCollection cleaned = new CodeNamespaceImportCollection();
         foreach (CodeNamespaceImport import in ns.Imports)
         {
             if (!import.Namespace.StartsWith("System"))
             {
                 cleaned.Add(import);
             }
         }
         ns.Imports.Clear();
         foreach (CodeNamespaceImport import in cleaned)
         {
             ns.Imports.Add(import);
         }
     }
 }
Beispiel #12
0
 public static void Add(this CodeNamespaceImportCollection imports, string import)
 {
     imports.Add(new CodeNamespaceImport(import));
 }
        private void buildInterfaceCode(ICodeGeneratorContext codeGeneratorContext, InterfaceContract interfaceContract, out CodeTypeDeclaration codeType, out CodeNamespaceImportCollection imports)
        {
            IDictionary <XmlQualifiedName, string>    ElementName2TypeNameMapping = codeGeneratorContext.ElementName2TypeNameMapping;
            IDictionary <string, CodeTypeDeclaration> CodeTypeMap = codeGeneratorContext.CodeTypeMap;

            imports = new CodeNamespaceImportCollection();

            // generate service or client
            CodeGeneratorMode generatorMode = codeGeneratorContext.CodeGenOptions.CodeGeneratorMode;

            string interfaceName = "I" + interfaceContract.ServiceName.Replace("Interface", string.Empty);
            CodeTypeDeclaration interfaceType = new CodeTypeDeclaration(interfaceName);

            interfaceType.UserData.Add(Constants.GENERATED_TYPE, interfaceName);
            interfaceType.IsClass        = false;
            interfaceType.TypeAttributes = TypeAttributes.Public;
            interfaceType.IsInterface    = true;
            interfaceType.Comments.Clear();
            // Generate service documentation
            string serviceDoc = "Service interface auto-generated by SOA tool, DO NOT CHANGE!\n\n";

            serviceDoc += "注意,实现该接口的服务在AntServiceStack服务容器中是以new instance per request的形式被初始化的,\n";
            serviceDoc += "也就是说,容器会为每个请求创建一个新的服务实例,并在请求结束时释放(release),而不是单个\n";
            serviceDoc += "服务实例(singleton)服务所有的请求, 所以请务必不要在服务初始化(例如构造函数中)时做较重的初始化\n";
            serviceDoc += "(例如初始化数据库等)动作,否则对性能有很大影响,如果有较重的初始化动作,\n";
            serviceDoc += "请在服务实现中以静态方式(例如静态构造函数中)一次性完成,或者以IoC注入方式初始化,在服务容器\n";
            serviceDoc += "启动时事先将依赖初始化并注册在容器中,让容器在构造服务实例时自动解析和注入依赖(也可在服务实现中手动解析依赖),\n";
            serviceDoc += "关于静态和依赖注入初始化的样例,请参考AntServiceStack提供的样例程序.\n\n";

            if (!string.IsNullOrEmpty(interfaceContract.ServiceDocumentation))
            {
                serviceDoc += interfaceContract.ServiceDocumentation;
            }
            CodeDomHelper.CreateSummaryComment(interfaceType.Comments, serviceDoc);

            // Import AntServiceStack.ServiceHost namespace, requried by both client and service sides
            imports.Add(new CodeNamespaceImport(Constants.C_SERVICE_STACK_SERVICE_HOST_NAMESPACE));

            if (generatorMode == CodeGeneratorMode.Service)
            {
                // Mark as AntServiceStack supported service
                CodeAttributeDeclaration cServiceAttribute = new
                                                             CodeAttributeDeclaration("AntServiceInterface");

                string serviceName = interfaceContract.ServiceName;
                CodeAttributeArgument serviceNameArgument = new CodeAttributeArgument(new CodePrimitiveExpression(serviceName));
                cServiceAttribute.Arguments.Add(serviceNameArgument);

                string serviceNamespace = interfaceContract.ServiceNamespace;
                CodeAttributeArgument serviceNamespaceArgument = new CodeAttributeArgument(new CodePrimitiveExpression(serviceNamespace));
                cServiceAttribute.Arguments.Add(serviceNamespaceArgument);

                Version ver     = Assembly.GetExecutingAssembly().GetName().Version;
                string  version = ver.Major.ToString() + "." + ver.Minor.ToString() + "." +
                                  ver.Build.ToString() + "." + ver.Revision.ToString();
                CodeAttributeArgument codeGeneratorVersionArgument = new CodeAttributeArgument(new CodePrimitiveExpression(version));
                cServiceAttribute.Arguments.Add(codeGeneratorVersionArgument);
                interfaceType.CustomAttributes.Add(cServiceAttribute);
            }

            //CodeTypeDeclaration healthCheckRequestType = null;
            //CodeTypeDeclaration healthCheckResponseType = null;
            bool hasAsync = false;

            foreach (Operation operation in interfaceContract.OperationsCollection)
            {
                var isHealthCheckOperation = false;

                CodeMemberMethod method = new CodeMemberMethod();
                method.Name = operation.Name;
                if (operation.Name.ToLower() == HEALTH_CHECK_OPERATION_NAME.ToLower())
                {
                    isHealthCheckOperation = true;
                }

                Message          inMessage             = operation.Input;
                XmlQualifiedName inMessageElementQName = new XmlQualifiedName(inMessage.Element.ElementName, inMessage.Element.ElementNamespace);
                string           requestTypeName       = null;
                ElementName2TypeNameMapping.TryGetValue(inMessageElementQName, out requestTypeName);
                Enforce.IsNotNull <string>(requestTypeName, "Fail to retrieve request type from wsdl using innput message element QName : " + inMessageElementQName);

                CodeTypeReference requestTypeReference         = new CodeTypeReference(requestTypeName);
                CodeParameterDeclarationExpression methodParam =
                    new CodeParameterDeclarationExpression(requestTypeReference, "request");
                methodParam.Type = requestTypeReference;
                method.Parameters.Add(methodParam);

                Message outMessage = operation.Output;
                Enforce.IsNotNull <Message>(outMessage, "Fail to get out message in operation :  " + operation.Name + ", only requst/response style operation is supported");
                XmlQualifiedName outMessageElementQName = new XmlQualifiedName(outMessage.Element.ElementName, outMessage.Element.ElementNamespace);
                string           responseTypeName       = null;
                ElementName2TypeNameMapping.TryGetValue(outMessageElementQName, out responseTypeName);
                Enforce.IsNotNull <string>(responseTypeName, "Fail to retrieve response type from wsdl using output message element QName : " + outMessageElementQName);
                if (codeGeneratorContext.CodeGenOptions.GenerateAsyncOperations && outMessageElementQName.Name.EndsWith("AsyncResponse"))
                {
                    method.ReturnType = new CodeTypeReference("Task<" + responseTypeName + ">");
                    hasAsync          = true;
                }
                else
                {
                    method.ReturnType = new CodeTypeReference(responseTypeName);
                }

                //  SOA Policy enforcement : response type must extend SOA common AbstractResponseType
                CodeTypeDeclaration responseType = null;
                CodeTypeMap.TryGetValue(responseTypeName, out responseType);
                Enforce.IsNotNull <CodeTypeDeclaration>(responseType, "Weird code generator internal error, please ask soa framework team for help.");

                if (isHealthCheckOperation)
                {
                    //healthCheckResponseType = responseType;
                }

                if (!CodeExtension.HasProperty(responseType, Constants.RESPONSE_STATUS_PROPERTY_NAME, Constants.RESPONSE_STATUS_TYPE_NAME))
                {
                    throw new SOAPolicyViolationException(string.Format(" SOA Policy Violation, response type '{0}' does not include requried {1} property of type {2}",
                                                                        responseTypeName, Constants.RESPONSE_STATUS_PROPERTY_NAME, Constants.RESPONSE_STATUS_TYPE_NAME));
                }
                CodeTypeDeclaration responseStatusType = null;
                CodeTypeMap.TryGetValue(Constants.RESPONSE_STATUS_TYPE_NAME, out responseStatusType);
                Enforce.IsNotNull <CodeTypeDeclaration>(responseStatusType, string.Format("Weird code generator internal error, missing requried {0}, please ask soa framework team for help.", Constants.RESPONSE_STATUS_TYPE_NAME));
                if (!CodeExtension.IsSOACommonType(responseStatusType))
                {
                    throw new SOAPolicyViolationException(string.Format(" SOA Policy Violation, {0} reference is not  SOA Common {1}.", Constants.RESPONSE_STATUS_TYPE_NAME, Constants.RESPONSE_STATUS_TYPE_NAME));
                }

                if (!CodeExtension.HasInterface(responseType, HAS_RESPONSE_STATUS_INTERFACE_NAME))
                {
                    // make response type implement IHasResponseStatus interface
                    responseType.BaseTypes.Add(HAS_RESPONSE_STATUS_INTERFACE_NAME);
                }

                // optional common request handling
                CodeTypeDeclaration requestType = null;
                CodeTypeMap.TryGetValue(requestTypeName, out requestType);
                Enforce.IsNotNull <CodeTypeDeclaration>(requestType, "Weird code generator internal error, please ask soa framework team for help.");

                if (isHealthCheckOperation)
                {
                    //healthCheckRequestType = requestType;
                }

                if (CodeExtension.HasProperty(requestType, MOBILE_REQUEST_HEAD_PROPERTY_NAME, MOBILE_REQUEST_HEAD_TYPE_NAME) &&
                    !CodeExtension.HasInterface(requestType, HAS_MOBILE_REQUEST_HEAD_INTERFACE_NAME))
                {
                    requestType.BaseTypes.Add(HAS_MOBILE_REQUEST_HEAD_INTERFACE_NAME);
                }

                if (CodeExtension.HasProperty(responseType, Constants.COMMON_REQUEST_PROPERTY_NAME, Constants.COMMON_REQUEST_TYPE_NAME))
                {
                    CodeTypeDeclaration commonRequestType = null;
                    CodeTypeMap.TryGetValue(Constants.COMMON_REQUEST_TYPE_NAME, out commonRequestType);
                    Enforce.IsNotNull <CodeTypeDeclaration>(commonRequestType, string.Format("Weird code generator internal error, missing requried {0}, please ask soa framework team for help.", Constants.COMMON_REQUEST_TYPE_NAME));
                    if (!CodeExtension.IsSOACommonType(commonRequestType))
                    {
                        throw new SOAPolicyViolationException(string.Format(" SOA Policy Violation, {0} reference is not Ant SOA Common {1}.", Constants.COMMON_REQUEST_TYPE_NAME, Constants.COMMON_REQUEST_TYPE_NAME));
                    }
                    if (!CodeExtension.HasInterface(responseType, HAS_COMMON_REQUEST_INTERFACE_NAME))
                    {
                        // make request type implement IHasCommonRequest interface
                        responseType.BaseTypes.Add(HAS_COMMON_REQUEST_INTERFACE_NAME);
                    }
                }

                // Generate operation documentation
                if (!string.IsNullOrEmpty(operation.Documentation))
                {
                    CodeDomHelper.CreateSummaryComment(method.Comments, operation.Documentation);
                }

                interfaceType.Members.Add(method);
            }

            if (hasAsync)
            {
                imports.Add(new CodeNamespaceImport(Constants.SYSTEM_THREADING_TASKS_NAMESPACE));
            }

            // SOA Policy enforcement : healtch check operation is mandatory
            //if (healthCheckRequestType == null || healthCheckResponseType == null)
            //{
            //    throw new SOAPolicyViolationException(string.Format("SOA Policy Violation, missing mandatory check health operation."));
            //}
            //if (!CodeExtension.IsSOACommonType(healthCheckRequestType) || !CodeExtension.IsSOACommonType(healthCheckResponseType))
            //{
            //    throw new SOAPolicyViolationException(string.Format("SOA Policy Violation, wrong SOA common healthcheck types."));
            //}

            if (generatorMode == CodeGeneratorMode.Service)
            {
                codeType = interfaceType;
                return;
            }

            imports.Add(new CodeNamespaceImport(SYSTEM_NAMESPACE));
            imports.Add(new CodeNamespaceImport(SYSTEM_THREADING_NAMESPACE));
            imports.Add(new CodeNamespaceImport(SYSTEM_THREADING_TASKS_NAMESPACE));
            imports.Add(new CodeNamespaceImport(
                            generatorMode == CodeGeneratorMode.Client ? C_SERVICE_STACK_SERVICE_CLIENT_NAMESPACE : C_SERVICE_STACK_AUTOMATION_TEST_CLIENT_NAMESPACE));

            string clientName = interfaceContract.ServiceName.Replace("Interface", string.Empty) + "Client";

            CodeTypeDeclaration clientType = new CodeTypeDeclaration(clientName);

            clientType.UserData.Add(Constants.GENERATED_TYPE, clientName);
            var baseType = new CodeTypeReference(
                generatorMode == CodeGeneratorMode.Client ? SERVICE_CLIENT_BASE_NAME : SERVICE_CLIENT_FOR_AUTOMATION_BASE_NAME,
                new CodeTypeReference[] { new CodeTypeReference(clientName) });

            clientType.BaseTypes.Add(baseType);
            codeType = clientType;

            // Generate client documentation
            string clientDoc = "Service client auto-generated by SOA tool, DO NOT CHANGE!\n\n";

            if (!string.IsNullOrEmpty(interfaceContract.ServiceDocumentation))
            {
                clientDoc += interfaceContract.ServiceDocumentation;
            }
            CodeDomHelper.CreateSummaryComment(clientType.Comments, clientDoc);

            // base constructor
            //CodeConstructor baseConstructor = new CodeConstructor();
            //baseConstructor.Attributes = MemberAttributes.Public;
            //baseConstructor.BaseConstructorArgs.Add(new CodeVariableReferenceExpression(""));
            //clientType.Members.Add(baseConstructor);

            // public constant string service name and namespace
            CodeMemberField codeMemberField = new CodeMemberField(typeof(string), SERVICE_CLIENT_CODE_GENERATOR_VERSION_FIELD_NAME);

            codeMemberField.Attributes     = (codeMemberField.Attributes & ~MemberAttributes.AccessMask & ~MemberAttributes.ScopeMask) | MemberAttributes.Public | MemberAttributes.Const;
            codeMemberField.InitExpression = new CodePrimitiveExpression(typeof(InterfaceContractGenerator).Assembly.GetName().Version.ToString());
            clientType.Members.Add(codeMemberField);
            codeMemberField                = new CodeMemberField(typeof(string), SERVICE_CLIENT_ORIGINAL_SERVICE_NAME_FIELD_NAME);
            codeMemberField.Attributes     = (codeMemberField.Attributes & ~MemberAttributes.AccessMask & ~MemberAttributes.ScopeMask) | MemberAttributes.Public | MemberAttributes.Const;
            codeMemberField.InitExpression = new CodePrimitiveExpression(interfaceContract.ServiceName);
            clientType.Members.Add(codeMemberField);
            codeMemberField                = new CodeMemberField(typeof(string), SERVICE_CLIENT_ORIGINAL_SERVICE_NAMESPACE_FIELD_NAME);
            codeMemberField.Attributes     = (codeMemberField.Attributes & ~MemberAttributes.AccessMask & ~MemberAttributes.ScopeMask) | MemberAttributes.Public | MemberAttributes.Const;
            codeMemberField.InitExpression = new CodePrimitiveExpression(interfaceContract.ServiceNamespace);
            clientType.Members.Add(codeMemberField);
            codeMemberField                = new CodeMemberField(typeof(string), SERVICE_CLIENT_ORIGINAL_SERVICE_TYPE_FIELD_NAME);
            codeMemberField.Attributes     = (codeMemberField.Attributes & ~MemberAttributes.AccessMask & ~MemberAttributes.ScopeMask) | MemberAttributes.Public | MemberAttributes.Const;
            codeMemberField.InitExpression = new CodePrimitiveExpression(SERVICE_CLIENT_NON_SLB_SERVICE_TYPE_FIELD_NAME);
            clientType.Members.Add(codeMemberField);

            // private constructor with baseUri parameter
            CodeConstructor baseConstructorWithParameter = new CodeConstructor();

            baseConstructorWithParameter.Attributes = MemberAttributes.Private;
            baseConstructorWithParameter.Parameters.Add(new CodeParameterDeclarationExpression("System.String", "baseUri"));
            baseConstructorWithParameter.BaseConstructorArgs.Add(new CodeVariableReferenceExpression("baseUri"));
            clientType.Members.Add(baseConstructorWithParameter);

            // private constructor with serviceName & serviceNamespace parameter
            baseConstructorWithParameter            = new CodeConstructor();
            baseConstructorWithParameter.Attributes = MemberAttributes.Private;
            baseConstructorWithParameter.Parameters.Add(new CodeParameterDeclarationExpression("System.String", "serviceName"));
            baseConstructorWithParameter.BaseConstructorArgs.Add(new CodeVariableReferenceExpression("serviceName"));
            baseConstructorWithParameter.Parameters.Add(new CodeParameterDeclarationExpression("System.String", "serviceNamespace"));
            baseConstructorWithParameter.BaseConstructorArgs.Add(new CodeVariableReferenceExpression("serviceNamespace"));
            baseConstructorWithParameter.Parameters.Add(new CodeParameterDeclarationExpression("System.String", "subEnv"));
            baseConstructorWithParameter.BaseConstructorArgs.Add(new CodeVariableReferenceExpression("subEnv"));
            clientType.Members.Add(baseConstructorWithParameter);

            // build methods
            foreach (Operation operation in interfaceContract.OperationsCollection)
            {
                string           operationName         = operation.Name;
                Message          inMessage             = operation.Input;
                XmlQualifiedName inMessageElementQName = new XmlQualifiedName(inMessage.Element.ElementName, inMessage.Element.ElementNamespace);
                string           requestTypeName       = null;
                ElementName2TypeNameMapping.TryGetValue(inMessageElementQName, out requestTypeName);
                Message          outMessage             = operation.Output;
                XmlQualifiedName outMessageElementQName = new XmlQualifiedName(outMessage.Element.ElementName, outMessage.Element.ElementNamespace);
                string           responseTypeName       = null;
                ElementName2TypeNameMapping.TryGetValue(outMessageElementQName, out responseTypeName);
                CodeTypeReference responseType = new CodeTypeReference(responseTypeName);

                BuildSyncMethod(clientType, operation, requestTypeName, responseTypeName);
                BuildSyncWithCallbackMethod(clientType, operation, requestTypeName, responseTypeName);
                BuildCreateRequestTaskMethod(clientType, operation, requestTypeName, responseTypeName);
                BuildStartIOCPTaskMethod(clientType, operation, requestTypeName, responseTypeName);
            }
        }
 public void Add_Null_ThrowsNullReferenceException()
 {
     var collection = new CodeNamespaceImportCollection();
     Assert.Throws<NullReferenceException>(() => collection.Add(null));
 }
        public void Generate(IEnumerable <string> names, string targetDirectory)
        {
            CodeNamespaceImportCollection imports = new CodeNamespaceImportCollection();

            // Generate the ILibiMobileDevice interface
            CodeTypeDeclaration interfaceType = new CodeTypeDeclaration();

            interfaceType.Name        = "ILibiMobileDevice";
            interfaceType.IsInterface = true;

            foreach (var name in names)
            {
                interfaceType.Members.Add(
                    new CodeMemberProperty()
                {
                    Name   = name,
                    Type   = new CodeTypeReference($"I{name}Api"),
                    HasGet = true
                });

                imports.Add(new CodeNamespaceImport($"iMobileDevice.{name}"));
            }

            // Generate the interface implementation
            CodeTypeDeclaration classType = new CodeTypeDeclaration();

            classType.Name = "LibiMobileDevice";
            classType.BaseTypes.Add("ILibiMobileDevice");

            var constructor = new CodeConstructor();

            constructor.Attributes = MemberAttributes.Public;
            classType.Members.Add(constructor);

            var instanceField = new CodeMemberField();

            instanceField.Attributes     = MemberAttributes.Static;
            instanceField.Name           = "instance";
            instanceField.Type           = new CodeTypeReference("ILibiMobileDevice");
            instanceField.InitExpression =
                new CodeObjectCreateExpression(
                    new CodeTypeReference("LibiMobileDevice"));
            classType.Members.Add(instanceField);

            var instanceProperty = new CodeMemberProperty();

            instanceProperty.Attributes = MemberAttributes.Static | MemberAttributes.Public;
            instanceProperty.Name       = "Instance";
            instanceProperty.HasGet     = true;
            instanceProperty.Type       = new CodeTypeReference("ILibiMobileDevice");

            instanceProperty.GetStatements.Add(
                new CodeMethodReturnStatement(
                    new CodeFieldReferenceExpression(
                        new CodeTypeReferenceExpression("LibiMobileDevice"),
                        "instance")));

            instanceProperty.SetStatements.Add(
                new CodeAssignStatement(
                    new CodeFieldReferenceExpression(
                        new CodeTypeReferenceExpression("LibiMobileDevice"),
                        "instance"),
                    new CodeArgumentReferenceExpression("value")));

            classType.Members.Add(instanceProperty);

            foreach (var name in names)
            {
                string camelCased = null;

                if (name[0] != 'i')
                {
                    camelCased = char.ToLower(name[0]) + name.Substring(1);
                }
                else
                {
                    camelCased = "i" + char.ToLower(name[1]) + name.Substring(2);
                }

                // Add the backing field
                classType.Members.Add(
                    new CodeMemberField()
                {
                    Name = camelCased,
                    Type = new CodeTypeReference($"I{name}Api")
                });

                // Add the property + getter
                var property =
                    new CodeMemberProperty()
                {
                    Name       = name,
                    Type       = new CodeTypeReference($"I{name}Api"),
                    HasGet     = true,
                    HasSet     = true,
                    Attributes = MemberAttributes.Public
                };
                property.GetStatements.Add(
                    new CodeMethodReturnStatement(
                        new CodeFieldReferenceExpression(
                            new CodeThisReferenceExpression(),
                            camelCased)));
                property.SetStatements.Add(
                    new CodeAssignStatement(
                        new CodeFieldReferenceExpression(
                            new CodeThisReferenceExpression(),
                            camelCased),
                        new CodeArgumentReferenceExpression("value")));
                classType.Members.Add(property);

                constructor.Statements.Add(
                    new CodeAssignStatement(
                        new CodeFieldReferenceExpression(
                            new CodeThisReferenceExpression(),
                            camelCased),
                        new CodeObjectCreateExpression(
                            new CodeTypeReference($"{name}Api"),
                            new CodeThisReferenceExpression())));
            }

            // Add the LibraryFound property
            var libraryFoundInterfaceProperty = new CodeMemberProperty();

            libraryFoundInterfaceProperty.Name   = "LibraryFound";
            libraryFoundInterfaceProperty.Type   = new CodeTypeReference(typeof(bool));
            libraryFoundInterfaceProperty.HasGet = true;
            interfaceType.Members.Add(libraryFoundInterfaceProperty);

            var libraryFoundClassProperty = new CodeMemberProperty();

            libraryFoundClassProperty.Name       = "LibraryFound";
            libraryFoundClassProperty.Attributes = MemberAttributes.Public;
            libraryFoundClassProperty.Type       = new CodeTypeReference(typeof(bool));
            libraryFoundClassProperty.HasGet     = true;
            libraryFoundClassProperty.GetStatements.Add(
                new CodeMethodReturnStatement(
                    new CodePropertyReferenceExpression(
                        new CodeTypeReferenceExpression("NativeLibraries"),
                        "LibraryFound")));
            classType.Members.Add(libraryFoundClassProperty);

            foreach (var type in new CodeTypeDeclaration[] { interfaceType, classType })
            {
                // Generate the container unit
                CodeCompileUnit program = new CodeCompileUnit();

                // Generate the namespace
                CodeNamespace ns = new CodeNamespace($"iMobileDevice");
                ns.Imports.AddRange(imports.OfType <CodeNamespaceImport>().ToArray());
                ns.Types.Add(type);
                program.Namespaces.Add(ns);

                string path = Path.Combine(targetDirectory, $"{type.Name}.cs");

                using (var outFile = File.Open(path, FileMode.Create))
                    using (var fileWriter = new StreamWriter(outFile))
                        using (var indentedTextWriter = new IndentedTextWriter(fileWriter, "    "))
                        {
                            // Generate source code using the code provider.
                            var provider = new Microsoft.CSharp.CSharpCodeProvider();
                            provider.GenerateCodeFromCompileUnit(
                                program,
                                indentedTextWriter,
                                new CodeGeneratorOptions()
                            {
                                BracingStyle = "C"
                            });
                        }
            }
        }
        static CodeCompileUnit ParseInternal(TextReader codeStream)
        {
            IParser parser = ParserFactory.CreateParser(
                SupportedLanguage.CSharp,
                codeStream);

            parser.ParseMethodBodies = true;
            parser.Parse();

            if (parser.Errors.Count > 0)
            {
                throw new ArgumentException(parser.Errors.ErrorOutput);
            }

            var cdv = new CodeDomVisitor(); // new CodeDomVisitor (parser.Lexer.SpecialTracker.CurrentSpecials);

            parser.CompilationUnit.AcceptVisitor(cdv, null);

            parser.Dispose();

            CodeCompileUnit ccu = cdv.codeCompileUnit;

            //C# parser seems to insist on putting imports in the "Global" namespace; fix it up
            for (int i = 0; i < ccu.Namespaces.Count; i++)
            {
                CodeNamespace global = ccu.Namespaces [i];
                if ((global.Name == "Global") && (global.Types.Count == 0))
                {
                    global.Name = "";
                    ccu.Namespaces.RemoveAt(i);
                    ccu.Namespaces.Insert(0, global);

                    //clear out repeat imports...
                    for (int j = 1; j < ccu.Namespaces.Count; j++)
                    {
                        CodeNamespace cn = ccu.Namespaces [j];

                        //why can't we remove imports? will have to collect ones to keep
                        //then clear and refill
                        CodeNamespaceImportCollection imps = new CodeNamespaceImportCollection();

                        for (int m = 0; m < cn.Imports.Count; m++)
                        {
                            bool found = false;

                            for (int n = 0; n < global.Imports.Count; n++)
                            {
                                if (global.Imports [n] == cn.Imports [m])
                                {
                                    found = true;
                                }
                            }

                            if (!found)
                            {
                                imps.Add(cn.Imports [m]);
                            }
                        }

                        cn.Imports.Clear();

                        foreach (CodeNamespaceImport imp in imps)
                        {
                            cn.Imports.Add(imp);
                        }
                    }

                    break;
                }
            }
            return(ccu);
        }
Beispiel #17
0
        public void Add_Null_ThrowsNullReferenceException()
        {
            var collection = new CodeNamespaceImportCollection();

            Assert.Throws <NullReferenceException>(() => collection.Add(null));
        }
        public void Add_Null()
        {
            CodeNamespaceImportCollection coll = new CodeNamespaceImportCollection();

            coll.Add((CodeNamespaceImport)null);
        }