Static methods/properties in interface?

D

Dathan

I'm working with a data retrieval / reporting system that I've
designed which is capable of generating sets of data from a database
according to some criteria. The various data set generators are
implemented via classes which inherit from my DataRetriever class, and
all interactions with them are done using a reference to the base
class. This all works fine. However, I've developed enough of these
reports now that it's becoming problematic to keep track of which one
does what. So I'd like to add static properties for name and
description. I'd like to do something like this:

interface IDataSetGenerator
{
static string Name { get; }
static string Description { get; }
}

That way when I create a list of available reports to run for the user
to choose, I can add the value of this static member as a description
to help the user pick the right report. However, the above code
doesn't compile, and I can't figure out if there's a general way to do
this. The only other idea I've had is to implement my own custom
attribute and attach it to each class, which would be fine except that
then these values would ONLY (AFAIK) be available for access via
reflection.

Any ideas on how I can implement this functionality?
 
A

Anthony Jones

Dathan said:
I'm working with a data retrieval / reporting system that I've
designed which is capable of generating sets of data from a database
according to some criteria. The various data set generators are
implemented via classes which inherit from my DataRetriever class, and
all interactions with them are done using a reference to the base
class. This all works fine. However, I've developed enough of these
reports now that it's becoming problematic to keep track of which one
does what. So I'd like to add static properties for name and
description. I'd like to do something like this:

interface IDataSetGenerator
{
static string Name { get; }
static string Description { get; }
}

That way when I create a list of available reports to run for the user
to choose, I can add the value of this static member as a description
to help the user pick the right report. However, the above code
doesn't compile, and I can't figure out if there's a general way to do
this. The only other idea I've had is to implement my own custom
attribute and attach it to each class, which would be fine except that
then these values would ONLY (AFAIK) be available for access via
reflection.

Any ideas on how I can implement this functionality?

An interface cannot define static members hence your code above fails to
compile.

You could add Name and Description properties to DataRetriever as virtual
properties:-

public class DataRetriever
{
public virtual string Name { get { return "DataRetriever"; } }
public virtual string Description { get { return "Base Data
Retriever"; } }
...
}

public class SpecificDataRetriever : DataRetriever
{
public override string Name { get { return "SpecificDataRetriever"; } }
public overrde string Description { get { return "Data Retriever for
specific stuff"; } }
...
}


Although a little reflection might be better than cluttering your API with
these properties

using System.ComponentModel;

public class DataRetriever
{

public static string GetName(DataRetriever dr)
{
return dr.GetType().Name;
}

public static string GetDescription(DataRetriever dr)
{
Type t = dr.GetType();
DescriptionAttribute desc =
(DescriptionAttribute)Attribute.GetCustomAttribute(t,
typeof(DescriptionAttribute))
return desc ? desc.Description : null
}
}

[Description("Data Retriever for specific stuff")]
public class SpecificDataRetriever : DataRetriever { }

DataRetriever x = new SpecificDataRetriever()

Console.Write(DataRetriever.GetName(x));
Console.Write(DataRetriever.GetDescription(x));
 
M

Michael C

Dathan said:
I'm working with a data retrieval / reporting system that I've
designed which is capable of generating sets of data from a database
according to some criteria. The various data set generators are
implemented via classes which inherit from my DataRetriever class, and
all interactions with them are done using a reference to the base
class. This all works fine. However, I've developed enough of these
reports now that it's becoming problematic to keep track of which one
does what. So I'd like to add static properties for name and
description. I'd like to do something like this:

interface IDataSetGenerator
{
static string Name { get; }
static string Description { get; }
}

That way when I create a list of available reports to run for the user
to choose, I can add the value of this static member as a description
to help the user pick the right report. However, the above code
doesn't compile, and I can't figure out if there's a general way to do
this. The only other idea I've had is to implement my own custom
attribute and attach it to each class, which would be fine except that
then these values would ONLY (AFAIK) be available for access via
reflection.

Any ideas on how I can implement this functionality?

I've gotta admit I tried do this rather silly idea once. The reason it's
rather silly is that if this was possible to define you'd have no way to
call the methods. If SomeClass implements IDataSetGenerator and you wanted
to call the Name property how would you call it? You can't call it from any
instance you have because the properties are static. You can't call it from
IDataSetGenerator because that would just be calling it on an interface
which a) can't have code and b) wouldn't give you the result you need
anyway. You could call it from SomeClass but then there is no purpose to the
interface because you may as well just put a static Name and Description
property on each report.

I think as suggested by Anthony the best option would be a custom attribute.

Michael
 

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