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();
}
}
}
}
...全文
247 2 打赏 收藏 转发到动态 举报
写回复
用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
  • 打赏
  • 举报
回复
大神请解释为何出现这种情况。谢谢!
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

110,538

社区成员

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

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

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