小编ran*_*med的帖子

由于Type.ContainsGenericParameters为true,因此无法创建实例

在此处输入图片说明

我正在使用反射动态创建实例。

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);
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

而我的例外是:-“因为Type.ContainsGenericParameters为true,所以无法创建CGenericConfigurationClass`1 [T]的实例。”

c# generics entity-framework-6

3
推荐指数
1
解决办法
3797
查看次数

标签 统计

c# ×1

entity-framework-6 ×1

generics ×1