确定泛型类是否实现类型
我正在开发一个混合流畅映射和自动映射的清晰架构项目。一个基本的锐架构项目已经有一个方法(AutoMappingConfiguration.ShouldMap)来确定类型是否应该自动映射。我的目前看起来像这样:
public override bool ShouldMap(System.Type type)
{
if (type == typeof(ActiveUser))
return false;
return type.GetInterfaces().Any(x =>
x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IEntityWithTypedId<>));
}
本质上,类型 ActiveUser 是使用流畅映射进行映射的,其他所有内容都是使用自动映射进行映射的,当然通用基类除外。我现在将添加更流畅的映射类,并且真的不想继续向此方法添加 if 语句来排除它们。 存在,我基本上需要返回 true 的方法
bool ShouldMap(System.Type type)
如果泛型类
ClassMap<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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 Type.MakeGenericType 创建泛型类型假设您有一个包含您可以执行的所有映射的程序集:
You can create the generic type using Type.MakeGenericType so assuming you have an assembly which contains all the mappings you can do:
您是否考虑过映射所有类,但使用映射*覆盖* 对于您想要显式映射的类(而不是常规的流畅映射)?
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)?