方法中属性的访问属性

时间:2014-09-24 11:41:23

标签: c# attributes

我一直在寻找'脚本'来帮助我从方法的属性访问运行时设置的属性。我实现了一些东西,但在运行时设置的属性只有它们的默认值。这甚至可能吗?我告诉你我的代码:

首先,如果全部,我有一个界面:

public interface IRootController{}

我的Controller继承自实现此接口的基本控制器。 在这个控制器中,我有一个带有属性的方法:

[LowerAndUpperDateTime]
public HttpResponseMessage GetAllLastResponding(string cultureinfo, Guid campaignUniqueID, int pageIndex, int pageSize, string lowerDateTime, string upperDateTime, BreakDown breakDown)
{
    var test = this.GetAttribute<LowerAndUpperDateTimeAttribute>(GetCurrentMethod());
    return null;

}

属性的代码如下:

public class LowerAndUpperDateTimeAttribute : ActionFilterAttribute
    {
        public DateTime UpperDateTime;
        public DateTime LowerDateTime;
        public string test = "sfsdfdsf";
        public override void OnActionExecuting(HttpActionContext actionContext)
        {

            if (actionContext.ActionArguments.Count < 1)
            {
                actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.BadRequest,
                    new[]{Errors.ObjectRequired});
            }
            else
            {
                try
                {

                    var lowerDateTime = string.Empty;
                    var upperDateTime = string.Empty;

                    //controleren of de arguments bestaan
                    if (actionContext.ActionArguments.ContainsKey("lowerDateTime"))
                        lowerDateTime = actionContext.ActionArguments["lowerDateTime"].ToString();
                    if (actionContext.ActionArguments.ContainsKey("upperDateTime"))
                        upperDateTime = actionContext.ActionArguments["upperDateTime"].ToString();
                    if (string.IsNullOrEmpty(lowerDateTime) || string.IsNullOrEmpty(upperDateTime))
                    {
                        actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.BadRequest,
                            new[]{Errors.EmptyDateTimes});
                    }
                    else{

                        try
                        {
                            LowerDateTime = DateTime.ParseExact(lowerDateTime, "yyyyMMddHHmmss", CultureInfo.InvariantCulture);
                            UpperDateTime = DateTime.ParseExact(upperDateTime, "yyyyMMddHHmmss", CultureInfo.InvariantCulture);
                            if (LowerDateTime > UpperDateTime)
                                actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.BadRequest,
                                    new[]{Errors.LowerDateBiggerTheUpperDate});
                        }
                        catch (Exception e)
                        {
                            actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.BadRequest,
                            new[]{e.Message});
                        }
                    }
                }
                catch
                {
                    actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.BadRequest, new[]{Errors.NoAccess});
                }
            }

        }

    }

我创建的扩展方法在我的控制器方法中用于访问属性,如下所示:

public static TAttribute GetAttribute<TAttribute>(this IRootController currentclass, string methodname)where TAttribute : Attribute
{
    return (TAttribute)currentclass.GetType().GetMethod(methodname).GetCustomAttributes(typeof(TAttribute), false).FirstOrDefault();
}

所以这样我可以访问该属性,但是两个日期时间都有默认值,即使它们已经设置了。

我甚至可能在这里尝试,如果是的话,我错在哪里?

谢谢!

0 个答案:

没有答案