当前位置: 首页>>代码示例>>C#>>正文


C# Microsoft.CSharp.CSharpCodeProvider.GenerateCodeFromMember方法代码示例

本文整理汇总了C#中Microsoft.CSharp.CSharpCodeProvider.GenerateCodeFromMember方法的典型用法代码示例。如果您正苦于以下问题:C# Microsoft.CSharp.CSharpCodeProvider.GenerateCodeFromMember方法的具体用法?C# Microsoft.CSharp.CSharpCodeProvider.GenerateCodeFromMember怎么用?C# Microsoft.CSharp.CSharpCodeProvider.GenerateCodeFromMember使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Microsoft.CSharp.CSharpCodeProvider的用法示例。


在下文中一共展示了Microsoft.CSharp.CSharpCodeProvider.GenerateCodeFromMember方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GenerateCS

        private void GenerateCS()
        {
            using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
            {
                CodeGeneratorOptions opts = new CodeGeneratorOptions();
                StringWriter sw = new StringWriter();
                provider.GenerateCodeFromMember(_method, sw, opts);
                StringReader sr = new StringReader(sw.GetStringBuilder().ToString());
                string line = sr.ReadLine();
                while (string.IsNullOrEmpty(line))
                    line = sr.ReadLine();
                int idx = line.IndexOf(" " + _method.Name + "(");
                idx = line.LastIndexOf(' ', idx - 1);

                if (_method.Statements.Count > 0)
                {
                    Text = "partial" + line.Remove(0, idx);
                    Text = sw.GetStringBuilder().Replace(line, Text).ToString();
                }
                else
                {
                    line = "partial" + line.Remove(0, idx);
                    idx = line.LastIndexOf(')');
                    Text = line.Remove(idx + 1) + ";" + Environment.NewLine;
                }
            }
        }
开发者ID:laymain,项目名称:CodeDomUtils,代码行数:27,代码来源:CodePartialMethod.cs

示例2: GenerateCS

 private void GenerateCS()
 {
     using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
     {
         CodeGeneratorOptions opts = new CodeGeneratorOptions();
         StringBuilder sb = new StringBuilder();
         using (IndentedTextWriter tw = new IndentedTextWriter(new StringWriter(sb), opts.IndentString))
         {
             provider.GenerateCodeFromMember(Field, tw, opts);
             var type = new StringWriter();
             provider.GenerateCodeFromExpression(new CodeTypeReferenceExpression(Field.Type), type, opts);
             sb.Replace(" " + type.GetStringBuilder(), " readonly " + type.GetStringBuilder());
         }
         Text = sb.ToString();
     }
 }
开发者ID:laymain,项目名称:CodeDomUtils,代码行数:16,代码来源:CodeReadonlyField.cs

示例3: EmitCodeToAddIntoCustomStdBinding

        internal static void EmitCodeToAddIntoCustomStdBinding(Type standardBindingType, string generatedElementClassName, string generatedCollectionElementClassName, string srcFile)
        {
            CodeMemberMethod applyCfgMethodForStdBinding = new CodeMemberMethod();
            applyCfgMethodForStdBinding.Name = MethodNameConstants.ApplyConfigurationMethod;

            string paramConfigName = "configurationName";
            CodeVariableReferenceExpression paramVarRef = new CodeVariableReferenceExpression(paramConfigName);

            applyCfgMethodForStdBinding.Parameters.Add(new CodeParameterDeclarationExpression(
                                                          CodeDomHelperObjects.stringTypeRef,
                                                          paramConfigName));

            string bindingsString = "bindings";
            CodeVariableReferenceExpression bindingsVarRef = new CodeVariableReferenceExpression(bindingsString);

            string sectionString = "section";
            CodeVariableReferenceExpression sectionVarRef = new CodeVariableReferenceExpression(sectionString);

            string elementString = "element";
            CodeVariableReferenceExpression elementVarRef = new CodeVariableReferenceExpression(elementString);

            string topLevelSectionNameInConfig = "system.serviceModel/bindings/";
            string subSectionNameInConfig = Helpers.TurnFirstCharLower(standardBindingType.Name);

            CodeVariableDeclarationStatement bindingsInit = new CodeVariableDeclarationStatement(
                                                            new CodeTypeReference(TypeNameConstants.BindingsSection),
                                                            bindingsString,
                                                            new CodeCastExpression(TypeNameConstants.BindingsSection,
                                                                new CodeMethodInvokeExpression(
                                                                    new CodeTypeReferenceExpression(TypeNameConstants.ConfigurationManager),
                                                                    MethodNameConstants.GetSectionMethod,
                                                                    new CodePrimitiveExpression(topLevelSectionNameInConfig))));
            applyCfgMethodForStdBinding.Statements.Add(bindingsInit);

            CodeVariableDeclarationStatement sectionInit = new CodeVariableDeclarationStatement(
                                                            new CodeTypeReference(generatedCollectionElementClassName),
                                                            sectionString,
                                                            new CodeCastExpression(generatedCollectionElementClassName,
                                                                new CodeArrayIndexerExpression(
                                                                    bindingsVarRef,
                                                                    new CodePrimitiveExpression(subSectionNameInConfig))));
            applyCfgMethodForStdBinding.Statements.Add(sectionInit);

            CodeVariableDeclarationStatement elementInit = new CodeVariableDeclarationStatement(
                                                new CodeTypeReference(generatedElementClassName),
                                                elementString,
                                                new CodeArrayIndexerExpression(
                                                    new CodeFieldReferenceExpression(
                                                        sectionVarRef,
                                                        PropertyNameConstants.BindingsProperty),
                                                    paramVarRef));
            applyCfgMethodForStdBinding.Statements.Add(elementInit);

            CodeBinaryOperatorExpression cboe = new CodeBinaryOperatorExpression(
                                                    elementVarRef,
                                                    CodeBinaryOperatorType.IdentityEquality,
                                                    CodeDomHelperObjects.NullRef);

            CodeThrowExceptionStatement ctes = new CodeThrowExceptionStatement(
                        new CodeObjectCreateExpression(
                            new CodeTypeReference(typeof(ConfigurationErrorsException)),
                            new CodeMethodInvokeExpression(
                                new CodeTypeReferenceExpression(CodeDomHelperObjects.stringTypeRef),
                                MethodNameConstants.FormatMethod,
                                CodeDomHelperObjects.cultureInfoCurrent,
                                new CodePrimitiveExpression("There is no binding named {0} at {1}."),
                                paramVarRef,
                                new CodePropertyReferenceExpression(
                                    sectionVarRef, PropertyNameConstants.BindingNameProperty))));
            CodeMethodInvokeExpression cmie = new CodeMethodInvokeExpression(
                                                            elementVarRef,
                                                            MethodNameConstants.ApplyConfigurationMethod,
                                                            CodeDomHelperObjects.ThisRef);
            CodeStatement[] trueStatements = { ctes };
            CodeStatement[] falseStatements = { new CodeExpressionStatement(cmie) };
            CodeConditionStatement ccs = new CodeConditionStatement(cboe, trueStatements, falseStatements);
            applyCfgMethodForStdBinding.Statements.Add(ccs);

            string indent = "    ";
            CodeDomProvider provider = new Microsoft.CSharp.CSharpCodeProvider();

            using (System.IO.StreamWriter sbSW = new System.IO.StreamWriter(srcFile, false))
            {
                using (IndentedTextWriter sbTW = new IndentedTextWriter(sbSW, indent))
                {
                    provider.GenerateCodeFromMember(
                                    applyCfgMethodForStdBinding,
                                    sbTW,
                                    new CodeGeneratorOptions());
                }
            }
        }
开发者ID:ssickles,项目名称:archive,代码行数:92,代码来源:UserTypeCodeEnhancer.cs

示例4: GenerateCS

        private void GenerateCS()
        {
            using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
            {
                CodeGeneratorOptions opts = new CodeGeneratorOptions();
                StringWriter sw = new StringWriter();
                provider.GenerateCodeFromMember(_property, sw, opts);
                StringReader sr = new StringReader(sw.GetStringBuilder().ToString());
                string line = sr.ReadLine();
                while (string.IsNullOrEmpty(line))
                    line = sr.ReadLine();
                int idx = line.IndexOf(" " + _property.Name);

                StringBuilder sb = new StringBuilder();
                sb.Append(" this[");
                foreach (CodeParameterDeclarationExpression parameter in Parameters)
                {
                    provider.GenerateCodeFromExpression(parameter, new StringWriter(sb), opts);
                    sb.Append(", ");
                }
                sb.Length -= 2;
                sb.Append("]");
                Text = sw.GetStringBuilder().Replace(line, line.Substring(0, idx) + sb.ToString() + line.Substring(idx+_property.Name.Length+1)).ToString();
            }
        }
开发者ID:laymain,项目名称:CodeDomUtils,代码行数:25,代码来源:CodeDefaultProperty.cs

示例5: GenerateCS

        private void GenerateCS()
        {
            using (Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider())
            {
                CodeGeneratorOptions opts = new CodeGeneratorOptions();
                StringWriter sw = new StringWriter();
                List<CodeTypeReference> implTypes = new List<CodeTypeReference>();
                if (_property.ImplementationTypes != null)
                {
                    var arr = new CodeTypeReference[_property.ImplementationTypes.Count];
                    _property.ImplementationTypes.CopyTo(arr, 0);
                    _property.ImplementationTypes.Clear();
                    _property.PrivateImplementationType = null;
                    implTypes.AddRange(arr);
                }
                provider.GenerateCodeFromMember(_property, sw, opts);
                foreach (CodeTypeReference tr in implTypes)
                {
                    _property.ImplementationTypes.Add(tr);
                }
                //StringReader sr = new StringReader(sw.GetStringBuilder().ToString());
                //string line = sr.ReadLine();
                //while (string.IsNullOrEmpty(line) || line.StartsWith("/") || line.StartsWith("["))
                //    line = sr.ReadLine();

                StringBuilder sb = new StringBuilder();

                if (InterfaceProperties != null)
                {
                    foreach (CodeTypeReference tr in implTypes)
                    {
                        string prop;
                        if (InterfaceProperties.TryGetValue(tr, out prop))
                        {
                            var newProp = Define.Property(_property.Type, MemberAttributes.Private, prop).Implements(tr);
                            if (_property.HasGet)
                            {
                                newProp.GetStatements.Add([email protected](()=>[email protected](_property.Name)));
                                newProp.HasGet = true;
                            }
                            if (_property.HasSet)
                            {
                                newProp.SetStatements.Add(Emit.assignProperty(_property.Name, () => CodeDom.VarRef("value")));
                                newProp.HasSet = true;
                            }

                            StringWriter swNew = new StringWriter();
                            provider.GenerateCodeFromMember(CodeDomTreeProcessor.ProcessMember(newProp, CodeDomGenerator.Language.CSharp),
                                swNew, opts);
                            sb.Append(swNew.ToString());
                        }
                    }
                    if (sb.Length > 0)
                        sb.Insert(0, Environment.NewLine);
                }
                Text = sw.GetStringBuilder().ToString() + sb.ToString();
            }
        }
开发者ID:laymain,项目名称:CodeDomUtils,代码行数:58,代码来源:CodePropertyImplementsInterface.cs


注:本文中的Microsoft.CSharp.CSharpCodeProvider.GenerateCodeFromMember方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。