程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> attribute- Attribute.GetCustomAttribute() 返回值為空

attribute- Attribute.GetCustomAttribute() 返回值為空

編輯:編程綜合問答
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();
        }
    }
}

}

最佳回答:


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

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved