0

I have a List of object came to me by serialization. But I know following:

  1. The list is not Empty
  2. All types of the list have the same type. I can know this type only dynamically. For example:

    list[0].GetType()

How to convert all list to Generic List using above mentioned conditions ?

P.S. I deserialize an array of objects came to me in JSON

    {
   "Message": [
      {
         "__type": "GameResponse:#Bridge3.Server",
         "GameId": 1,
         "GameName": "Game1                                  ",
         "PlayerId": 1
      },
      {
         "__type": "GameResponse:#Bridge3.Server",
         "GameId": 2,
         "GameName": "Game2                                  ",
         "PlayerId": 1
      }
    }
7
  • 4
    You can't. Change the serialization code to return the proper strongly typed data. Commented Mar 12, 2014 at 21:43
  • You can make a generic list by reflection. But what is your usage about? What do you want to do with generated generic list? Explain more please. Commented Mar 12, 2014 at 21:46
  • @javad_amiry if the type were know at compile time, you can just list.OfType<TheType>(). However, the OP's apparent situation is that the type is unknown at compile time Commented Mar 12, 2014 at 21:46
  • 1
    @Javad_Amiry which makes no sense whatsoever, since you're already dealing with a list of unkown objects, you better treat them as dynamic in that case. No need for reflection, unless you want to convert the data to some sort of Dictionary<string,object> such as a DataTable or the like. Commented Mar 12, 2014 at 21:50
  • 1
    Yes again, and it is the why of my question: What is usage? Commented Mar 12, 2014 at 21:52

2 Answers 2

2

You can use somewhat of a workaround with the magic of dynamic keyword:

interface ICar {}
interface IAnimal {}

class Program
{
    static void Dispatch(dynamic list)
    {
        Console.WriteLine("Dispatch called");
        DoSomething(list);
    }

    static void DoSomething<T>(List<T> genericList)
    {
        Console.WriteLine("Generic unconstrained method called");
    }

    static void DoSomething(List<IAnimal> animalList)
    {
        Console.WriteLine("Do something WILD");
    }

    static void DoSomething(List<ICar> carList)
    {
        Console.WriteLine("Calculate loans");
    }

    static void Main()
    {
        object deserializedList = new List<ICar>();
        Dispatch(deserializedList);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

static void DoSomething<T>(List<T> genericList) will use List<object> :) No need for dynamic
@alerya but with DoSomething<T> you don't have type; you can't use the methods. This is a nice way of giving you all the type info. (sans reflection)
1

You can use reflection to create a strong typed list. You won't have that type for use in code, but the list type will be fixed.

//DetermineSerializedType here would be your own method to determine the type you have
Type deserializedType = DetermineSerializedType(serializedData);

Type genericType = typeof(List<>).MakeGenericType(deserializedType);
ConstructorInfo ctor = genericType.GetConstructor(new Type[] { });
object inst = ctor.Invoke(new object[] { });
IList list = inst as IList;

EDIT Personally I think the dynamic solution Grozz gives is cleaner.

3 Comments

how is this any better than an object[] ?
And use the Activator to create an instance of the generic list type. I had to create a custome Serializer and this works.
@HighCore Because he didn't ask for an object[]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.