【发布时间】:2018-09-09 17:47:55
【问题描述】:
我正在使用反射动态创建一个实例。
var typesTR = Assembly.GetAssembly(typeof(BGenericConfigurationClass<>)).GetTypes()
.Where(type =>
!string.IsNullOrEmpty(type.Namespace) &&
(type.Namespace == "EntitiesConfiguration"))
.Where(type => type.BaseType != null
&& type.BaseType.IsGenericType
&&
(type.BaseType.GetGenericTypeDefinition() == typeof(BGenericConfigurationClass<>) ||
type.BaseType.GetGenericTypeDefinition() == typeof(CGenericConfigurationClass<>) ));
foreach (var type in typesTR)
{
dynamic configurationInstance = Activator.CreateInstance(type);
modelBuilder.Configurations.Add(configurationInstance);
}
我的例外如下:- “无法创建 CGenericConfigurationClass`1[T] 的实例,因为 Type.ContainsGenericParameters 为真。”
【问题讨论】:
-
图片一点用都没有。请删除它并改为显示代码。另外请注意,EF 有一个内置函数来发现程序集中的
EntityTypeConfiguration类。 -
我想使用 ef 的 EntityTypeConfiguration 类来使用多级继承。给我任何想法或任何解决方案。
-
给我... 从语言上讲,这是命令式的,或“命令式”。在每个人都自愿帮助他人的 Stack Overflow 上效果不佳。其中一位志愿者正确地向您解释了您需要
type.MakeGenericType——您没有对此做出任何实质性回应。同样,我怀疑您正在尝试重新发明 EF already provides 的轮子,但您也没有对此做出回应。 -
很抱歉,如果您觉得我的选词有误,但我告诉您英语是您的母语,但我在写作前转换了每句话.. 我不是在命令我非常感谢您回复并分享我的问题解决方案,谢谢先生或兄弟采取任何你喜欢的话。我对行为错误感到内疚和羞耻...请分享我分享整个项目的Skype或电子邮件地址。
-
没有难过的感觉,只是解释一下 SO 的工作原理。但我也要求对迄今为止提出的建议作出回应。
标签: c# generics entity-framework-6