Invalid expression term string in a method that accepts type as an argument

I am about to go crazy.

I can't figure out why this won't work.

Wrapper.DecompressAndDeserialize(string, ms);

Here is the overload for the method:

public static Object DecompressAndDeserialize(Type t, byte[] bytData)
    {
        byte[] b = Decompress(bytData);
        Object o = Deserialize(t, b);
        return o;
    }

I keep getting the error

Invalid expression term 'string'

I thought when I put in string, it knows it is the type string. I know it is probably a very simple answer, but I have hit a mental block.

Thank you for any help.

Jon Skeet
people
quotationmark

string is just the name of the type. You want an expression of type Type - in other words, a reference to an instance of Type for the relevant type.

The simplest way to get that is with the typeof operator:

Wrapper.DecompressAndDeserialize(typeof(string), ms);

Another alternative would be to make the method generic:

public static T DecompressAndDeserialize<T>(byte[] data)
{
    byte[] b = Decompress(data);
    return (T) Deserialize(typeof(T), b);
}

And then call it as:

string x = Wrapper.DecompressAndDeserialize<string>(ms);

You could probably change the Deserialize method to be generic, too...

people

See more on this question at Stackoverflow