我正在使用反射动态创建实例。
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]的实例。”