Late binding -- Calling a static method

  • Thread starter Thread starter BrianGenisio
  • Start date Start date
B

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
 
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
 
Back
Top