我有这样一堂课
[Description("This is a wahala class")]
public class Wahala
{
}
Run Code Online (Sandbox Code Playgroud)
反正有没有得到类的Description属性的内容Wahala?
Jon*_*eet 34
绝对 - 使用Type.GetCustomAttributes.示例代码:
using System;
using System.ComponentModel;
[Description("This is a wahala class")]
public class Wahala
{
}
public class Test
{
static void Main()
{
Console.WriteLine(GetDescription(typeof(Wahala)));
}
static string GetDescription(Type type)
{
var descriptions = (DescriptionAttribute[])
type.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (descriptions.Length == 0)
{
return null;
}
return descriptions[0].Description;
}
}
Run Code Online (Sandbox Code Playgroud)
相同类型的代码可以检索其他成员的描述,例如字段,属性等.
| 归档时间: |
|
| 查看次数: |
12617 次 |
| 最近记录: |