Attribute.GetCustomAttribute() 返回为null

叶落_下 2014-05-14 09:46:37

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();
}
}
}
}
...全文
340 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
gomoku 2014-05-15
  • 打赏
  • 举报
回复
LastModifiedAttribute只能修饰到类和方法,而不能修饰到assembly。 因此,Attribute.GetCustomAttribute(Assembly, Type)不能得到任何东西。 要得到Vector类的LastModified,可以: Type vectorType = theAssembly.GetType("VectorClass.Vector"); Attribute modifiedAttibute = vectorType.GetCustomAttributes(false).OfType<LastModifiedAttribute>().FirstOrDefault(); 要得到assembly的attribute,可以: 1、目标assembly要有特性标志: [assembly:SupportWhatsNew] 2、获得assembly级别的特性: Attribute whatsNew = Attribute.GetCustomAttribute(theAssembly, typeof(SupportWhatsNewAttribute))
叶落_下 2014-05-14
  • 打赏
  • 举报
回复
大神请解释为何出现这种情况。谢谢!

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧