Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用反射来确定属性上是否存在属性?_C#_Reflection - Fatal编程技术网

C# 如何使用反射来确定属性上是否存在属性?

C# 如何使用反射来确定属性上是否存在属性?,c#,reflection,C#,Reflection,类似的: 但是,当尝试收集自定义属性时,我总是得到相同的结果。空的脚本忽略 PropertyInfo[] Properties = Entity.GetType().GetProperties(); foreach (PropertyInfo Property in Properties) 调试时,这行代码 var annotes = Property.GetCustomAttributes(typeof(ScriptIgnoreAttribute), false); (我还尝试使用true

类似的:

但是,当尝试收集自定义属性时,我总是得到相同的结果。空的
脚本忽略

PropertyInfo[] Properties = Entity.GetType().GetProperties();
foreach (PropertyInfo Property in Properties)
调试时,这行代码

var annotes = Property.GetCustomAttributes(typeof(ScriptIgnoreAttribute), false);
(我还尝试使用
true

看起来像这样

annotes | {System.Web.Script.Serialization.ScriptIgnoreAttribute[0]}
public virtual Lot Lot { get; set; }
[ScriptIgnore]
public virtual ICollection<Lot> Lots { get; set; }
但是,属性是这样定义为类属性的

annotes | {System.Web.Script.Serialization.ScriptIgnoreAttribute[0]}
public virtual Lot Lot { get; set; }
[ScriptIgnore]
public virtual ICollection<Lot> Lots { get; set; }
没有附加
[ScriptIgnore]
属性。此外,当我在属性上尝试这个时,它的定义是这样的

annotes | {System.Web.Script.Serialization.ScriptIgnoreAttribute[0]}
public virtual Lot Lot { get; set; }
[ScriptIgnore]
public virtual ICollection<Lot> Lots { get; set; }
如何使用反射来确定属性是否存在?或者其他方式,如果可能的话,我也尝试过

var attri = Property.Attributes;

但它不包含任何属性。

以下代码有效:

var props = type.GetProperties()
                .Where(p => p.GetCustomAttributes().Any(a => a is ScriptIgnoreAttribute))
                .ToList();
using System.Web.Script.Serialization;
public class TestAttribute
{
  [ScriptIgnore]
  public string SomeProperty1 { get; set; }

  public string SomeProperty2 { get; set; }

  public string SomeProperty3 { get; set; }

  [ScriptIgnore]
  public string SomeProperty4 { get; set; }
}
定义静态扩展:

public static class AttributeExtension
{
  public static bool HasAttribute(this PropertyInfo target, Type attribType)
  {
    var attribs = target.GetCustomAttributes(attribType, false);
    return attribs.Length > 0;
  }
}
将以下示例代码放入一个方法中,它会正确地拾取属性-顺便说一句,包括
ICollection

  var test = new TestAttribute();
  var props = (typeof (TestAttribute)).GetProperties();
  foreach (var p in props)
  {
    if (p.HasAttribute(typeof(ScriptIgnoreAttribute)))
    {
      Console.WriteLine("{0} : {1}", p.Name, attribs[0].ToString());
    }
  }

  Console.ReadLine();

注意:如果您使用的是EF动态代理类,我认为您需要使用
ObjectContext.GetObjectType()
解析到原始类,然后才能获取属性,因为EF生成的代理类将不会继承属性。

GetCustomAttributes的重载不接受0个参数@TravisJ我在发布之前对它进行了测试,它对我有效。无论如何,请尝试
GetCustomAttributes(false)
——它确实使用
(false)
进行编译,但是它仍然是空的。我认为这可能是实体框架延迟加载的结果,而不是代码的错误。@TravisJ,我再次测试了它,我可以使用
ScriptIgnore
属性获得道具。-这是我获取类型的方式的问题。EF返回一个动态代理,反射似乎并不总是很好地处理它们;var propers=objType.GetProperties()成功了。非常感谢你。
var props = type.GetProperties()
                .Where(p => p.GetCustomAttributes().Any(a => a is ScriptIgnoreAttribute))
                .ToList();