How can I assure a class to have a static property by using interface or abstract?

34,388

Solution 1

You can't do that. Interfaces, abstract, etc. cannot apply to static members. If you want to accomplish this, you will have to manually remember to do it on all deriving classes.

Also, static members are inherited by deriving classes. Child classes must hide the static parent member if they wish to specify alternate behavior.

Solution 2

It doesn't make sense, anyway, as you'd have no way to access that static property without determining the type, thus breaking the whole point of having an interface anyway.

I'd just put a property on the interface, and route it to the static member.

public interface IMyInterface
{
    void Foo();
    IList<string> Properties { get; }
}

public class ConcreteClass : IMyInterface
{
    public void Foo(){}
    public IList<string> Properties
    {
        get { return s_properties; }
    }
}

But that leads me to the second question - what is it that you are trying to accomplish? Why do you need to have a static member on the class? What you really want is, given an object, to be able to determine what properties it has, right? So why would your code care if they're stored statically or per instance?

It seems like you're confusing contract (what you want to be able to do) with implementation (how the provider of the service accomplishes the goal).

Solution 3

Ok. Maybe I was not clear enough. But I have achieved basically what I need by doing something like this:

public abstract myBaseClass
{
 public List<string> MyParameterNames
   {
     get 
         {
             throw 
               new ApplicationException("MyParameterNames in base class 
                                 is not hidden by its child.");
         }
   }
}

So any class derived from this class, will throw an exception if MyParameterNames property is tried to reach the parameter names of that derivedclass.

Not a perfect way, but it helps me to overcome my problem in a way.

Solution 4

It is imposible. Inheritance cannot be aplied to the members of the type (static members).

Solution 5

All the pieces of the solution are here, spread across multiple answers.

  1. Create the interface as you would normally.
  2. Create an abstract base class which implements the interface, and defines any static members which will be required.
  3. Inherit from the abstract base class, rather than the interface when creating your actual implementations.

While it still will not allow you to access Subclass.MyParameterNames from AbstractClass.MyParameterNames, you will be able to ensure that all implementations of AbastractClass have that property available.

Depending on the specifics of your use case, however, it may be better to expose MyParameterNames it as a non-static member, and simply implement it as a singleton so that there is only one copy of the list for each subclass. Either way, you'll still need to initialize an instance of the class in order to get the data you want.

At the very least, to get the static data, you'll need to know what specific subclass you're dealing with, so it doesn't make much sense to attempt to look at it from an interface, which can be an arbitrary, unknown data type.

Share:
34,388
vel
Author by

vel

Updated on May 08, 2020

Comments

  • vel
    vel about 4 years

    I have one abstract class -let's say myBase. And I want all the classes derived from myBase to have one static field called

    public static List<string> MyPArameterNames 
    {
    get {return _myParameterNames;} 
    }
    

    So, every child class can tell what parameter names it uses; I want static because I do not want to create an instance just for this.

    How can I achieve this?

  • tofutim
    tofutim over 10 years
    "manually remember" - gee it is like we don't have computers or something
  • SvendK
    SvendK about 8 years
    Didn't you want a static property?
  • mkb
    mkb about 8 years
    I say the question title is a nice one but what you meant is not related to question title
  • Adeem
    Adeem over 7 years
    And what about when we use interface to declare object of that class (as in case of dependency injection), in that case we will not be able to use that static variable/property cause property is not available in interface