属性帮助路由、编译器错误
我创建了一个名为 RouteAttribute
的自定义属性:
[AttributeUsage(AttributeTargets.Property)]
public class RouteAttribute : Attribute
{
public string Url { get; set; }
public bool CheckPhysicalUrlAccess { get; set; }
public RouteValueDictionary Defaults { get; set; }
public RouteValueDictionary Constraints { get; set; }
public RouteValueDictionary DataTokens { get; set; }
}
它用于通过我的 url 帮助程序类上的属性添加路由,该类包含我网站中的 url 列表,因此我有一种简单的方法来管理我的网站 url 。
但添加默认值时遇到问题,出现编译器错误:
[Route("~/MyPage/Home.aspx", new RouteValueDictionary { { "query", "value" } })]
public string HomePage
{
get { return "Home" }
}
为了避免混淆,该值设置为routeurl,物理url来自属性, 原因是,我正在转换一个现有网站,而不是到处更改链接,一旦我完成页面,我就去我的班级并将物理网址更改为新页面
给出错误:
属性参数必须是属性参数类型的常量表达式、typeof 表达式或数组创建表达式
I have created a custom attribute called RouteAttribute
:
[AttributeUsage(AttributeTargets.Property)]
public class RouteAttribute : Attribute
{
public string Url { get; set; }
public bool CheckPhysicalUrlAccess { get; set; }
public RouteValueDictionary Defaults { get; set; }
public RouteValueDictionary Constraints { get; set; }
public RouteValueDictionary DataTokens { get; set; }
}
It is used to add routing via attribute on my url helper class that contains a list of urls in my site, so i have a easy way to manage my site urls.
Having a problem with adding a default though, getting compiler error:
[Route("~/MyPage/Home.aspx", new RouteValueDictionary { { "query", "value" } })]
public string HomePage
{
get { return "Home" }
}
To avoid confusion, the value is set to the routeurl, physical url is from attribute,
reason for this is, I am converting an existing site, and rather than changing links everywhere, once I'm done with page, I go to my class and change the physical url to new page
Giving an error:
An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
属性构造函数的参数值存储在元数据中。这对您可以指定的内容施加了严格的限制。只是简单的值类型,来自 typeof 的 Type 和这些值的简单一维数组。不允许使用代码,这是编译器抱怨的,new 运算符需要代码。
在属性构造函数的主体中可以执行的操作没有任何限制,该代码稍后在反射代码检查属性时运行。建议类似的事情:
这显然需要工作,我不知道字典是什么意思。请注意它是否有副作用或需要资源,您不知道构造属性时的运行时状态。
The argument values for an attribute constructor are stored in metadata. That puts severe restrictions on what you can specify. Just simple value types, a Type from typeof and a simple one dimensional array of these values. No code is allowed, which is what the compiler complains about, the new operator requires code.
There are no constrictions on what you can do in the body of the attribute constructor, that code runs later when reflection code inspects the attribute. Suggesting something similar to this:
This obviously needs work, I have no idea what the dictionary means. Be careful about it having side-effects or requiring resources, you don't know the runtime state when the attribute gets constructed.
该错误准确地告诉您问题所在。
由于
不是常量表达式,不是 typeof 表达式,也不是数组创建表达式,所以这是不合法的。
The error tells you exactly what the problem is.
As
is not a constant expression, not a typeof expression and not an array creation expression, this is not legal.