3

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
    }

私が得ているエラーは次のとおりです。

属性引数は、定数式、typeof 式、または属性パラメーター タイプの配列作成式でなければなりません

カスタム属性を削除すると、コンパイル エラーは発生しません。

4

1 に答える 1

2

これは、VS2010 の C# コンパイラのバグのようです (コードは VS2012 で正常にコンパイルされます)。コンパイラはnull、パラメータのデフォルト値を定数として扱わないようです。

このコードはコンパイルされません:

[IncompleteCodePort()]
interface IGL
{}

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

上記のエラー メッセージ (「属性引数は定数式、typeof 式、または属性パラメーター型の配列作成式である必要があります」) が表示されますが、混乱を招くようにソース コードの場所がありません。

機能する属性の宣言の例:

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)
    {}
}
于 2013-02-24T15:28:08.857 に答える