Home All Groups Group Topic Archive Search About

Late binding -- Calling a static method

Author
19 Jun 2006 1:42 PM
BrianGenisio
This is purely an academic question for me... I don't actually need to
do it, but I am trying to figure out it might be done.

Lets say I have a singleton class... meaning the constructor is
private, and GetInstance is public static.

How do I use late binding to call the static method?  All I see, is
that I can use an Activator to CreateInstance on a type, but in this
case, the ctor is private. 

How can this be done?

Thanks,
Brian

Author
19 Jun 2006 1:54 PM
Barry Kelly
BrianGeni***@gmail.com wrote:

> This is purely an academic question for me... I don't actually need to
> do it, but I am trying to figure out it might be done.
>
> Lets say I have a singleton class... meaning the constructor is
> private, and GetInstance is public static.
>
> How do I use late binding to call the static method?  All I see, is
> that I can use an Activator to CreateInstance on a type, but in this
> case, the ctor is private. 

    typeof(YourType).GetMethod("YourMethod", BindingFlags.Public |
        BindingFlags.Static)

This gets a MethodInfo. You can wrap it in a delegate (with the same
signature as the method) with Delegate.CreateDelegate() if you want to
avoid the overhead of MethodInfo.Invoke().

There are other overloads of GetMethod that allow you to pass the types
of the parameters, in case the method is overloaded.

-- Barry


AddThis Social Bookmark Button