DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3

Hybrid View

  1. #1
    Staffan Guest

    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

  2. #2
    Patrick Steele [MVP] Guest

    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

  3. #3
    Staffan Guest

    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]);


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


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


Sponsored Links