Moh*_*nde 3 c# reflection recursion attributes properties
以此示例类为例:
[AttributeUsage(AttributeTargets.All, AllowMultiple=true)]
public class BugFixAttribute : System.Attribute
{
public int BugId { get; private set; }
public string Programmer { get; private set; }
public DateTime Date { get; private set; }
public string Comments { get; set; }
public string RefersTo { get; set; }
public BugFixAttribute(int bugId = 0, string programmer = "")
{
this.BugId = bugId;
this.Programmer = programmer;
Date = DateTime.Now;
}
}
Run Code Online (Sandbox Code Playgroud)
我想回避使用的属性,如:
object[] attr = info.GetCustomAttributes(typeof(BugFixAttribute), false);
foreach (object attribute in attr)
{
BugFixAttribute bfa = (BugFixAttribute) attribute;
Debug.WriteLine(string.Format("\nBugId: {0}", bfa.BugId));
Debug.WriteLine(string.Format("Programmer: {0}", bfa.Programmer));
//...
}
Run Code Online (Sandbox Code Playgroud)
因为我需要做的是将这些打印到文件中.那么我怎样才能通过属性进行递归而不是Debug.WriteLine()通过所有属性,是否有办法或者我必须将其写出来.
我建议这可能不是属性的一个很好的用途,因为它混淆了附加到代码的元.如果您希望标准化获取有关错误修复的此类信息的方法,我建议您提供XML注释标记,然后为您的项目启用XML注释,并使用它.
语法示例:
/// <summary>This Method Does Something</summary>
/// <BugFix BugId="1234" Programmer="Bob" Date="2/1/2010">Fix Comments</BugFix>
public void MyMethod()
{
// Do Something
}
Run Code Online (Sandbox Code Playgroud)