Reflection, create an instance during runtime on CF

  • Thread starter Thread starter stony via .NET 247
  • Start date Start date
S

stony via .NET 247

I try to create a new form instance during runtime using the compact framework. My constructor has an argument. It is possible
to compile it, but during runtime I get following exception: 'System.NotSupportedException'.

The code:

Type type = Type.GetType( "WMSRF.MyForm", true );
object[] args = new Object[] {myArgument};
Form x1 =(Form)type.InvokeMember(null, BindingFlags.Instance |
BindingFlags.Public | BindingFlags.CreateInstance, null,
null, args, null, null, null);
x1.show();


Thanks for your help.
stony
 
stony via .NET 247 ha scritto:

Type type = Type.GetType( "WMSRF.MyForm", true );
object[] args = new Object[] {myArgument};
Form x1 =(Form)type.InvokeMember(null, BindingFlags.Instance |
BindingFlags.Public | BindingFlags.CreateInstance, null,
null, args, null, null, null);
x1.show();

Try removing the BindingFlags.Instance: I don't think the constructor is
an instance method :)

Also, you can use the type.GetConstructor().Invoke()

Bye
 
The only way I've tried to create objects dynamically at run time with the
CF is in the example below. The problem is that it only works if the class
has a public parameterless constructor. I haven't found any way with the CF
to invoke a constructer with parameters for a particular Type determined at
run time, although the full framework supports doing so. If in fact, the CF
supports calling constructors with parameters, I'd like to see any example
someone might offer. Hope this helps:

Dave

Type type = Type.GetType("WMSRF.MyForm");
if(type.IsSubclassOf(typeof(Form)))
{
Form x1 =(Form) System.Activator.CreateInstance(type);
x1.show();
}


stony via .NET 247 said:
I try to create a new form instance during runtime using the compact
framework. My constructor has an argument. It is possible
to compile it, but during runtime I get following exception: 'System.NotSupportedException'.

The code:

Type type = Type.GetType( "WMSRF.MyForm", true );
object[] args = new Object[] {myArgument};
Form x1 =(Form)type.InvokeMember(null, BindingFlags.Instance |
BindingFlags.Public | BindingFlags.CreateInstance, null,
null, args, null, null, null);
x1.show();


Thanks for your help.
stony
 
Back
Top