-
Invoking a static method with Reflection
I like to invoke a static method "getInstance" on a class using C#. Any ideas?
This won't work...
Assembly a = Assembly.LoadFrom("Reflection.dll");
Type t = a.GetType("Reflection.Singleton");
MethodInfo mi = t.GetMethod("getInstance");
object o = mi.Invoke(t, new object[]{});
Thanks
/Staffan
-
Re: Invoking a static method with Reflection
In article <3ec114e5$1@tnews.web.devx.com>, st_rosengren@hotmail.com
says...
>
> I like to invoke a static method "getInstance" on a class using C#. Any ideas?
> This won't work...
>
> Assembly a = Assembly.LoadFrom("Reflection.dll");
> Type t = a.GetType("Reflection.Singleton");
> MethodInfo mi = t.GetMethod("getInstance");
> object o = mi.Invoke(t, new object[]{});
Try:
MethodInfo mi = t.GetMethod("getInstance", BindingFlags.Static);
--
Patrick Steele
Microsoft .NET MVP
http://radio.weblogs.com/0110109
-
Re: Invoking a static method with Reflection
Patrick Steele [MVP] <patrick@mvps.org> wrote:
>In article <3ec114e5$1@tnews.web.devx.com>, st_rosengren@hotmail.com
>says...
>>
>> I like to invoke a static method "getInstance" on a class using C#. Any
ideas?
>> This won't work...
>>
>> Assembly a = Assembly.LoadFrom("Reflection.dll");
>> Type t = a.GetType("Reflection.Singleton");
>> MethodInfo mi = t.GetMethod("getInstance");
>> object o = mi.Invoke(t, new object[]{});
>
>Try:
>
>MethodInfo mi = t.GetMethod("getInstance", BindingFlags.Static);
>
>--
>Patrick Steele
>Microsoft .NET MVP
>http://radio.weblogs.com/0110109
Thanks, although it didn't work. But I could simply do like this...
Type t = t.GetType("Reflection.Singelton");
object o = t.InvokeMember("getInstance", BindingFlags.InvokeMethod, null,
t, new object[0]);
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|