Have a generic class that inherits from a non-generic class like the structure below:
public class Result
{
public string ErrorCode { get; set;}
public string ErrorMessage { get; set;}
public boo Success { get; set;}
//Lots more properties
public ClientResult ToClientResult()
{
//some pretty involved calculations of error coded and status
}
}
public class Result<T> : Result
{
public T details {get; set;}
public ClientResult<T> ToClientResult<T>()
{
//Need to call the parent class implementation and convert result to generic ver
}
}
My question is how can i call the parent ToClientResult() and convert the result to the generic version of ClientResult<T> and then i need to set a property of the ClientResult<T> to the details property of the Result<T> class.
I am sure i am missing an easy solution here, i really dont want to duplicate the parent class logic as it is pretty complicated.
You can’t cast an object of the parent type to a child type if it’s created as the parent type (using
new ClientResult()). It just doesn’t work that way.What you could do is factor the complicated code out to another method which you use to do the heavy lifting in both the
Resultclass and theResult<T>class:This is all assuming
ClientResult<T>derives fromClientResult, which is a little hard to tell from your question.