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