How can i cast from Func<Object> to Func<dynamicType> c#

Viewed 153

Hello this is my first post, don't be to hard if I do anything wrong :D

I am writing a DeSerializer for a big Programm, To do so, I have a own Class

public class DeSerializeableElement
{
    public Func<Object> action;
    public Type type;

    public DeSerializeableElement( Func<Object> p_action,Type p_type)
    {   
        type = p_type;
        action = p_action;
    }

I read a String and then it always starts with 0XXX a 4 digit number. with this number I get the right method from my

Dictionary<int,DeSerializableElement>

the initialize of the Dictionary is auto Generated and has 300 elements

deSerializableObjectDictionary.Add(276, new DeSerializeableElement(GetString, typeof(System.String)));

GetString is a method with no parameters and returns a String

Now my problem, if I Deserialize a List, at the moment I create a DeSerializableElement the Func looses its return value Information. Because I save it as Func so I get back a List but it`s Important to get a List in case of GetString there are also GetInt or GetDouble and lots more

So If i call GetList(GetString) I want as return value a List and if I call GetList(GetInt) I want a List and so on. But I get always a List because my SerializableElement has Func as attribute

The call for GetList looks like

GetList(deSerializableObjectDictionary[objectIdent].action);

GetList looks like

public IList<T> GetList<T>(Func<T> p_action) //T is always Object because of Func<Object> here I the right Type
{
    IList<T> list = new List<T>();
    ExpectToken('[');          //The start Token of a serialized List
    while (!IsNextToken(']'))  //End Token of serialized List
    {
        list.Add(p_action());
    }
    return lst;
}
1 Answers
Related