加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 通过类的属性进行递归

发布时间:2020-12-15 23:54:41 所属栏目:百科 来源:网络整理
导读:以此示例类为例: [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; pr
以此示例类为例:

[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;
    }
}

我想回避使用的属性,如:

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));
    //...
}

因为我需要做的是将这些打印到文件中.那么我如何通过属性递归而不是通过所有这些来执行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
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读