CodeDom 不使用 MemberAttributes

CodeDom not using MemberAttributes

在 visual studio 女士的指导下尝试学习 CodeDom 时,我注意到代码生成似乎忽略了为我的 class 设置的成员属性。

这是我的示例代码

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 对象的 Attributes 设置为 20482,这是默认值。如 here 所述,这意味着 class 应该是私有的 (20480) 和最终的 (2)。相反,class 生成为 public。 这里发生了什么?

翻阅the docs:

The TypeAttributes property indicates the TypeAttributes values for the type declaration, which indicate the type category of the type.

进一步查看 TypeAttributes

The Attributes property is a side effect of the CodeTypeDeclaration class inheriting from CodeTypeMember so that classes can be nested. The flags in the TypeAttributes property should be used instead of the flags in the Attributes property.

(强调我的)。

看来你应该申请 TypeAttributes.Sealed,等等

另请注意,“private”仅适用于嵌套 类(和成员),而“final”通常指成员:“sealed”是 类.[=15 的术语=]