对于属性构造函数的默认值? [英] Default value for attribute constructor?

查看:326
本文介绍了对于属性构造函数的默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到这个错误,

错误CS0182:属性参数必须是一个常量前pression,属性参数类型的typeof前pression或数组创建前pression

error CS0182: An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

当我尝试写这样的事

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class UrlAttribute : Attribute
{
    public UrlAttribute(string pattern, string name=null)
    {
        // ...

它甚至没有显示行号,但它消失时,我拿出了 = NULL 位。

其实,错误只发生的两个的时候我提供一个默认值的的依靠它(即我忽略它)像这样

Actually, the error only occurs both when I provide a default value and rely on it (i.e., I omit it) like so

    [Url("/index")]

我很好奇,想知道为什么吗?如何为null不是一个常量前pression?

I'm curious to know why this? How is "null" not a constant expression?

推荐答案

我打电话'错误'。

我希望你不介意,我已报告的错误给微软。

I hope you don't mind, I reported the bug to Microsoft.

更新:

UPDATE:

今天我收到了来自微软的反馈之后,加上强调。

I received the following feedback from Microsoft today, emphasis added.

报告此问题你已经感谢
  与Visual Studio碰到的问题!

Thanks for reporting this issue you've encountered with Visual Studio!

我们已经搞掂可选字符串
  在我们的code的属性参数。
  你会看到这个修复程序的版本
  Visual Studio 2010的VS之后。

We've fixed up optional string parameters on attributes in our code. You'll see this fix in the version of Visual Studio after VS 2010.

亚历克斯·特纳

项目经理

Visual Basic和C#编译器

Visual Basic and C# Compiler

这篇关于对于属性构造函数的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆