Problem with reflection

S

Skandy

Hello All:

I'm having this little trouble getting a form displayed using
reflection.

objForm.GetType().GetMethod("ShowDialog",
BindingFlags.InvokeMethod).Invoke(objForm, null);

The above is the line of code that is causing this trouble. I'm getting
a NullreferenceException at this line.

I have debugged and checked that objForm is fine and is not null, and
the MethodInfo is correct too and is not null again.
Once I get the MethodInfo I try the Invoke(object, params);

Because the ShowDialog method for the Form class doesnt accept any
parameters, I pass a null value here.

Can anyone point why I'm getting this NullReferenceException?

Thanks In Advance.

Skandy.
 
Y

Yury

I checked your code and MethodInfo is null. Use other overload of
GetMethod method to find ShowDialog:

Type t = objForm.GetType();
MethodInfo m = t.GetMethod("ShowDialog", new Type[0]);
m.Invoke(objForm, null);
 
S

Skandy

Hi Yury, Thanks for the reply.

Yes I relaized it just now, that the MethodInfo is returned NULL and
that explains why I get a NullreferenceException.

But why is that I get the following exception against this code:
MethodInfo mi = objForm.GetType().GetMethod("Show");

Form is not null
SmartClientAssembly.frmDisplay, Text: Reference Implementation for
Smart Clients

Ambiguous match found.
mscorlib


at System.RuntimeType.GetMethodImpl(String name, BindingFlags
bindingAttr, Bi
nder binder, CallingConventions callConv, Type[] types,
ParameterModifier[] modi
fiers)
at System.Type.GetMethod(String name)
at ReferenceImplementationSmartClients.Program.Main(String[] args)
in Z:\Deve
lopment\RC1\ReferenceImplementationSmartClients\ReferenceImplementationSmartClie
nts\Program.cs:line 34
Press any key to continue . . .

TIA.
Skandy
 
Y

Yury

You get this error because runtime doesn't know what method you need
Show(IWin32Windows) or Show without parameters. So you need to specify
parameters types to help runtime to resolve method. If you need Show()
just pass empty array of Type as second parameter of GetMethod like in
first case.

GetMethod("Show", new Type[0]);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top