mardi 29 avril 2014

c# - Cast générique avec un type dérivé de générique avec un type de parent - Stack Overflow


I have a base class:


public abstract class InputParserBase<TInputData> : ServerComponentBase
where TInputData : SolverDataBase
{
public abstract TInputData Parse(ClientInputData task);
}

and a child:


public class BinomTaskParser : InputParserBase<BinomTaskData>
{

public override BinomTaskData Parse(ClientInputData inputData)
{//Some stuff
}
}

BinomTaskData is inherited from SolverDataBase


My second class is in another assembley and i load it in runtime. And the thing is that i can have different childs with another types, derived from SolverDataBase. I want to load them and store like this:


var result = new List<InputParserBase<SolverDataBase>>();
var parser = (InputParserBase<SolverDataBase>)AssembleyHelper.CreateInstance(typePair, null);
result.Add(parser);

But get a cast exception. What am i doing wrong and how can i implement this?




Classes are invariant so the cast is invalid. You could create an interface:


public interface IInputParser<out T> where T : SolverDataBase
{
T Parse(ClientInputData);
}

and implement it in the base class or in each subclass:


public abstract class InputParserBase<TInputData> : ServerComponentBase, IInputParser<TInputData>
where TInputData : SolverDataBase
{
public abstract TInputData Parse(ClientInputData task);
}

then you can create a list like:


var result = new List<IInputParser<SolverDataBase>>();
var parser = (IInputParser<SolverDataBase>)AssembleyHelper.CreateInstance(typePair, null);
result.Add(parser);


I have a base class:


public abstract class InputParserBase<TInputData> : ServerComponentBase
where TInputData : SolverDataBase
{
public abstract TInputData Parse(ClientInputData task);
}

and a child:


public class BinomTaskParser : InputParserBase<BinomTaskData>
{

public override BinomTaskData Parse(ClientInputData inputData)
{//Some stuff
}
}

BinomTaskData is inherited from SolverDataBase


My second class is in another assembley and i load it in runtime. And the thing is that i can have different childs with another types, derived from SolverDataBase. I want to load them and store like this:


var result = new List<InputParserBase<SolverDataBase>>();
var parser = (InputParserBase<SolverDataBase>)AssembleyHelper.CreateInstance(typePair, null);
result.Add(parser);

But get a cast exception. What am i doing wrong and how can i implement this?



Classes are invariant so the cast is invalid. You could create an interface:


public interface IInputParser<out T> where T : SolverDataBase
{
T Parse(ClientInputData);
}

and implement it in the base class or in each subclass:


public abstract class InputParserBase<TInputData> : ServerComponentBase, IInputParser<TInputData>
where TInputData : SolverDataBase
{
public abstract TInputData Parse(ClientInputData task);
}

then you can create a list like:


var result = new List<IInputParser<SolverDataBase>>();
var parser = (IInputParser<SolverDataBase>)AssembleyHelper.CreateInstance(typePair, null);
result.Add(parser);

0 commentaires:

Enregistrer un commentaire