C# CodeDom不使用MemberAttributes

C# CodeDom不使用MemberAttributes,c#,code-generation,codedom,C#,Code Generation,Codedom,在Ms visual studio下学习CodeDom时,我注意到代码生成似乎忽略了为我的类设置的成员属性 这是我的示例代码 using Microsoft.CSharp; using System; using System.CodeDom; using System.CodeDom.Compiler; using System.IO; namespace CodeDomTest { class Program { public static CodeComp

在Ms visual studio下学习CodeDom时,我注意到代码生成似乎忽略了为我的类设置的成员属性

这是我的示例代码

using Microsoft.CSharp;
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.IO;

namespace CodeDomTest
{
    class Program
    {
        public static CodeCompileUnit BuildHelloWorldGraph()
        {
            // Create a new CodeCompileUnit to contain
            // the program graph.
            CodeCompileUnit compileUnit = new CodeCompileUnit();

            // Declare a new namespace called Samples.
            CodeNamespace samples = new CodeNamespace("Samples");
            // Add the new namespace to the compile unit.
            compileUnit.Namespaces.Add(samples);

            // Declare a new type called Class1.
            CodeTypeDeclaration class1 = new CodeTypeDeclaration("Class1");
            // Add the new type to the namespace type collection.
            samples.Types.Add(class1); // should be private
            Console.WriteLine("Class1 attributes: " + class1.Attributes);

            return compileUnit;
        }
        public static string GenerateCSharpCode(CodeCompileUnit compileunit)
        {
            // Generate the code with the C# code provider.
            CSharpCodeProvider provider = new CSharpCodeProvider();

            // Create a TextWriter to a StreamWriter to the output file.
            StringWriter strWriter = new StringWriter();

            IndentedTextWriter tw = new IndentedTextWriter(strWriter, "    ");

            // Generate source code using the code provider.
            provider.GenerateCodeFromCompileUnit(compileunit, tw,
                new CodeGeneratorOptions());

            // Close the output file.
            tw.Close();

            return strWriter.ToString();
        }

        static void Main(string[] args)
        {
            Console.WriteLine(GenerateCSharpCode(BuildHelloWorldGraph()));
            Console.ReadKey();
        }
    }
}
它产生以下输出:

Class1 attributes: 20482
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Samples {


    public class Class1 {
    }
}
Class1属性:20482
//------------------------------------------------------------------------------
//,这意味着该类应为private(20480)和final(2)。相反,该类被生成为public。
这里发生了什么事?

浏览:

TypeAttributes属性表示类型声明的TypeAttributes值,该值表示类型的类型类别

进一步看:

Attributes属性是从CodeTypeMember继承的CodeTypeDeclaration类的副作用,因此可以嵌套类应使用TypeAttributes属性中的标志,而不是Attributes属性中的标志。

(我的重点)

所以看起来您应该应用
TypeAttributes.Sealed
,等等


还请注意,“private”仅适用于嵌套类(和成员),而“final”通常指成员:“sealed”是类的术语。

快速浏览文档,也许您应该使用
TypeAttributes
来进行
code类型声明
?非嵌套类不能是私有的,“final”是应用于成员的,而不是类(类是“密封的”)@canton7好的,我错过了。将其设置为
[…]NestedPrivate
确实解决了这个问题。非常奇怪的是,这个对象同时有一个
属性
和一个
类型属性
,这两个属性都处理访问修饰符。我知道这没有道理。我只是想更好地理解这一点。把它放在一个答案中,我很乐意接受它,
codetypedesclaration
是一个
codetypember
,因此继承了通常应用于成员的属性,这很奇怪