如图中我仅仅知道该类的特性[Table("CU")]
请问我怎么获得这个类呢?
1.遍历运行程序集下的所有类 Assembly.GetExecutingAssembly().GetTypes()
2.获取类下面的所有特性,看是否包含这个特性,如果有就获取了这个类型 Type.GetCustomAttributes(Type, Boolean)
核心就是上面两个方法,第一个会得到一个Type数组 然后遍历这个数组用第二个方法获取这个Type的 “Table”特性,能找到的话就找到了这个Type
C
D
Press any key to continue . . .
1.遍历运行程序集下的所有类 Assembly.GetExecutingAssembly().GetTypes()
2.获取类下面的所有特性,看是否包含这个特性,如果有就获取了这个类型 Type.GetCustomAttributes(Type, Boolean)
核心就是上面两个方法,第一个会得到一个Type数组 然后遍历这个数组用第二个方法获取这个Type的 “Table”特性,能找到的话就找到了这个Type
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace Q694521
{
class TableAttribute : Attribute
{
}
class A
{
}
class B
{
}
[Table]
class C
{
}
[Table]
class D
{
}
class Program
{
static void Main(string[] args)
{
var query = Assembly.GetAssembly(typeof(Program)).GetTypes().Where(x => x.GetCustomAttributes(typeof(TableAttribute), false).Count() > 0);
foreach (var item in query)
Console.WriteLine(item.Name);
}
}
}