9

可能重复:
查找具有特定属性的所有类

在程序集中,我想获取特定类属性的所有实例。换句话说,我想拥有具有特定属性的类列表。

通常,您将拥有一个可以使用该GetCustomAttributes方法获取属性的类。

是否有可能列出谁具有特定属性?

4

4 回答 4

4
public static IEnumerable<Type> GetTypesWithMyAttribute(Assembly assembly)
{
    foreach(Type type in assembly.GetTypes())
    {
        if (Attribute.IsDefined(type, typeof(MyAttribute)))
            yield return type;
    }
}

或者:

public static List<Type> GetTypesWithMyAttribute(Assembly assembly)
{
    List<Type> types = new List<Type>();

    foreach(Type type in assembly.GetTypes())
    {
        if (type.GetCustomAttributes(typeof(MyAttribute), true).Length > 0)
            types.Add(type);
    }

    return types;
}

Linq VS 我的方法基准(100000 次迭代):

Round 1
My Approach:     2088ms
Linq Approach 1: 7469ms
Linq Approach 2: 2514ms

Round 2
My Approach:     2055ms
Linq Approach 1: 7082ms
Linq Approach 2: 2149ms

Round 3
My Approach:     2058ms
Linq Approach 1: 7001ms
Linq Approach 2: 2249ms

基准代码:

[STAThread]
public static void Main()
{
    List<Type> list;

    Stopwatch watch = Stopwatch.StartNew();

    for (Int32 i = 0; i < 100000; ++i)
        list = GetTypesWithMyAttribute(Assembly.GetExecutingAssembly());

    watch.Stop();

    Console.WriteLine("ForEach: " + watch.ElapsedMilliseconds);

    watch.Restart();

    for (Int32 i = 0; i < 100000; ++i)
        list = GetTypesWithMyAttributeLinq1(Assembly.GetExecutingAssembly());

    Console.WriteLine("Linq 1: " + watch.ElapsedMilliseconds);

    watch.Restart();

    for (Int32 i = 0; i < 100000; ++i)
        list = GetTypesWithMyAttributeLinq2(Assembly.GetExecutingAssembly());

    Console.WriteLine("Linq 2: " + watch.ElapsedMilliseconds);

    Console.Read();
}

public static List<Type> GetTypesWithMyAttribute(Assembly assembly)
{
    List<Type> types = new List<Type>();

    foreach (Type type in assembly.GetTypes())
    {
        if (Attribute.IsDefined(type, typeof(MyAttribute)))
            types.Add(type);
    }

    return types;
}

public static List<Type> GetTypesWithMyAttributeLinq1(Assembly assembly)
{
    return assembly.GetTypes()
               .Where(t => t.GetCustomAttributes().Any(a => a is MyAttribute))
               .ToList();
}

public static List<Type> GetTypesWithMyAttributeLinq2(Assembly assembly)
{
    return assembly.GetTypes()
               .Where(t => Attribute.IsDefined(t, typeof(MyAttribute)))
               .ToList();
}
于 2013-01-21T14:15:54.250 回答
2

您可以使用反射来做到这一点。这将为您提供List<Type>当前程序集中所有具有MyAttribute.

using System.Linq;
using System.Reflection;

// ...

var asmbly = Assembly.GetExecutingAssembly();
var typeList = asmbly.GetTypes().Where(
        t => t.GetCustomAttributes(typeof (MyAttribute), true).Length > 0
).ToList();
于 2013-01-21T14:17:32.983 回答
1
var list = asm.GetTypes()
            .Where(t => t.GetCustomAttributes().Any(a => a is YourAttribute))
            .ToList();
于 2013-01-21T14:16:53.490 回答
0

没有代码示例,假设您有一个List<Type>或一个Assembly.

public List<Type> TypesWithAttributeDefined(Type attribute)
{
  List<Type> types = assembly.GetTypes();
  return types.Where(t => Attribute.IsDefined(t, attribute)).ToList();
}
于 2013-01-21T14:20:50.927 回答