'constructor'不是公认的属性位置

问题描述 投票:0回答:1

尝试编译以下代码后:

public sealed class Program
{
    [constructor: CLSCompliant(false)]
    public Program()
    {
    }
}

我收到以下错误:

'constructor'不是公认的属性位置。此声明的有效属性位置为“方法”。此块中的所有属性将被忽略。 [Console.NET] csharp(CS0658)

我知道存在以下位置:assemblymodulemethodparameterreturn等。因此,我的猜测是constructor也应存在(因为我们也可以将构造函数作为属性的目标)。但似乎这里并非如此。

而且,我无法在MSDN上找到已识别属性位置的完整列表。因此,如果有人提供指向MSDN上位置列表的链接,这将很有帮助。

我对constructor位置的猜测是基于我在通过CLR通过C#书中遇到以下代码示例之后的:

using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
[assembly: CLSCompliant(true)]
[Serializable]
[DefaultMemberAttribute("Main")]
[DebuggerDisplayAttribute("Richter", Name = "Jeff", Target = typeof(Program))]
public sealed class Program
{
    [Conditional("Debug")]
    [Conditional("Release")]
    public void DoSomething() { }
    public Program()
    {
    }
    [CLSCompliant(true)]
    [STAThread]
    public static void Main()
    {
        // Show the set of attributes applied to this type
        ShowAttributes(typeof(Program));
        // Get the set of methods associated with the type
        var members =
        from m in typeof(Program).GetTypeInfo().DeclaredMembers.OfType<MethodBase>()
        where m.IsPublic
        select m;
        foreach (MemberInfo member in members)
        {
            // Show the set of attributes applied to this member
            ShowAttributes(member);
        }
    }
    private static void ShowAttributes(MemberInfo attributeTarget)
    {
        var attributes = attributeTarget.GetCustomAttributes<Attribute>();

        Console.WriteLine("Attributes applied to {0}: {1}",
        attributeTarget.Name, (attributes.Count() == 0 ? "None" : String.Empty));
        foreach (Attribute attribute in attributes)
        {
            // Display the type of each applied attribute
            Console.WriteLine(" {0}", attribute.GetType().ToString());

            if (attribute is DefaultMemberAttribute)
                Console.WriteLine(" MemberName={0}",
                ((DefaultMemberAttribute)attribute).MemberName);
            if (attribute is ConditionalAttribute)
                Console.WriteLine(" ConditionString={0}",
                ((ConditionalAttribute)attribute).ConditionString);
            if (attribute is CLSCompliantAttribute)
                Console.WriteLine(" IsCompliant={0}",
                ((CLSCompliantAttribute)attribute).IsCompliant);
            DebuggerDisplayAttribute dda = attribute as DebuggerDisplayAttribute;
            if (dda != null)
            {
            Console.WriteLine(" Value={0}, Name={1}, Target={2}",
 dda.Value, dda.Name, dda.Target);
            }
        }
        Console.WriteLine();
    }
}

并且该程序的输出如下:

Attributes applied to Program:
 System.SerializableAttribute
 System.Diagnostics.DebuggerDisplayAttribute
 Value=Richter, Name=Jeff, Target=Program
 System.Reflection.DefaultMemberAttribute
 MemberName=Main

Attributes applied to DoSomething:
 System.Diagnostics.ConditionalAttribute
 ConditionString=Release
 System.Diagnostics.ConditionalAttribute
 ConditionString=Debug

Attributes applied to Main:
 System.CLSCompliantAttribute
 IsCompliant=True
 System.STAThreadAttribute

Attributes applied to .ctor: None 

输出清楚表明,构造函数与类和方法的区别。并且由于存在位置classmethod,因此我也希望constructor位置也存在。

我仅出于学习目的需要它。

c# custom-attributes
1个回答
0
投票
属性目标列表出现在两个位置:

  • 所以我们为什么要有“方法”而不是“构造函数”目标(我的解释是,我不知道官方的推理):

      “方法”适用于方法和属性。它包括构造函数,作为方法的特殊变体。因此,有一种方法可以允许在构造函数上而不是在程序集或字段上使用属性。
  • 当属性应用于构造函数时,默认的“方法”目标没有其他可能的选择,因为构造函数仅与目标列表中的“方法”选项匹配(例如,与自动属性不同,可以将属性视为目标“方法”或“属性”甚至“字段”)
  • 大概没有有用的情况,必须将属性限制为仅构造函数。
  • 旁注:由于属性本身通常不执行任何操作,所以属性目标主要是限制设置属性但不影响属性的潜在情况。如果您想“目标”只是构造函数,那么一些好的命名就足够了。如果您真的只想限制构造函数,则可以在启动时检查所有加载的类型是否使用了自定义属性,/首先需要检查您的自定义属性。
  • © www.soinside.com 2019 - 2024. All rights reserved.