首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CodeDom不使用MemberAttributes

CodeDom不使用MemberAttributes
EN

Stack Overflow用户
提问于 2020-07-10 09:19:11
回答 1查看 200关注 0票数 0

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

这是我的示例代码

代码语言:javascript
复制
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();
        }
    }
}

它产生以下输出:

代码语言:javascript
复制
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 {
    }
}

如您所见,Attributes对象的class1设置为20482,这是默认值。正如所描述的这里,这意味着类应该是私有的(20480)和最终的(2)。相反,类是以公共形式生成的。这里发生了什么事?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-10 09:38:37

从一部电影到医生们

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

再看TypeAttributes

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

(强调我的)。

所以看起来你应该应用TypeAttributes.Sealed等等。

还要注意的是,“私有”只适用于嵌套类(和成员),而“最终”通常指的是成员:“密封”是类的术语。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62831106

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档