0

This question was going to be "defining static interfaces' but I realized that was

  • already asked (answer from Skeet = No)

  • presupposed the answer

So the question is. I have a method that accepts a type (its not a generic method at the moment but could be changed if needed). I need to know if this type has a given method and if so call that method.

I know I could define an interface and make the class implement that interface and instantiate if its of the right type and call the method. Like this

public interface IDoFizz
{
    void Fizz();
}

...

var t = Activator.CreateInstance(type);
if(t is IDoFizz)
{
    (t as IDoFizz).Fizz();
}

the problem is that means I have to instantiate the object. This might be expensive, or even impossible (if it has no null contructor or its a static class)

So any ideas? I was thinking about something like

DoThing<T>(xxx) where T: IDoFizz
{
 // code that calls IFoo.Fizz
}
DoThing<T>()
{
}

but it feels a bit clunky. Or I suppose I could use reflection to see if it supports the method.

"Can you describe the reasoning some more"

This is a job scheduling system. The type is the type of the job being submitted. The jobs all implement IAmAJob. Once the job gets run it is instantiated and IAmAJob.run is invoked. SO obviously I could add the Fizz (actually a preflight check) to IAmAJob interface. But that means I have to instantiate it - which I dont want to do. The preflight check is a perfectly happy being a static method

pm100
  • 48,078
  • 23
  • 82
  • 145
  • 1
    You need reflection. – SLaks Mar 17 '16 at 20:56
  • 1
    You need to call a static method you say in the header. Why do you need an object? – Magnus Mar 17 '16 at 20:57
  • Pretty close / good starting point is this question about how to use reflection to invoke a static method: http://stackoverflow.com/questions/11908156/call-static-method-with-reflection – stephen.vakil Mar 17 '16 at 20:59
  • Taking the question purely at face value, the answer would be to use Reflection, I don't think you have a choice. But depending on the situation, you may have other options, e.g. passing a lambda. Can you add some more context to describe how this code would be called, and we may find a neater way to achieve want you want. – Rhumborl Mar 17 '16 at 21:01
  • @Rhumborl - I did start using a lambda but sadly I need to serialize and you cant serialize a lambda – pm100 Mar 17 '16 at 21:46
  • @Magnus - i was showing the model I *would* use if I was instantiating an object. I need the equivalent for static. Maybe this was just confusing to put in the question – pm100 Mar 17 '16 at 21:55
  • 1
    Can you describe the reasoning some more? What does the static method do? Is it defined on multiple types that don't have a common base type? Why do you not know if the type will have that static method or not? What do you do with the type other than call this static method? (please add to your question, not as a comment) – D Stanley Mar 17 '16 at 21:59

1 Answers1

0

If you need to call a non static member (like in your examples, but unlike your question title) you can do:

DoThing<T>() where T: IDoFizz, new()
{
   new T().Fizz();
}

But for static method you would have to use reflection like:

public void DoThing(Type t)
{
    var m = t.GetMethod("Fizz", BindingFlags.Public | BindingFlags.Static);
    if(m != null)
        m.Invoke(null, null);
}
Magnus
  • 45,362
  • 8
  • 80
  • 118
  • yup,I worked out that i need to be able to do T.Fizz() . And the reflection one seems smelly to me – pm100 Mar 17 '16 at 21:43