I've got an abstract class CommandBase<T, X> that I want to have a property InnerCommand.
But since the InnerCommand might have other types for T and X than the command that contains it, how can I define that?
The abstract class:
public abstract class CommandBase<T, X>
where T : CommandResultBase
where X : CommandBase<T, X>
{
public CommandBase<T, X> InnerCommand { get; set; }
(...)
}
In the example above InnerCommand will only accept instances that have the same types for T and X, but I need to allow for other types.
An AddOrderitemCommand:
public class AddOrderitemCommand : CommandBase<AddOrderitemResult, AddOrderitemCommand>
{
(...)
}
Might contain a WebserviceCommand:
public class GetMenuCommand : CommandBase<GetMenuResult,GetMenuCommand>
{
(...)
}
Please advise on the syntax for allowing this.
In the same way, you can derive a generic class from another generic class that derived from a generic interface. You may be tempted to derive just any type of class from it. One of the features of generics is that you can create a class that must implement the functionality of a certain abstract class of your choice.
An attribute cannot inherit from a generic class, nor can a generic class inherit from an attribute.
You cannot inherit a generic type. // class Derived20 : T {}// NO!
You basically have three options:
dynamic as the type of that property. Nothing I would do.object as the type of that property. Nothing I would do.CommandBase<T, X> implement it and use it as the type of the property. That's the way I would go.If the InnerCommand doesn't relate to the parent T/X, then I would suggest using a non-generic InnerCommand that doesn't advertise the type in the signature. This may mean adding a non-generic base-type (CommandBase) or an interface (ICommandBase). Then you can simply use:
public ICommandBase InnerCommand {get;set;}
// note : CommandBase<T,X> : ICommandBase
or
public CommandBase InnerCommand {get;set;}
// note : CommandBase<T,X> : CommandBase
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With