2 u011990549 u011990549 于 2014.05.14 21:43 提问

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
feiyun0112   Rxr 2014.05.15 09:10
已采纳

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

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!