2016-07-20 1 views
0

웹 API에서 사용자 지정 특성을 작성하고 있습니다. OnActionExecuting 메서드에서 매개 변수를 Action 메서드에 전달하고 원본 형식으로 캐스팅하려고합니다. 내가 그웹 APi OnActionExecuting 메서드의 원래 형식으로 매개 변수 캐스팅

public override void OnActionExecuting(HttpActionContext actionContext) 
    { 
     ActionParameters = actionContext.ActionArguments; 
     object param = ActionParameters.First().Value; 


     var actionInfo = actionContext.ActionDescriptor; 
     var parameter = actionInfo.GetParameters().First(); 
     string p = parameter.ParameterType.FullName; 
     Type t = Type.GetType(p + "," + "Models"); //Type of the parameter 
    } 

같은에서 매개 변수를 얻고는 난 그냥 그 매개 변수가 properties.How의 몇 가지를해야합니다 수있어 parameter.I의 정확한 유형을 잘 모릅니다 parameter.Now 하나 있어야하는데이 매개 변수를 원래 유형으로 변환하십시오.

답변

0

ActionArguments는 사전 generic dixtionaryof string 및 object입니다. 객체를 값으로 저장하므로 예상 한 유형으로 값을 변환해야합니다.

public override void OnActionExecuting(HttpActionContext actionContext) 
{ 
    ActionParameters = actionContext.ActionArguments; 
    if (actionContext.ActionArguments.ContainsKey("Key1")) 
    { 
     var val = actionContext.ActionArguments["Key1"] as string; 
    }   
}