R
rogercnorris
I would like, if possible, to guarantee that a certain set of classes
all implement a specific static method:
static bool DoSomething( byte [] data );
What's the best way to approach this problem? To the best of my
knowledge, there's no way to define an interface which applies to
static methods. I'm considering using a custom attribute which would
designate the static method, but I can't find any way to restrict the
attribute's usage to just static methods.
I can use AttributeTargets.Method to indicate that the custom attribute
must apply to a method. Is there some way to restrict it further to
static methods? Or would that have to be a run-time check?
Basically, I want to be able to look through an array of System.Type
objects and call a static method for each one:
void DoStuff( System.Type [] validTypes )
{
foreach( System.Type type in validTypes )
{
// find static method "DoSomething"
MethodInfo method = ...
// invoke static method
if( method.Invoke( null, new object [] { } ) )
// if method returns true, then we create an
// instance of this type
}
}
all implement a specific static method:
static bool DoSomething( byte [] data );
What's the best way to approach this problem? To the best of my
knowledge, there's no way to define an interface which applies to
static methods. I'm considering using a custom attribute which would
designate the static method, but I can't find any way to restrict the
attribute's usage to just static methods.
I can use AttributeTargets.Method to indicate that the custom attribute
must apply to a method. Is there some way to restrict it further to
static methods? Or would that have to be a run-time check?
Basically, I want to be able to look through an array of System.Type
objects and call a static method for each one:
void DoStuff( System.Type [] validTypes )
{
foreach( System.Type type in validTypes )
{
// find static method "DoSomething"
MethodInfo method = ...
// invoke static method
if( method.Invoke( null, new object [] { } ) )
// if method returns true, then we create an
// instance of this type
}
}