Activator.CreateInstance私人密封类 [英] Activator.CreateInstance with private sealed class

查看:303
本文介绍了Activator.CreateInstance私人密封类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想新的一个LocalCommand实例,它是一个私有的类System.Data.SqlClient.SqlCommandSet的。我似乎能够抓住类型的信息就好了:

I'm trying to new up a LocalCommand instance which is a private class of 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");

但是当我尝试初始化它Activator.CreateInstance抛出一个异常:

but Activator.CreateInstance throws an exception when I try to instantiate it:

object item = Activator.CreateInstance(localCmdType,
  new object[] { commandText, parameters, num7, commandType });

System.MissingMethodException:构造上键入'System.Data.SqlClient.SqlCommandSet + LocalCommand'未找到

System.MissingMethodException: Constructor on type 'System.Data.SqlClient.SqlCommandSet+LocalCommand' not found.

构造函数的参数匹配我的反射看到了签名。是new'ing了一个私有类与不同的CreateInstance超载或者什么支持的内部构造函数?

The constructor arguments match the signature I see in Reflector. Is new'ing up a private class with an internal ctor supported with a different CreateInstance overload or what?

推荐答案

我首先想到的是将获得 ConstructorInfo 使用 ConstructorInfo constructorInfo =类型.GetConstructor(),然后 constructorInfo.Invoke()这一点。我怀疑 Activator.CreateInstance 使它难以调用构造函数,你就不能正常访问,虽然我不记得想我自己。

My first thought would be to get the ConstructorInfo using ConstructorInfo constructorInfo = Type.GetConstructor(), and then constructorInfo.Invoke() that. 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私人密封类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆