Does exist any way to force child class to have at least one field with a specified attribute?

270 Views Asked by At

Suppose we have created an attribute named [Mandatory]. Those exist any way to force a child class to have at least on field with this attribute while the parent class does not have any field?? For example suppose this:

class parent{
    public abstract void doSomething(){};
}

What should I add to the parent class so child class had to be something like this:

class parent{
    [Mandatory] public field1;
    public override void doSomething(){ // do something ...};
}

And if it did not contain field1 it could not be compiled. Does exist something like this in c#?

Update

The parent class is only to apply a rule that Already staff respect but I wanted to guarantee that everybody have to declare at least on field. I think this is not a case of object oriented application. But I'm trying to force them obey this convention using oop! If it is not the correct way please inform me.

Thanks for any help.

1

There are 1 best solutions below

4
On

Did you specifically want an abstract field or is a property sufficient?

How about the following?

abstract class Base
{
    public abstract int Field { get; set; }
}

class Derived : Base
{
    public override int Field { get; set; }
}