Is it possible to obtain class summary at runtime?

后端 未结 6 1020
無奈伤痛
無奈伤痛 2020-12-10 02:34

Is it possible to obtain class summary at runtime in C#? I would like to obtain class summary through reflection and then write it to console. By class summary I mean summar

6条回答
  •  抹茶落季
    2020-12-10 03:11

    You cannot access those at runtime because those are considered to be comments by the compiler.

    However, if you wanted to use an Attribute to specify information and access it during runtime via reflection you could do that.

    See Creating Custom Attributes (C# Programming Guide) for attribute creation and Accessing Attributes With Reflection (C# Programming Guide) for runtime access.

    Example from MSDN:

    Author.cs:

    public class Author : System.Attribute
    {
        private string name;
        public double version;
    
        public Author(string name)
        {
            this.name = name;
            version = 1.0;
        }
    }
    

    SampleClass.cs:

    [Author("H. Ackerman", version = 1.1)]
    class SampleClass
    {
        // H. Ackerman's code goes here...
    }
    

提交回复
热议问题