使用反射的 C# 类型比较
我想使用反射检查属性是否属于 DbSet
类型。
public class Foo
{
public DbSet<Bar> Bars { get; set; }
}
通过使用反射:
var types = Assembly.GetExecutingAssembly().GetTypes();
foreach (var type in types)
{
if (type.IsSubclassOf(typeof (Foo)) || type.FullName == typeof (Foo).FullName)
{
foreach (
var prop in Type.GetType(type.FullName).
GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance))
{
var propType = prop.PropertyType;
bool a = propType.IsAssignableFrom(typeof (DbSet<>));
bool b = typeof (DbSet<>).IsAssignableFrom(propType);
bool c = propType.BaseType.IsAssignableFrom(typeof (DbSet<>));
bool d = typeof (DbSet<>).IsAssignableFrom(propType.BaseType);
bool e = typeof (DbSet<>).IsSubclassOf(propType);
bool f = typeof (DbSet<>).IsSubclassOf(propType.BaseType);
bool g = propType.IsSubclassOf(typeof (DbSet<>));
bool h = propType.BaseType.IsSubclassOf(typeof (DbSet<>));
bool i = propType.BaseType.Equals(typeof (DbSet<>));
bool j = typeof (DbSet<>).Equals(propType.BaseType);
bool k = propType.Name == typeof (DbSet<>).Name;
}
}
}
是否有合并的解决方案来检查类型?如您所见,我使用
IsSubClassOf
+FullName
来获取Foo
类型的类以及从派生的任何其他类Foo
。除 c、f、k 之外的所有检查(a 到 j)均返回 false。 c,f 将 System.Object 作为 BaseType 返回,这对我来说没有用。 k,我认为检查不安全。但如果没有找到其他解决方法,这将是我使用的。在调试模式下,
propType
的FullName
为:System.Data.Entity.DbSet`1[[Console1.Bar, ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
是否还有其他方法可以检查
propType
是否为DbSet
类型?
谢谢。
I'd like to check if a property is of type DbSet<T>
using reflection.
public class Foo
{
public DbSet<Bar> Bars { get; set; }
}
By using reflection:
var types = Assembly.GetExecutingAssembly().GetTypes();
foreach (var type in types)
{
if (type.IsSubclassOf(typeof (Foo)) || type.FullName == typeof (Foo).FullName)
{
foreach (
var prop in Type.GetType(type.FullName).
GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance))
{
var propType = prop.PropertyType;
bool a = propType.IsAssignableFrom(typeof (DbSet<>));
bool b = typeof (DbSet<>).IsAssignableFrom(propType);
bool c = propType.BaseType.IsAssignableFrom(typeof (DbSet<>));
bool d = typeof (DbSet<>).IsAssignableFrom(propType.BaseType);
bool e = typeof (DbSet<>).IsSubclassOf(propType);
bool f = typeof (DbSet<>).IsSubclassOf(propType.BaseType);
bool g = propType.IsSubclassOf(typeof (DbSet<>));
bool h = propType.BaseType.IsSubclassOf(typeof (DbSet<>));
bool i = propType.BaseType.Equals(typeof (DbSet<>));
bool j = typeof (DbSet<>).Equals(propType.BaseType);
bool k = propType.Name == typeof (DbSet<>).Name;
}
}
}
Is there a merged solution to check the type? As you can see, I'm using
IsSubClassOf
+FullName
to get classes of typeFoo
and any other class which is derived fromFoo
.all the checks (a to j) except c,f,k return false. c,f return System.Object as BaseType which is no use for me. k, I consider an unsafe check. But will be my what I use if no other workaround is found. In debug mode, the
propType
'sFullName
is:System.Data.Entity.DbSet`1[[Console1.Bar, ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
Is there any other way to check if
propType
is of typeDbSet<>
?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否也需要它来处理
DbSet
的子类?如果没有,您可以使用:完整示例:
对于子类,您只需要在继承层次结构中递归地应用相同的测试。如果您需要测试接口,这会变得更加痛苦。
Do you need it to cope with subclasses of
DbSet<>
as well? If not, you can use:Full sample:
With subclasses you'd just need to apply the same test recursively up the inheritance hierarchy. It becomes more of a pain if you need to test for interfaces.
您的代码的问题在于它假设
typeof(DbType<>)
表示常规类型。它不是普通类型:相反,它是泛型类型定义。这就是为什么IsAssignableFrom
、IsSubclassOf
等不起作用。如果满足以下条件,Type
x
的类型为DbSet
:The problem with your code is that it is assuming that
typeof(DbType<>)
denotes a regular type. It is not a normal type: rather, it is a generic type definition. That is why theIsAssignableFrom
,IsSubclassOf
etc. are not going to work.Type
x
is of typeDbSet<T>
if the following condition is true:我建议进行以下类型比较(对我来说效果很好):
对于可为 null 的类型(例如
DateTime?
),您可以使用这种类型的比较:I'd suggest the following type comparision (worked fine for me):
For nullable types (for example
DateTime?
) you might use this type of comparison: