Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I implicitly implement method in generic class, derived from abstract?

I am trying to have a List of classes that implement a generic class and be able to access some members of the GenericClass through the abstract base. This is where I am at the moment:

public abstract class AbstractParent: UserControl
{
    public abstract AAction Action { get; set; }
}

public abstract class GenericBase<ActionType> : AbstractParent 
{
    protected ActionType _action;

    new public ActionType Action
    {
        set { _action = value; }
        get { return _action; }
    }
}

In order to have a list of GenericBase<> instances, I use a List<AbstractParent>

The problem is that I want a public property of the genericType (passed by the derived class from GenericBase).

The error at the moment is Action hides inherited abstract member Action.get. I know that I can either have an abstract proprty and an implementation, or implementation and new modifier in the derived property. Both don't fit in this situation.

example of what I want:

GenericBase<BlueAction> blueClass;
blueClass.Action <- I want this member to be of BlueAction type
like image 882
Odys Avatar asked Feb 16 '26 01:02

Odys


2 Answers

public interface IActionable 
{
  public ActionType Action { get; }
}

public class ActionableWrapper : IActionable
{ 

  private ActionType _action;

  public ActionableWrapper(IActionable Actionable)
  {
    this._action = Actionable;
  }

  new public ActionType Action 
  { 
    get { return _action; } 
  } 
} 

public class BlueAction: IActionable
{
  public ActionType Action { get; }
}

This does what you want, granted not using generics. Since I can't figure out WHY you'd want a List<ActionableWrapper> when instead of using generics you could just use an interfaces and make it simplely List<IActionable>.

I am trying to have a List of classes that implement a generic class and be able to access some members of the GenericClass through the abstract base.

Using Linq you can do this easily anyway (using your example):

List<AbstractParent> AbstractParentCollection = new List<AbstractParent>();
GenericBase<BlueAction> blueClass;     
AbstractParentCollection.Add(blueClass);
AbStractParentCollection.Typeof(GenericBase<BlueAction>).First().Action...
like image 194
Erik Philips Avatar answered Feb 17 '26 14:02

Erik Philips


Can your AbstractParent be redesigned to require ActionType:

public abstract class AbstractParent<ActionType>
{
    public abstract Action<ActionType> Action { get; set; }
}

then your inherited class can override, if needed:

public override Action<ActionType> Action

Update

An alternative is to have ActionType implement a new interface, such as IActionType. The return property from the base class would be the interface and this wouldn't necessarily need to be overridden in the inherited class.

Here is the complete class/interface structure:

public interface IAction
{
}

public abstract class AbstractParent
{
    public abstract IAction Action { get; set; }
}

public abstract class GenericBase<T> : AbstractParent where T : IAction
{
    protected IAction _action;

    public override IAction Action
    {
        get
        {
            return _action;
        }
        set
        {
            _action = value;
        }
    }
}
like image 22
competent_tech Avatar answered Feb 17 '26 14:02

competent_tech



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!