0

Here is my problem. I have one base class and 2 or more derived class, I want to write a generic method to return one derived class but I don't know what type it is until it checked the passed paramater. However, I can't pass List<Food> to List<T> in GetData<T> as it said

Cannot implicitly convert type 'System.Collections.Generic.List<Generic.Food>' to 'System.Collections.Generic.List<T>' Generic  

Solution 1: Error

namespace Generic
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "some json document string here";
            var p = GetData<Cargo>(0, s);
        }

        static List<T> GetData<T>(int type, string src) where T : Cargo
        {
            if (type == 0)
                return JsonConvert.DeserializeObject<List<Food>>(src);
            else
                return JsonConvert.DeserializeObject<List<Paper>>(src);
        }
    }

    public abstract class Cargo
    {
        public double Price { get; set; }
    }

    public class Food : Cargo
    {
        public double Calorie { get; set; }
    }

    public class Paper : Cargo
    {
        public int Height { get; set; }
        public int Width { get; set; }
    }    
}

I know I could call something like this: Solutioon 2

        static void Main(string[] args)
        {
            string s = "some json document string here";
            int type = 0;
            if (type == 0)
                GetData<Food>(s);
            else
                GetData<Paper>(s);

        }

        static List<T> GetData<T>(string src) where T : Cargo
        {
            return JsonConvert.DeserializeObject<List<T>>(src);
        }

and don't check the parameter inside the function.

However, is there any possible to use the first solution?

Thanks.

Kunyuan Xiao
  • 175
  • 2
  • 13
  • Why don't you `return new List();`, or am I missing something? – ProgrammingLlama Oct 16 '19 at 01:43
  • Sorry I missed one step. Because in the method I need to get some data form mongodb and deserilize BsonDocument to specified type. If I don't use the type, I can't parse to the correct object. – Kunyuan Xiao Oct 16 '19 at 02:13
  • Again, why can't you deserialize to `T`? Can you edit your question to include a fuller example? – ProgrammingLlama Oct 16 '19 at 02:14
  • I edited a little bit codes. The question is I don't what exact type to parse and return before I check the passed parameter, what I should do If I want to check the parameter inside the method instead out of the method like solution 2. Sorry if I can' explain it clearly. – Kunyuan Xiao Oct 16 '19 at 02:58
  • Fundamentally, the issue is that `List` isn't variant on the type parameter. That is, you must always provide _exactly_ the type parameter on assignment; the marked duplicate explains this in more detail. Since you apparently have to check the `type` value anyway, it doesn't seem like a hardship to check it before calling the generic method, rather than inside it. – Peter Duniho Oct 16 '19 at 05:03

0 Answers0