|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Static methods/properties in interface?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?
Show quote
Hide quote
"Dathan" <dat***@gmail.com> wrote in message An interface cannot define static members hence your code above fails to news:0d9486d7-522c-431e-b4d7-03d3e81682fb@o40g2000prn.googlegroups.com... > 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? 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)); -- Anthony Jones - MVP ASP/ASP.NET
Show quote
Hide quote
"Dathan" <dat***@gmail.com> wrote in message I've gotta admit I tried do this rather silly idea once. The reason it's news:0d9486d7-522c-431e-b4d7-03d3e81682fb@o40g2000prn.googlegroups.com... > 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? 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
Other interesting topics
Truely unique file name.
Getting the oldest file in a directory. Base Class constructor problem Override the property FolderBrowserDialog won't open sometimes CPU bottleneck get namespace & classes in other projects ProvideProperty attribute unable to handle system.dll Getting modification time of remote files (on a website). |
|||||||||||||||||||||||