如何在我的C#方法中添加描述 [英] how to add description to my C# methods

查看:261
本文介绍了如何在我的C#方法中添加描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



i想要为我的方法添加一些描述(不是评论!),

所以我可以用方法阅读它们

how我可以实现吗?!

hi ,
i want add some Description (not Comment !) to my methods ,
So I can read them using methods
how can i achieve that?!

推荐答案

您可以使用 DescriptionAttribute类 [ ^ ]


您还可以使用 xml多行注释 [ ^ ]


您可以添加元数据(结构化描述)它们嵌入在您的C#代码中,并且可以被反映在您的C#代码中的其他程序使用代码)使用名为Attributes的工具进入.NET代码。



.NET中的属性写在方括号[]内,并在Classes,Interfaces的定义之前,结构,属性等属性声明关系。



.NET有许多预定义的属性,您可能已经看到在代码中使用了这些属性,你可以自己使用。例如,在WinForms Program.cs文件中,您将看到[STAThread]这是一个属性。



这是熟悉.NET的优秀资源预定义属性:[ ^ ]。



您可以通过创建从System.Attribute继承的密封公共类来创建自己的属性:
You can add metadata (structured descriptions which are embedded in your C# code, and meant to be used by other programs reflecting on your code) into your .NET code using the facility called "Attributes."

Attributes in .NET are written inside brackets [] and precede the definition of Classes, Interfaces, Structs, Properties, etc. Attributes declare a relationship.

.NET has many predefined Attributes you may have already seen being used in your code, and which you can use yourself. For example, in a WinForms Program.cs file, you'll see [STAThread] which is an Attribute.

This is an excellent resource for getting familiar with .NET's predefined Attributes: [^].

You create you own Attributes by creating sealed Public Classes that inherit from System.Attribute:
public sealed class MyAttribute : System.Attribute {}

Visual的更高版本Studio和.NET将自动为您插入属性模板:



1.将文本插入光标放在Visual Studio C#代码编辑窗口中,例如,一个类定义



2.输入单词属性,然后按两次Tab键:这应该出现(这里显示的例子)由.NET 4.5,VS Studio 2012生成:

Later versions of Visual Studio and .NET will automatically insert an Attribute Template for you:

1. Position the text insert cursor in a Visual Studio C# code-edit windows on the line just before, for example, a Class definition

2. Type in the word Attribute, then press the Tab key twice: this should appear (the example shown here is generated by .NET 4.5, VS Studio 2012):

[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
sealed class MyAttribute : Attribute
{
    // See the attribute guidelines at
    //  http://go.microsoft.com/fwlink/?LinkId=85236
    readonly string positionalString;

    // This is a positional argument
    public MyAttribute(string positionalString)
    {
        this.positionalString = positionalString;

        // TODO: Implement code here
        throw new NotImplementedException();
    }

    public string PositionalString
    {
        get { return positionalString; }
    }

    // This is a named argument
    public int NamedInt { get; set; }
}

一旦定义了属性类,就可以修饰代码中的那些对象,这些对象以后要被识别为使用属性名称绑定到属性括号内。



还有一种语法,用于在类和其他对象上使用的属性定义中传递数据。您可以在上面显示的代码中看到这样的示例:'NamedInt是一个命名变量,'positionalString是一个位置变量:位置变量是传递给属性构造函数的必需参数,而命名变量当然不是需要。



我建议你开始阅读CodeProject上有关使用属性的一些教程:[ ^ ]了解自己可以使用自定义属性做什么。



如果您愿意,您可以使用属性非常深入:AttributeUsage工具几乎是一种元属性,可让您操纵属性可应用于哪些对象(AttributeTargets)。 br />


您实际上可以使用Reflection.Emit在运行时创建属性,并将它们连接到您即时构建的类(我还没有看一个有用的例子。)

Once you have defined an Attribute Class, you then "decorate" those Objects in your code that you wish to later be identified as being "bound" to the Attribute with the Attribute name inside brackets.

There is also a syntax for passing data in the Attribute definition you use on your Classes and other Objects. You see examples of that in the code shown above: 'NamedInt is a "named" variable, and 'positionalString is a "positional" variable: positional variables are required parameters passed to the Attribute Constructor, and named variables are, of course, not required.

I suggest you start reading some of the tutorials on using Attributes here on CodeProject: [^] to get a sense of what you can do with custom Attributes.

You can go "very deep" with Attributes if you wish: the AttributeUsage facility is almost a kind of meta-Attribute that lets you manipulate what objects an Attribute can be applied to (AttributeTargets).

You could actually create Attributes at run-time using Reflection.Emit and wire-them-up to Classes you've built on-the-fly (I have yet to see a useful example of that).


这篇关于如何在我的C#方法中添加描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆