属性参数必须是常量前pression [英] Attribute argument must be a constant expression

查看:211
本文介绍了属性参数必须是常量前pression的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否为什么下面的接口定义在Visual Studio 2010中的错误编译有人请解释一下吗?

Could someone please explain to me why the following interface definition compiles with error in Visual Studio 2010?

   [IncompleteCodePort(SourceOriginType.Other, "This should be a GL abstraction depending on what OpenGL API will be used")]
    public interface IGL
    {
        /// <summary>
        /// Returns true if provided function is available or supported by graphics API
        /// </summary>
        /// <param name="funcName"></param>
        /// <returns></returns>
        bool IsFunctionAvailable(string funcName);

        /// <summary>
        /// Returns true if provided function is supported as extension by graphics API
        /// </summary>
        /// <param name="funcName"></param>
        /// <returns></returns>
        bool IsExtensionAvailable(string funcName);
    }



public class IncompleteCodePortAttribute : Attribute
    {
        public SourceOriginType SourceOriginType { get; private set; }
        public string SourceUrl { get; private set; }
        public string Reason { get; private set; }


        public IncompleteCodePortAttribute(SourceOriginType originType, string reason, string sourceUrl = null)
        {
            SourceOriginType = originType;
            SourceUrl = sourceUrl;
            Reason = reason;
        }
    }

    public enum SourceOriginType
    {
        CodePlex,
        WorldWindJdk,
        StackOverflow,
        Other
    }

和错误我得到的是:

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

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

如果我删除了自定义属性,我没有得到任何编译错误。

If I remove the custom attribute, I get no compile errors.

推荐答案

这似乎是在VS2010(的code下VS2012编译罚款)C#编译器的错误。它看起来像编译器不会在参数的默认值对待恒定。

This seems to be a bug in the C# compiler in VS2010 (the code compiles fine under VS2012). It looks like the compiler doesn't treat the null in the default value of the parameter as constant.

这code不能编译:

[IncompleteCodePort()]
interface IGL
{}

class IncompleteCodePortAttribute : Attribute
{
    public IncompleteCodePortAttribute(string sourceUrl = null)
    {}
}

与提到的错误消息(的属性参数必须是常量前pression,属性参数类型的typeof前pression或数组创建前pression),但容易使人误解没有源code的位置。

with the mentioned error message ("An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type"), but confusingly without a source code location.

这做的工作属性的声明中的一些例子:

Some examples of declaration of the attribute that do work:

class IncompleteCodePortAttribute : Attribute
{
    public IncompleteCodePortAttribute(string sourceUrl = "")
    {}
}

class IncompleteCodePortAttribute : Attribute
{
    private const string Null = null;

    public IncompleteCodePortAttribute(string sourceUrl = Null)
    {}
}

class IncompleteCodePortAttribute : Attribute
{
    public IncompleteCodePortAttribute()
        : this(null)
    {}

    public IncompleteCodePortAttribute(string sourceUrl)
    {}
}

这篇关于属性参数必须是常量前pression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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