Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force attribute usage in subclass of abstract superclass

How can I force a subclass to implement certain Attributes of its superclass? The reason is that I want to use Attributes for general information about the class, e.g. "DisplayName", "Description" or "Capabilities".

So I thought I might implement them in a superclass and force the subclasses to implement the attributes.

Is there something like an abstract attribute like for methods?

[abstract DeclareMe]
public abstract class InheritMe {
    public abstract void DeclareMe();
}
like image 228
F.P Avatar asked Jul 10 '12 08:07

F.P


People also ask

Does abstract class force implementation?

While an abstract class cannot be instantiated, it can have implementation details. The designers of C# chose to force the user to either implement the functionality, or specifically state that the functionality inherited from the interface will not be implemented at this level.

Can abstract classes have attributes?

An abstract class can contain attribute or variables, abstract method, and normal method or anyone of them. But the subclass of the abstract class can only implement the abstract method of the abstract class.

Can a subclass of an abstract class be abstract?

Abstract classes cannot be instantiated, but they can be subclassed. When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract .

Can an abstract class have concrete methods?

Abstract class can have both an abstract as well as concrete methods. A concrete class can only have concrete methods. Even a single abstract method makes the class abstract. Abstract class can not be instantiated using new keyword.


1 Answers

As your class must be run sooner or later, you can add checking mechanism to your base class to verify the existance of certain attributes in your sub classes.

Here's some sample code for you.

class Program
{
    static void Main(string[] args)
    {
        var a = new SubA();
        var b = new SubB();
    }
}

class BaseClass
{
    public BaseClass()
    {
        Type t = GetType();
        if (t.IsDefined(typeof(SerializableAttribute), false) == false)
        {
            Console.WriteLine("bad implementation");
            throw new InvalidOperationException();
        }
        Console.WriteLine("good implementation");
    }
}

[Serializable]
class SubA : BaseClass
{ }

class SubB : BaseClass
{ }

The last word, don't be too wary of yourself. Once I was doing my design, I always thought I might call two methods in a wrong order or forget to do something, then I turned a simple design into a complicated one to prevent my possible mistakes. Later I threw away the guards, just throwing Exceptions and the code used to detect unexpected situations were surrounded by #if DEBUG.

like image 105
Gqqnbig Avatar answered Oct 14 '22 20:10

Gqqnbig