Determine if Generic Class Implements Type
I'm working on a sharp-architecture project that mixes fluent mappings and auto mappings. A basic sharp-architecture project already has a method (AutoMappingConfiguration.ShouldMap) that determines if a type should be automatically mapped or not. Mine currently looks like this:
public override bool ShouldMap(System.Type type)
{
if (type == typeof(ActiveUser))
return false;
return type.GetInterfaces().Any(x =>
x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IEntityWithTypedId<>));
}
Essentially the type ActiveUser is mapped using a fluent mapping and everything else is mapped using auto mapping, except for the generic base classes of course. I'm at the point where I will be adding more fluently mapped classes and really don't want to keep adding if statements to this method to exclude them. I basically need the method
bool ShouldMap(System.Type type)
to return true if the generic class
ClassMap<type>
exists.
Any suggestions开发者_如何学Go?
You can create the generic type using Type.MakeGenericType so assuming you have an assembly which contains all the mappings you can do:
public bool ShouldMap(Assembly mappingAssembly, Type type)
{
Type classMapType = typeof(ClassMap<>).MakeGenericType(type);
return mappingAssembly.GetTypes().Any(t => t.IsSubclassOf(classMapType));
}
have you considered mapping ALL of the classes, but using a Mapping*Override* for the classes you want to map explicitly (instead of regular fluent mapping)?
精彩评论