Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamic Typing a Generic Class
    text
    copied!<p>I have an object that's being automatically serialized by the WebAPI, but I wanted to wrap it to provide context to my data. An example would be:</p> <pre><code>public class SecureModel&lt;T&gt; { public string Info { get; set; } public T Data { get; set; } } </code></pre> <p>This serializes/deserializes no problem with my JSON requests and all is well. However, I want to verify some of that Info before I allow the request to finish executing, so I've added an ActionFilter where I'm retrieving parameters of my POST.</p> <pre><code>public class MyAuth : System.Web.Http.Filters.ActionFilterAttribute { public override void OnActionExecuting(HttpActionContext actionContext) { var arg = actionContext.ActionArguments.FirstOrDefault().Value; // arg: get the Info? // if the info isn't correct, return a specific Response. } } </code></pre> <p>When I examine the argument, it's exactly what I need, however I can't figure out how to weakly type it to SecureModel without specifying the <em>actual</em> generic type. I feel like I should be able to case this to SecureModel&lt;object&gt; and properly access the root, however it does not allow that. So far, I can only get the data I need using:</p> <pre><code>var notStrongEnough = arg.GetType().GetProperty("Info"); </code></pre> <p>However, I'd prefer not to use Reflection for this and I am having a hard time moving forward without an answer. My alternative in this situation would be to change Data to a string and serialize/deserialize the JSON object manually, but that defeats some of the purpose of using Web API over MVC3.</p> <p><strong>Note:</strong> Changing the design to have SecureModel as the base class adds challenges to some of the hashing I am performing on the data, so I'd prefer to not go down that path either.</p> <p>Thanks!</p> <p><strong>Edit:</strong> Misphrased title.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload