471,325 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

Problem with reflection

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.

Feb 19 '06 #1
5 6440
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);

Feb 19 '06 #2
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(S tring[] args)
in Z:\Deve
lopment\RC1\ReferenceImplementationSmartClients\Re ferenceImplementationSmartClie
nts\Program.cs:line 34
Press any key to continue . . .

TIA.
Skandy

Feb 19 '06 #3
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]);

Feb 19 '06 #4
Thanks a lot, Yury

Feb 19 '06 #5
Thanks a lot, Yury

Feb 19 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Nicolas | last post: by
1 post views Thread by Dominique Vandensteen | last post: by
2 posts views Thread by Marco | last post: by
2 posts views Thread by Andrea V.F. | last post: by
15 posts views Thread by satankidneypie | last post: by
2 posts views Thread by =?Utf-8?B?UmVuYXVkIExhbmdpcw==?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.