[英]Activator.CreateInstance with private sealed class
I'm trying to new up a LocalCommand instance which is a private class of System.Data.SqlClient.SqlCommandSet.我正在尝试新建一个 LocalCommand 实例,它是 System.Data.SqlClient.SqlCommandSet 的私有类。 I seem to be able to grab the type information just fine:
我似乎能够很好地获取类型信息:
Assembly sysData = Assembly.Load("System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
localCmdType = sysData.GetType("System.Data.SqlClient.SqlCommandSet+LocalCommand");
but Activator.CreateInstance throws an exception when I try to instantiate it:但是当我尝试实例化它时 Activator.CreateInstance 抛出异常:
object item = Activator.CreateInstance(localCmdType,
new object[] { commandText, parameters, num7, commandType });
System.MissingMethodException: Constructor on type 'System.Data.SqlClient.SqlCommandSet+LocalCommand' not found. System.MissingMethodException:未找到类型“System.Data.SqlClient.SqlCommandSet+LocalCommand”的构造函数。
The constructor arguments match the signature I see in Reflector.构造函数参数与我在 Reflector 中看到的签名相匹配。 Is new'ing up a private class with an internal ctor supported with a different CreateInstance overload or what?
是新建一个私有类,内部 ctor 支持不同的 CreateInstance 重载还是什么?
My first thought would be to get the ConstructorInfo
using ConstructorInfo constructorInfo = Type.GetConstructor()
, and then constructorInfo.Invoke()
that.我的第一个想法是使用
ConstructorInfo constructorInfo = Type.GetConstructor()
获取ConstructorInfo
,然后是constructorInfo.Invoke()
。 I suspect that Activator.CreateInstance
makes it hard to call constructors you wouldn't normally have access to, although I don't remember trying it myself.我怀疑
Activator.CreateInstance
很难调用您通常无法访问的构造函数,尽管我不记得自己尝试过。
I got it to work this way:我让它以这种方式工作:
using System;
using System.Reflection;
class Test
{
public String X { get; set; }
Test(String x)
{
this.X = x;
}
}
class Program
{
static void Main()
{
Type type = typeof(Test);
ConstructorInfo c = type.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance,
null, new Type[] { typeof(String) }, null);
Object o = c.Invoke(new Object[] { "foo" });
}
}
The trick was to go after the constructor specifically with GetConstructor
rather then trying to find it in the results of GetConstructors
.诀窍是专门用
GetConstructor
构造函数,而不是尝试在GetConstructors
的结果中找到它。 Go figure.去搞清楚。
I might be a little late in responding, but I ran into a similar problem that fits into this topic.我的回复可能有点晚了,但我遇到了一个适合这个主题的类似问题。
I wanted to instantiate a non public constructor using Activator.CreateInstance
and passing it arguments.我想使用
Activator.CreateInstance
实例化一个非公共构造函数并传递它的参数。
public class Node
{
string name;
Node parent;
protected Node(string name,Node parent)
{
this.name = name;
this.parent = parent;
}
public static Node Create(string name,Node parent)
{
Node result = Activator.CreateInstance(
type: typeof(Node),
bindingAttr: BindingFlags.Instance | BindingFlags.NonPublic,
binder: null, //default binder
args: new object[] { name, parent },
culture: null);
return (Node)result;
}
}
The tricky part was the binding flags.棘手的部分是绑定标志。
My first instinct was to use BindingFlags.CreateInstance | BindingFlags.NonPublic
我的第一直觉是使用
BindingFlags.CreateInstance | BindingFlags.NonPublic
BindingFlags.CreateInstance | BindingFlags.NonPublic
, however that caused an exception to be thrown: MissingMethodException Constructor on type 'Node' not found. BindingFlags.CreateInstance | BindingFlags.NonPublic
,但是这导致抛出异常:未找到类型“节点”上的 MissingMethodException 构造函数。
Trick is to make sure to use the right CreateInstance
overload:技巧是确保使用正确的
CreateInstance
重载:
// WRONG
... Activator.CreateInstance(
type,
BindingFlags.Instance
| BindingFlags.NonPublic
);
This calls the params
overload, which will default to Instance | Public | CreateInstance
这将调用
params
重载,默认为Instance | Public | CreateInstance
Instance | Public | CreateInstance
Instance | Public | CreateInstance
, and your binding flags will be passed as arguments to the constructor, giving the vague MissingMethodException
. Instance | Public | CreateInstance
,并且您的绑定标志将作为参数传递给构造函数,给出模糊的MissingMethodException
。
Also, use Instance | Public | NonPublic
另外,使用
Instance | Public | NonPublic
Instance | Public | NonPublic
Instance | Public | NonPublic
if you aren't sure of the visibility of the constructor: Instance | Public | NonPublic
如果您不确定构造函数的可见性:
// right
... Activator.CreateInstance(
type,
BindingFlags.Instance
| BindingFlags.Public
| BindingFlags.NonPublic,
null,
new object[] { }, // or your actual constructor arguments
null
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.