我想查一个特定的运行时类型是否包含有特定属性的这样的特性:C#反射检查属性
public void Audit(MongoStorableObject oldVersion, MongoStorableObject newVersion)
{
if(oldVersion.GetType() != newVersion.GetType())
{
throw new ArgumentException("Can't Audit versions of different Types");
}
foreach(var i in oldVersion.GetType().GetProperties())
{
//The statement in here is not valid, how can I achieve look up of a particular attribute
if (i.GetCustomAttributes().Contains<Attribute>(MongoDB.Bson.Serialization.Attributes.BsonIgnoreAttribute)) continue;
//else do some actual auditing work
}
}
但声明是无效的,你能告诉我如何实现查找这样的属性的特定属性?谢谢,
更新:
我发现这个不使智能感知抱怨:
if (i.GetCustomAttributes((new MongoDB.Bson.Serialization.Attributes.BsonIgnoreAttribute()).GetType(),false).Length > 0) continue;
但我还是不能肯定这将做什么,我想这一点。
GetCustomAttribute是否需要参数?假也许? intellisense抱怨没有没有参数的重载。 – 0xor1
看起来像.NET 4.5并不需要它。使用true从基类继承属性。 –