【发布时间】:2019-04-29 11:18:51
【问题描述】:
我正在尝试获取具有特定属性和属性的枚举参数的所有类的列表。
在下面查看我的示例。我知道如何获取属性为GenericConfig 的所有类,但是如何过滤参数?
namespace ConsoleApp1
{
internal class Program
{
private static void Main(string[] args)
{
// get me all classes with Attriubute GenericConfigAttribute and Parameter Type1
var type1Types =
from type in Assembly.GetExecutingAssembly().GetTypes()
where type.IsDefined(typeof(GenericConfigAttribute), false)
select type;
Console.WriteLine(type1Types);
}
}
public enum GenericConfigType
{
Type1,
Type2
}
// program should return this class
[GenericConfig(GenericConfigType.Type1)]
public class Type1Implementation
{
}
// program should not return this class
[GenericConfig(GenericConfigType.Type2)]
public class Type2Implementation
{
}
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class GenericConfigAttribute : Attribute
{
public GenericConfigType MyEnum;
public GenericConfigAttribute(GenericConfigType myEnum)
{
MyEnum = myEnum;
}
}
}
【问题讨论】:
-
您不能直接将特定类型投射到顶部并访问其成员吗?我不明白您所说的“过滤参数”是什么意思。
标签: c# reflection