叶落_下 2014-05-14 13:43 采纳率: 100%
浏览 3349
已采纳

Attribute.GetCustomAttribute() 返回值为空

namespace LookUpWhatsNew
{
internal class WhatsNewChecker
{
private static readonly StringBuilder outPutText = new StringBuilder();
private static DateTime backDateTo = new DateTime(2010,2,1);

    static void Main(string[] args)
    {
        Assembly theAssembly = Assembly.Load("VectorClass");
        Attribute supportsAttibute = Attribute.GetCustomAttribute(theAssembly, typeof(LastModifiedAttribute));//  supportsAttibute 为空!      
        Conso.Read();

namespace WhatNewAttributes
{
[AttributeUsage(AttributeTargets .Class |AttributeTargets.Method ,AllowMultiple =true ,Inherited =false )]
public class LastModifiedAttribute:Attribute
{
private readonly DateTime dateModified;
private readonly string changes;
public LastModifiedAttribute(string dateModified, string changes)
{
this.dateModified = DateTime.Parse(dateModified);
this.changes = changes;
}
public DateTime DateModified
{
get
{
return this.dateModified;
}
}
public string Changes
{
get
{
return this.changes;
}
}
public string Issues { get; set; }
}
[AttributeUsage(AttributeTargets.Assembly)]
public class SupportWhatsNewAttribute : Attribute
{
}
}

namespace VectorClass
{
[LastModifiedAttribute("14,Feb,2010", "IEnumerable interface implemented so vector can now be treated as a Collection")]
[LastModifiedAttribute("10,Feb,2010", "IFormattable interface implemented so vector now responds to format specifiers N and VE")]
public class Vector:IEnumerable ,IFormattable
{
public double x, y, z;
public Vector(double x,double y,double z)
{
this.x = x;
this.y = y;
this.z = z;
}

    [LastModifiedAttribute("10,Feb,2010", "Method added in order to formating support")]
    public string ToString(string format, IFormatProvider formatProvider)
    {
        if (format == null)
        {
            return ToString();
        }

        switch (format)
        {
            case "N":
                return string.Format("({0},{1},{2})", x, y, z);
            case "VE":
                return string.Format("{0}i+{1}j+{2}k", x, y, z);
            default:
                throw new ArgumentException("No {0} format !", format);
        }
    }

    public string ToString(string format)
    {
        return ToString(format, null);
    }

    public IEnumerator GetEnumerator()
    {
        yield return x;
        yield return y;
        yield return z;
    }
    [LastModifiedAttribute("14,Feb,2010", "Class create as part of collection support for Vector")]
    private class VectorIEnumerator : IEnumerator
    {

        public object Current
        {
            get { throw new NotImplementedException(); }
        }

        public bool MoveNext()
        {
            throw new NotImplementedException();
        }

        public void Reset()
        {
            throw new NotImplementedException();
        }
    }
}

}

  • 写回答

1条回答

  • feiyun0112 2014-05-15 01:10
    关注

    foreach (Type type in theAssembly.GetTypes())
    {
    LastModifiedAttribute supportsAttibute = type.GetCustomAttributes(false)
    .Cast()
    .SingleOrDefault();
    if (supportsAttibute != null)
    {
    Console.WriteLine((supportsAttibute.DateModified);
    }

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?