Static interfaces? Static method attributes?

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
}
}
 
P

Peter Morris [Droopy eyes software]

Looks to me suspiciously like you are confusing a "Type" with a class
factory.

I would recommend having a class factory which implements your interface.
Mark it with a custom attribute. Scan the assembly for these factories
(marked with the attribute) and create an instance of each (keeping a
reference in a list of some kind). Then you can query each instance in the
list whenever you like, if the result of the method is what you desire then
you can execute another method (exposed via the same interface) to create
your instance.


--
Pete
====
ECO Modeler, Audio compression components, DIB graphics controls,
FastStrings
http://www.droopyeyes.com

Read or write articles on just about anything
http://www.HowToDoThings.com

My blog
http://blogs.slcdug.org/petermorris/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top