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+FullNameto get classes of typeFooand 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‘sFullNameis: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
propTypeis of typeDbSet<>?
Thanks.
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.