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:
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.
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
, however that caused an exception to be thrown: MissingMethodException Constructor on type 'Node' not found.