Generic Function With Abstract Class Implementations [duplicate] - c#

This question already has answers here:
Why an inherited interface can't be converted to its base interface in generic context?
(2 answers)
Closed 2 years ago.
I'm trying to write a generic function like the example of the image below.
The idea is to have a method that will receive a generic type that must inherit an abstract class that has a generic configuration which has another abstract class.
public class MainCode
{
public MainCode()
{
Execute<DefaultOptions>();
}
public void Execute<T>() where T : BaseClassOptions<BaseClassConfiguration> { }
}
public class DefaultOptions : BaseClassOptions<DefaultConfiguration> { }
public abstract class BaseClassOptions<T> where T : BaseClassConfiguration
{
public T Config { get; set; }
}
public class DefaultConfiguration : BaseClassConfiguration { }
public abstract class BaseClassConfiguration
{
public string Host { get; set; }
}
But I got the following error:
Could you please help me?

Maybe this will work for you?
public MainCode()
{
Execute<DefaultOptions, DefaultConfiguration>();
}
public void Execute<T, T2>() where T : BaseClassOptions<T2> where T2: BaseClassConfiguration
{ }
The problem is that BaseClassOptions<BaseClassConfiguration> is not the base class for the DefaultOptions, that's why you get this error. It happens because type parameters on generic classes are neither covariant nor contravariant, i.e. there is no inheritance relationship between specializations of the same generic class. If you don't want to use interfaces it may worth to say compiler directly what class to use as a generic parameter with your base type.

Related

Where Generic T is implementation of Abstract Class

Given the following classes and hierarchy:
public abstract class AbsractClass<T>
{
public T Id { get; private set; }
}
public class ImplementationA : AbsractClass<Guid> { }
public class ImplementationB : AbsractClass<int> { }
What constraint should I be using in where clause/constraint to specify that generic T must be implementation of AbsractClass. Which would mean that Entity would accept either ImplementationA or ImplementationB.
public abstract class Entity<T> // where : T ?
{
}
Is this something that is possible or should I be using interfaces instead?
You need to add two generic parameters to make it work:
public abstract class Entity<T, TKey> where T : AbstractClass<TKey>
{
}
Generic interfaces would behave the same way.

Polymorphism while iheriting generic class [duplicate]

This question already has answers here:
Convert List<DerivedClass> to List<BaseClass>
(13 answers)
Closed 5 years ago.
Let's consider that there is an abstract base class and one, or more child classes:
public abstract class BaseInnerClass
{
public int Id { get; set; }
}
public class ConcreteInnerClass : BaseInnerClass
{
public string Name { get; set; }
}
Then, let's assume there is a generic abstract class that has a property of above abstract class type:
public abstract class GeneriAbstractTestClass<T> where T : BaseInnerClass
{
public T InnerClass { get; set; }
}
Then let's make a class that inherits from the class above:
public class ConcreteTestClass : GeneriAbstractTestClass<ConcreteInnerClass>
{
public string ConcreteString { get; set; }
}
So now everything is prepared to ask a question ;) Why it is not possible to do it:
//cannot convert initializer type
GeneriAbstractTestClass<BaseInnerClass> genericClass = new ConcreteTestClass();
while this is allowed:
//ok
BaseInnerClass baseInner = new ConcreteInnerClass();
What's the difference between this two assignments?
This has nothing to do with abstract classes. A simpler example would be
List<BaseInnerClass> base = new List<ConcreteInnerClass>
The fact that type A is derived from type B does not imply that type C<A> is derived from type C<B>. Your example is a little bit more complicated, but it can be explained using the same logic.
Note that you can define another concrete type:
public class EvilConcreteInnerClass : BaseInnerClass
{
}
If what you wanted was possible, then the following would work:
GeneriAbstractTestClass<BaseInnerClass> genericClass = new ConcreteTestClass();
genericClass.InnerClass = new EvilConcreteInnerClass(); // OK, because the compiler sees `T` as `BaseInnerClass`
genericClass variable points to an object whose T generic parameter is ConcreteInnerClass, so assigning EvilConcreteInnerClass to the property would result in a run-time exception.
Actually. You can do this. But you need to specify interface with covariant out T generic, because it is type safe to make those casts.
Example
namespace ConsoleTest
{
class Program
{
static void Main(string[] args)
{
var a = new Generic<Concrete>();
IGeneric<Base> c = new Generic<Base>();
c = a;
}
}
public interface IGeneric<out T> where T: Base
{
T Inner { get; }
}
public class Generic<T> : IGeneric<T>
where T : Base
{
public T Inner { get; set; }
}
public class Concrete : Base
{
}
public class Base
{
}
}
Delegates also not restricted if they specify covariant out generic templates.
It means those casts you want is OK as long as you use readonly generic properties. So, like #Kapol said and provided you example why it is not type safe to allow setters on properties or pass T into function.
Summary
Use ReadOnly interfaces if you want to use those kinds of casts.

Generics inheritance compile error

I'm getting a compile error when I try to compile this
The type 'WpfApplication2.CommandInstrumentTrade' cannot be used as type parameter 'T' in the generic type or method 'WpfApplication2.GenericWindowBase'. There is no implicit reference conversion from 'WpfApplication2.CommandInstrumentTrade' to 'WpfApplication2.GenericCommandBase'
public interface IBaseClass
{
int ID { get; set; }
}
public class BaseClass : IBaseClass
{
public int ID { get; set; }
}
public class DerivedClass : BaseClass
{
}
public class Command
{
}
public class GenericCommandBase<T> : Command where T : IBaseClass
{
}
public class DerivedGenericCommand : GenericCommandBase<DerivedClass>
{
}
public class GenericWindowBase<T> where T : GenericCommandBase<IBaseClass>
{
}
public class DerivedGenericWindow : GenericWindowBase<DerivedGenericCommand> // this line fails
{
}
The issue is that Generic<Derived> does not satisfy the condition where T : Generic<Base>. Even if Derived derives from Base, Generic<Derived> does not derive from Generic<Base>
There are many questions like that in StackOverflow.
Try reading those:
C# Generics Inheritance
generic inheritance in C#?
Inheritance doesn't compose with generics. You need to create some kind of converter from one to another. Maybe if you present some less abstract code we could help You

How do I pass two similar concrete objects to a method with interface parameters that implement generics in C#?

I have the following interface declarations:
interface IOrder<T> where T: IOrderItem
{
IList<T> Items { get; set; }
}
interface IDistro<T> : IOrder<T> where T: IOrderItem
{
}
I have two concrete classes, like so:
// DistroItem implements IOrderItem
public class Distro : IDistro<DistroItem>
{
public IList<DistroItem> Items { get; set; }
}
// PerishableOrderItem implements IOrderItem
public class PerishableOrder : IDistro<PerishableOrderItem>
{
public IList<PerishableOrderItem> Items { get; set; }
}
Lastly, I have a static service method for saving to the database:
public static void UpdateDistro(IDistro<IOrderItem> distro)
{
}
My problem is, how do I pass a distro of either concrete type to my static method? The following doesn't compile:
Distro d = new Distro();
UpdateDistro(d);
The error is:
The best overloaded method match for UpdateDistro(IDistro<IOrderItem>)' has some invalid arguments
Is contravariance the answer? I tried adding <in T> to the original interface declaration, but that added more errors that I was unable to resolve. This is my first in depth foray into interfaces and I'm sure generics is adding complexity, so there might be a fundamental lack of understanding here.
Have you tried this:
public static void UpdateDistro<T>(IDistro<T> distro)
where T : IOrderItem
{
}
EDIT:
With empty implementations for DistroItem and PerishableItem classes (both implementing IOrderItem), I've got the following compiling without an error:
Distro d = new Distro();
PerishableOrder p = new PerishableOrder();
UpdateDistro(d);
UpdateDistro(p);
You can define a covariant generic parameter in your interface, you need to change the interface a little bit though to ensure that T is not uses contravariantly:
public interface IOrder<out T> where T : IOrderItem
{
IEnumerator<T> Items { get; }
}
public interface IDistro<out T> : IOrder<T> where T : IOrderItem
{
}
To define T as coverient parameter (out), allows for implicit conversion of classes that implement your variant interfaces.

How to have a list of ProblemBase<TResult>? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I create a list of objects that inherit from the same generic class with varying types?
I'm using several objects where they are inherited from an abstract class. But to use the abstract class must be declara a generic datatype.
I'm having problems because I need to have a list where contains a list of ProblemBase, although each one contains a different TResult datatype.
public abstract class ProblemBase<TResult>
{
TResult[] Array;
}
And I want to get Array property. That's the problem.
This type of thing happens for me quite often. The solution I typically go with is to have a base class for ProblemBase<T> that is type free:
public abstract class ProblemBase
{
public abstract object Result { get; }
}
public abstract class ProblemBase<TResult> : ProblemBase
{
public override object Result
{
get { return Result; }
}
new public TResult Result { get; private set; }
}
Whenever you need a collection of problems, then, you can make a collection of ProblemBase without the generics.
If TResult has its own required inheritance hierarchy, then you can do this instead:
public abstract class ProblemBase
{
public abstract ResultBase Result { get; }
}
public abstract class ProblemBase<TResult> : ProblemBase
where TResult : ResultBase
{
public override ResultBase Result { get { return Result; } }
new public TResult Result { get; private set; }
}

Categories

Resources