Question: I basically have something like this:

```
void Foo(Type ty)
{
    var result = serializer.Deserialize<ty>(inputContent);
}

Foo(typeof(Person));

```

The `Deserialize<ty>` doesn't work because it expects `Deserialize<Person>` instead. How do I work around this?

I'd also like to understand how generics work and why it won't accept `ty` which is `typeof(Person)`.

EDIT: I ought to have mentioned that this is a contrived example. I cannot actually change the signature of the function because it implements an interface.

EDIT: serializer is a JavascriptSerializer and implemented as an action filter here. It is called thusly:

```
[JsonFilter(Param="test", JsonDataType=typeof(Person))]

```

Solution
--------

Based on Marc and Anton's answers:

```
var result = typeof(JavaScriptSerializer).GetMethod("Deserialize")
                 .MakeGenericMethod(JsonDataType)
                 .Invoke(serializer, new object[] { inputContent });

```

Answer: 
Which serializer is that? If you only know the `Type` at runtime (not compile time), and it doesn't have a non-generic API, then you might have to use `MakeGenericMethod`:

```
void Foo(Type ty)
{
    object result = typeof(ContainingClass).GetMethod("Bar").
        .MakeGenericMethod(ty).Invoke(null, new object[] {inputContent});
}
public static T Bar<T>(SomeType inputContent) {
    return serializer.Deserialize<T>(inputContent);
}

```
In this case, just do this:

```
void Foo<ty>()
{
    var result = serializer.Deserialize<ty>(inputContent);
}

Foo<Person>();

```

Otherwise, you need to call the generic method late-bound, since you have to get the correct generic method for it first (it is not known at compile time). Have a look at the [MethodInfo.MakeGenericMethod](http://msdn.microsoft.com/de-de/library/system.reflection.methodinfo.makegenericmethod(VS.95).aspx) method.