62,046
社区成员
发帖
与我相关
我的任务
分享
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
public class APIFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
//string a = "统一处理";
//var q = HttpContext.Current.Session["a"] == null ? "" : HttpContext.Current.Session["a"].ToString();
var q = (Father)actionContext.ActionArguments["f"];
Strategy s = new Strategy(q);
string result = s.Done();
}
}
//简单的策略模式
public class Strategy
{
Father f;
public Strategy(Father f) { this.f = f; }
public string Done()
{
return f.Done();
}
}
//父类
public class Father
{
public string name { get; set; }
public int age { get; set; }
public virtual string Done()
{
return "我是老爸";
}
}
//子类
public class Son1 : Father
{
public string hobby { get; set; }
public override string Done()
{
return "我是Son1,我的属性是hobby";
}
}
//子类
public class Son2 : Father
{
public string test { get; set; }
public override string Done()
{
return "我是Son2,我的属性是test";
}
}
[APIFilter]
[HttpPost]
public string Post(Son1 f)
{
return "123";
}