Activator.CreateInstance with param - 未找到 child class 类型的构造函数
Activator.CreateInstance with param - constructor of child class type not found
我尝试将 connectionString 传递给上下文工厂构造函数,但出现错误 "constructor type of ContextImplementationTest not found" 有什么问题?
我打电话给
string bid="someCOnnectionString";
using (BaseContext<DbContext> db = MediaDbContext.GetBidContext(bid)){...}
public static class MediaDbContext
{
public static BaseContext<DbContext> GetBidContext(string connString)
{
//Settings.ContextFactory="ContextImplementationTest"
Type contextFactoryType = Type.GetType("Media.DB.Context.Implementation." + Settings.ContextFactory);
object[] args = new object[] { connString};
BaseContext<DbContext> instance = (BaseContext<DbContext>)Activator.CreateInstance(contextFactoryType, args);
return instance;
}
}
基础Class
public class BaseContext<TContext> : DbContext where TContext : DbContext
{
static BaseContext()
{
Database.SetInitializer<TContext>(null);
}
protected BaseContext(string connectionString) : base(connectionString)
{
}...
Child class
public class ContextImplementationTest : BaseContext<DbContext>
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{...}
更新:要调用 child class 中的构造函数,您需要在此 class 中有构造函数。
Child class
public class ContextImplementationTest : BaseContext<DbContext>
{
protected ContextImplementationTest(string connectionString) : base(connectionString)
{
}...
Activator.CreateInstance 仅在默认构造函数调用的情况下采用 nonPublic
参数。
您可以使用 Type.GetConstructor 代替:
Type contextFactoryType =
Type.GetType("Media.DB.Context.Implementation." + Settings.ContextFactory);
ConstructorInfo ci = contextFactoryType .GetConstructor(
BindingFlags.Instance | BindingFlags.NonPublic,
binder: null, new Type[] { typeof(string) }, modifiers: null);
object[] args = new object[] { connString };
var instance = (BaseContext<DbContext> instance)ci.Invoke(args);
return instance;
我尝试将 connectionString 传递给上下文工厂构造函数,但出现错误 "constructor type of ContextImplementationTest not found" 有什么问题? 我打电话给
string bid="someCOnnectionString";
using (BaseContext<DbContext> db = MediaDbContext.GetBidContext(bid)){...}
public static class MediaDbContext
{
public static BaseContext<DbContext> GetBidContext(string connString)
{
//Settings.ContextFactory="ContextImplementationTest"
Type contextFactoryType = Type.GetType("Media.DB.Context.Implementation." + Settings.ContextFactory);
object[] args = new object[] { connString};
BaseContext<DbContext> instance = (BaseContext<DbContext>)Activator.CreateInstance(contextFactoryType, args);
return instance;
}
}
基础Class
public class BaseContext<TContext> : DbContext where TContext : DbContext
{
static BaseContext()
{
Database.SetInitializer<TContext>(null);
}
protected BaseContext(string connectionString) : base(connectionString)
{
}...
Child class
public class ContextImplementationTest : BaseContext<DbContext>
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{...}
更新:要调用 child class 中的构造函数,您需要在此 class 中有构造函数。
Child class
public class ContextImplementationTest : BaseContext<DbContext>
{
protected ContextImplementationTest(string connectionString) : base(connectionString)
{
}...
Activator.CreateInstance 仅在默认构造函数调用的情况下采用 nonPublic
参数。
您可以使用 Type.GetConstructor 代替:
Type contextFactoryType =
Type.GetType("Media.DB.Context.Implementation." + Settings.ContextFactory);
ConstructorInfo ci = contextFactoryType .GetConstructor(
BindingFlags.Instance | BindingFlags.NonPublic,
binder: null, new Type[] { typeof(string) }, modifiers: null);
object[] args = new object[] { connString };
var instance = (BaseContext<DbContext> instance)ci.Invoke(args);
return instance;