-
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
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks