【问题标题】:Issue finding custom attributes问题查找自定义属性
【发布时间】:2015-06-05 03:51:26
【问题描述】:

我正在尝试查找我的程序集中具有自定义属性的所有方法。我已经尝试过这里发布的其他解决方案,但我似乎无法获得关于哪些方法具有我正在寻找的属性类型的任何结果。希望有人能指出我做错了什么。

[AttributeUsage(AttributeTargets.All)]
public class RPCAttributeType : Attribute
{
    private string _name;
    private double _version;

    public double Version
    {
        get { return _version; }
        set { _version = value; }
    }

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    public RPCAttributeType(string name)
    {
        Name = name;
        Version = 1.0;
    }
}


    public RPCModule()
    {
        try
        {



            Assembly assembly = Assembly.ReflectionOnlyLoad("Parser");


            var results = CustomAttributeData.GetCustomAttributes(assembly);

            ShowAttributeData(results);


        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);

        }

    }

    [RPCAttributeType("Select")]
    public DataRow[] Select(string expression)
    {
        return MessageDataTable.Select(expression);
    }

【问题讨论】:

  • “我已经尝试了其他解决方案...” 想告诉我们哪个?我想出了 2 打不同的,我什至没有缩小我的标准。这个怎么样? stackoverflow.com/questions/3467765/…

标签: c# reflection attributes


【解决方案1】:

您可以这样做以使用 RPCAttributeType 获取所有方法的列表

var results = assembly.GetTypes().SelectMany(t => t.GetMethods())
.Where(m => m.GetCustomAttributes(typeof(RPCAttributeType), true).Count() > 0).ToList();

编辑:基于评论

分配程序集使用时

var assembly = Assembly.GetExecutingAssembly();

【讨论】:

  • 反过来。它获取方法而不是类型,并且问题清楚地要求“程序集中具有自定义属性的所有方法”
  • 当我尝试您的解决方案时,它会抛出 InvalidOperationException 并出现以下错误:"It is illegal to reflect on the custom attributes of a Type loaded via ReflectionOnlyGetType (see Assembly.ReflectionOnly) -- use CustomAttributeData instead."
  • 使用程序集 = Assembly.GetExecutingAssenbly()
  • 是的,如果使用var assembly = Assembly.GetExecutingAssenbly();,它就可以工作。您可以将其添加到您的答案中。此外,要获得特定的程序集,var assembly = typeof(Parser).Assembly; 可以工作 - 使用类名为“Parser”的 OP 示例。
【解决方案2】:

您的代码仅检查程序集级属性。

由于您的属性可以应用于类、方法、属性等,因此您需要通过反射遍历所有这些内容以定位您的属性。

在概念上类似于...

foreach assembly
    foreach type
        foreach property
        foreach method
        foreach field

但是,您的问题是“在我的程序集中查找所有方法”,因此如果您只打算将此属性应用于方法,那么您应该更改

[AttributeUsage(AttributeTargets.All)]

到:

[AttributeUsage(AttributeTargets.Method)]

然后您可以使用 Praveen 的解决方案来检查所有类型的方法,而不用担心属性、字段等。

【讨论】:

    【解决方案3】:

    使用原例子的ReflectionOnlyLoad,可以这样:

    Assembly assembly = Assembly.ReflectionOnlyLoad("Parser");
    var results = assembly.GetTypes()
                  .SelectMany(t => t.GetMethods())
                  .SelectMany(m => CustomAttributeData.GetCustomAttributes(m)
                  .Where(c => c.AttributeType.FullName == typeof(RPCAttributeType).FullName))
                  .ToList();
    ShowAttributeData(results);
    

    注意:这将使用指定自定义属性的CustomAttributeData 列表填充results 变量,就像原始代码示例一样,而不是包含原始问题中描述的自定义属性的方法的MethodInfo 列表.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多