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


C# CSharpCodeProvider.GenerateCodeFromType方法代码示例

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


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

示例1: CreatePublicApiForAssembly

        public static string CreatePublicApiForAssembly(Assembly assembly)
        {
            var publicApiBuilder = new StringBuilder();
            var cgo = new CodeGeneratorOptions
            {
                BracingStyle = "C",
                BlankLinesBetweenMembers = false
            };

            using (var provider = new CSharpCodeProvider())
            {
                var publicTypes = assembly.GetTypes()
                    .Where(t => t.IsPublic && t.Name != "GeneratedInternalTypeHelper") //GeneratedInternalTypeHelper seems to be a r# runner side effect
                    .OrderBy(t => t.FullName);
                foreach (var publicType in publicTypes)
                {
                    var writer = new StringWriter();
                    var genClass = CreateClassDeclaration(publicType);
                    foreach (var memberInfo in publicType.GetMembers().Where(m => !IsDotNetTypeMember(m)).OrderBy(m => m.Name))
                    {
                        AddMemberToClassDefinition(genClass, memberInfo);
                    }
                    provider.GenerateCodeFromType(genClass, writer, cgo);
                    var gennedClass = GenerateClassCode(writer);
                    publicApiBuilder.AppendLine(gennedClass);
                }
            }
            var publicApi = publicApiBuilder.ToString();
            return publicApi.Trim();
        }
开发者ID:BrainCrumbz,项目名称:Humanizer,代码行数:30,代码来源:PublicApiGenerator.cs

示例2: Main

        static void Main(string[] args)
        {
            CodeTypeDeclaration MyClass = Class("MyClass");
            MyClass.Add(Method("StrLen", Type(typeof(int))));
            TheMethod.Add(Parameter(typeof(string), "str"));
            TheMethod.Return(Expression("str.Length"));

            CSharpCodeProvider Provider = new CSharpCodeProvider();
            Provider.GenerateCodeFromType(MyClass, Console.Out, new System.CodeDom.Compiler.CodeGeneratorOptions());
            Console.ReadKey();
        }
开发者ID:jtalbert123,项目名称:CodeDOM,代码行数:11,代码来源:Program.cs

示例3: ConvertAttrbuteToCode

 // Litee: This method is used for additional sorting of custom attributes when multiple values are allowed
 private static object ConvertAttrbuteToCode(Func<CodeTypeReference, CodeTypeReference> codeTypeModifier, CustomAttribute customAttribute)
 {
     using (var provider = new CSharpCodeProvider())
     {
         var cgo = new CodeGeneratorOptions
         {
             BracingStyle = "C",
             BlankLinesBetweenMembers = false,
             VerbatimOrder = false
         };
         var attribute = GenerateCodeAttributeDeclaration(codeTypeModifier, customAttribute);
         var declaration = new CodeTypeDeclaration("DummyClass")
         {
             CustomAttributes = new CodeAttributeDeclarationCollection(new[] { attribute }),
         };
         using (var writer = new StringWriter())
         {
             provider.GenerateCodeFromType(declaration, writer, cgo);
             return writer.ToString();
         }
     }
 }
开发者ID:Particular,项目名称:ServiceControl.Plugin.Nsb6.CustomChecks,代码行数:23,代码来源:PublicApiGenerator.cs

示例4: GenerateCSharpTo

 /// <summary>
 /// Generates CSharp text from a type declaration and writes it to a text writer with the given options.
 /// </summary>
 /// <param name="typeDeclaration">The type declaration to generate text from.</param>
 /// <param name="writer">The text writer to write to.</param>
 /// <param name="options">The generation options.</param>
 public static void GenerateCSharpTo(this CodeTypeDeclaration typeDeclaration, TextWriter writer, CodeGeneratorOptions options)
 {
     using (var provider = new CSharpCodeProvider())
     {
         provider.GenerateCodeFromType(typeDeclaration, writer, options);
     }
 }
开发者ID:DzmitrySo,项目名称:mc,代码行数:13,代码来源:CodeDomGenerator.cs

示例5: DeclToString

        private static string DeclToString(CodeTypeDeclaration decl)
        {
            var provider = new CSharpCodeProvider();
            var opts = new CodeGeneratorOptions { IndentString = "    ", BracingStyle = "C" };

            var writer = new StringWriter();
            provider.GenerateCodeFromType(decl, writer, opts);
            return writer.GetStringBuilder().ToString();
        }
开发者ID:vietnt,项目名称:Castle.MonoRail3,代码行数:9,代码来源:CodeGenTests.cs

示例6: GetClassWrapperText


//.........这里部分代码省略.........
     {
      if(((MethodInfo)mi).IsSpecialName)
       break;
      CodeMemberMethod cc = new CodeMemberMethod();
      if(((MethodInfo)mi).Name.Contains("."))
       cc.Attributes = (cc.Attributes & ~MemberAttributes.AccessMask);
      else if(((MethodInfo)mi).IsPrivate)
       cc.Attributes = (cc.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Private;
      else if(((MethodInfo)mi).IsPublic)
       cc.Attributes = (cc.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
      if(((MethodInfo)mi).IsStatic)
       cc.Attributes |= MemberAttributes.Static;
      cc.Name = ((MethodInfo)mi).Name;
      foreach(ParameterInfo i in ((MethodInfo)mi).GetParameters())
      {
       CodeParameterDeclarationExpression par = new CodeParameterDeclarationExpression(i.ParameterType, i.Name);
       if(i.IsOut)
        par.Direction = FieldDirection.Out;
       if(i.IsRetval)
        par.Direction = FieldDirection.Ref;
       if(i.IsIn)
        par.Direction = FieldDirection.In;
       if(i.ParameterType.IsByRef)
       {
        par.Type = new CodeTypeReference(i.ParameterType.FullName.Replace("&", ""));
        cc.Statements.Add(new CodeSnippetExpression(
         String.Format("{0} = default({1})", i.Name, par.Type.BaseType)));
       }
       cc.Parameters.Add(par);
      }
      foreach(Type p in ((MethodInfo)mi).GetGenericArguments())
       cc.TypeParameters.Add(p.Name);
      cc.ReturnType = new CodeTypeReference(((MethodInfo)mi).ReturnType);
      if(((MethodInfo)mi).ReturnType != typeof(void))
       cc.Statements.Add(new CodeMethodReturnStatement(new CodeDefaultValueExpression(cc.ReturnType)));
      ctd.Members.Add(cc);
     }
     break;
     case MemberTypes.Property:
     {
      #region ...
      CodeMemberProperty cc = new CodeMemberProperty();
      cc.Name = ((PropertyInfo)mi).Name;
      foreach(ParameterInfo i in ((PropertyInfo)mi).GetIndexParameters())
       cc.Parameters.Add(new CodeParameterDeclarationExpression(i.ParameterType, i.Name));
      cc.Type = new CodeTypeReference(((PropertyInfo)mi).PropertyType);
      cc.HasGet = ((PropertyInfo)mi).CanRead;
      cc.HasSet = ((PropertyInfo)mi).CanWrite;
      cc.GetStatements.Add(new CodeMethodReturnStatement(new CodeDefaultValueExpression(cc.Type)));

      MethodInfo[] mis = ((PropertyInfo)mi).GetAccessors();
      if(((PropertyInfo)mi).Name.Contains("."))
       cc.Attributes = (cc.Attributes & ~MemberAttributes.AccessMask);
      else if(mis.Length > 0)
       cc.Attributes = (cc.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
      else
       cc.Attributes = (cc.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Private;
      ctd.Members.Add(cc);
      #endregion ...
     }
     break;

    }

   CodeMemberMethod dm = new CodeMemberMethod();
   dm.Attributes = (dm.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Private;
   dm.Name = "DynamicMethod";
   dm.ReturnType = new CodeTypeReference(typeof(object));
   if(String.IsNullOrWhiteSpace(dynMethodBody))
    dm.Statements.Add(new CodeSnippetStatement("\r\n  //Dynamic Method Body\r\n\r\n\r\n   return null;"));
   dm.Statements.Add(new CodeSnippetStatement(dynMethodBody));
   ctd.Members.Add(dm);

   string s;
   using(CSharpCodeProvider cp = new CSharpCodeProvider())
    using(System.IO.TextWriter tw = new System.IO.StringWriter())
    {
     CodeGeneratorOptions cgo = new CodeGeneratorOptions();
     cgo.BracingStyle = "C";
     cgo.IndentString = " ";
     cgo.BlankLinesBetweenMembers = false;
     cp.GenerateCodeFromType(ctd, tw, cgo);
     s = tw.ToString();
    }
   s = s.Insert(0, "using System;\r\nusing System.Collections;\r\nusing System.Collections.Generic;\r\n" +
                   "\r\nusing Pulsar;\r\nusing Pulsar.Shared;\r\n");
   s = s.Replace(" {\r\n", " { ");
   s = s.Replace("\r\n {", " {");
   s = s.Replace("\r\n  {", " {");
   s = s.Replace("\r\n   {", " {");
   s = s.Replace("\r\n }", " }");
   s = s.Replace("\r\n  }", " }");
   s = s.Replace("\r\n   }", " }");
   s = s.Replace("  ", " ");
   s = s.Replace("  ", " ");
   s = s.Replace("DynamicMethod() {", "DynamicMethod()\r\n {");
   s = s.Replace("\r\n set {", "set {");
   s = s.Replace("\r\n return ", " return ");
   return s;
  }
开发者ID:GoldMax,项目名称:Pulsar.NET,代码行数:101,代码来源:PulsarCodeTransfer.cs

示例7: Test

        public void Test()
        {
            string[] Names = Enum.GetNames(typeof(Voldemort.AdminRequestType));

            CodeTypeDeclaration implementation = new CodeTypeDeclaration("VoldemortAdminClient");
            implementation.IsClass = true;
            implementation.Attributes = MemberAttributes.Public;
            CodeTypeDeclaration interfaceType = new CodeTypeDeclaration("IVoldemortAdminClient");
            interfaceType.IsInterface = true;
            interfaceType.Attributes = MemberAttributes.Public;

            foreach (string Name in Names)
            {
                string[] parts = Name.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < parts.Length; i++)
                    parts[i] = System.Globalization.CultureInfo.CurrentUICulture.TextInfo.ToTitleCase(parts[i].ToLower());
                CodeMemberMethod method = new CodeMemberMethod();
                method.Name = string.Concat(parts);
                interfaceType.Members.Add(method);

                string typeName = string.Format("Voldemort.{0}Request, Voldemort", method.Name);


                Type type = Type.GetType(typeName);

                if (null == type)
                    continue;

                PropertyInfo[] properties = type.GetProperties();

                foreach (PropertyInfo property in properties)
                {
                    CodeParameterDeclarationExpression methodParameter = new CodeParameterDeclarationExpression(property.PropertyType, property.Name);
                    method.Parameters.Add(methodParameter);
                }
            }

            CodeGeneratorOptions options = new CodeGeneratorOptions();
            options.BracingStyle = "C";
            options.BlankLinesBetweenMembers = false;

            CSharpCodeProvider csp = new CSharpCodeProvider();
            csp.GenerateCodeFromType(interfaceType, Console.Out, options);
        }
开发者ID:jcustenborder,项目名称:voldemort,代码行数:44,代码来源:StoreClientTests.cs


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