我正在尝试创建与调用类位于同一程序集中的内部类的实例。不幸的是,这对我不起作用。我正在使用以下代码段:
Assembly businessAssembly = this.GetType().Assembly;
var concreteValidator = businessAssembly.CreateInstance(t.NotificationType.ValidatorClassName, true,
BindingFlags.Default | BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.NonPublic,
null,
new Object[] { t },
null,
null);
请注意,t.NotificationType.ValidatorClassName
的格式为Namespace.ClassName
。
不应该做那个工作吗?我哪里出错了?
答案 0 :(得分:0)
如果类是 public internal且构造函数是public,则CreateInstance
方法缺少公共绑定标志:
BindingFlags.Public
哪个会成为:
Assembly businessAssembly = this.GetType().Assembly;
var concreteValidator = businessAssembly.CreateInstance(t.NotificationType.ValidatorClassName, true,
BindingFlags.Default | BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
null,
new Object[] { t },
null,
null);