tags:

views:

735

answers:

4

I am getting an error when trying to use a Type parameter when specifying the Type for a generic method.

Error: 'JsonFilter.JsonDataType' is a 'property' but is used like a 'type'

public class JsonFilter : ActionFilterAttribute
{
    public Type JsonDataType { get; set; }
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        ...
        JavaScriptSerializer jss = new JavaScriptSerializer();
        var result = jss.Deserialize<JsonDataType>(inputContent);//Error here
        ...

New Code

...
JavaScriptSerializer jss = new JavaScriptSerializer();
MethodInfo method = jss.GetType()
               .GetMethod("Deserialize")
               .MakeGenericMethod(new Type[] { JsonDataType });
var result = method.Invoke(jss, new object[] { inputContent });
filterContext.ActionParameters[Param] = result;
...

Reflection saves the day. Thanks @Jason for the explanation that when type is specified as part of generic method ( <Typename> ), then it gets compiled into bytes. Whereas when as property, it can be any type, only determinable at runtime.

UPDATE

For this specific problem, the following code is more concise.

var o = new DataContractJsonSerializer(JsonDataType).ReadObject(
    filterContext.HttpContext.Request.InputStream);
filterContext.ActionParameters[Param] = o;
+11  A: 

The error

Error: 'JsonFilter.JsonDataType' is a 'property' but is used like a 'type'

is telling you exactly the problem.

var result = jss.Deserialize<JsonDataType>(inputContent);

Here, you are trying to pass JsonDataType as a type parameter to the generic method JavaScriptSerializer.Deserialize<T>

but here

public Type JsonDataType { get; set; }

you declared JsonDataType as a property of type Type, but not as a type. To use a generic method you need to pass a type parameter (or, in some cases, let the compiler infer one). For example

var result = jss.Deserialize<string>(inputContent);

would be correct usage as string is a type.

Now, if you absolutely want to use the type that is represented by JsonDataType you can use reflection.

MethodInfo generic = typeof(JavaScriptSerializer).GetMethod("Deserialize")
                                                 .GetGenericMethodDefinition();
MethodInfo closed = generic.MakeGenericMethod(new [] { JsonDataType });
closed.Invoke(jss, new object[] { inputContent });
Jason
True, true. This is a good explanation of the problem.
Greg Ogle
Hello, I am receiving Ambiguous match found exception on the following line:typeof(JavaScriptSerializer).GetMethod("Deserialize") .GetGenericMethodDefinition();Do you have an idea on why the error is thrown? :(
Genady Sergeev
+1  A: 

Try this something along these lines. I'll update it for your code in a sec.

typeof(IDependencyResolver).GetMethod("Resolve", new Type[] { })
                .MakeGenericMethod(type)
                .Invoke(this, null);

Here is a rough draft for you.

typeof(JavaScriptSerializer).GetMethod("Deserialize", new Type[] {} /* param types */)
    .MakeGenericMethod(JsonDataType).Invoke(jss, /*params*/);

Basically it uses reflection to invoke the generic method.

Daniel A. White
Why the downvote?
Daniel A. White
+3  A: 

You cannot pass a generic type argument in this way - try something like this:

public class JsonFilter<T> : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        JavaScriptSerializer jss = new JavaScriptSerializer();
        var result = jss.Deserialize<T>(inputContent);//Error here
    }
}

Rather than setting the type in a property, make your JsonFilter class generic so that you can pass the generic type argument through to the JavaScriptSerializer.

Andrew Hare
"A generic type cannot derive from 'ActionFilterAttribute' because it is an attribute class" ... Can't make the attribute generic.
Greg Ogle
+4  A: 
LukeH
"A generic type cannot derive from 'ActionFilterAttribute' because it is an attribute class" ... Can't make the attribute generic.
Greg Ogle
@Greg, In that case then I guess the updated answer, using reflection, is the way to go.
LukeH
Yes. Though, as you likely know, in other cases, what you originally put is obviously better.
Greg Ogle