C#成员后的自定义属性声明

C#成员后的自定义属性声明,c#,custom-attributes,C#,Custom Attributes,有人知道是否可以在成员之后而不是在引用/代码示例中通常显示的之前声明属性吗 例如: public class MarkerAttribute: Attribute { public MarkerAttribute(): base() { } } public class TheClass { public int PropertyWithNoAttribute1 { get; set; } [Marker] public int PropertyWithAtt

有人知道是否可以在成员之后而不是在引用/代码示例中通常显示的之前声明属性吗

例如:

public class MarkerAttribute: Attribute
{
    public MarkerAttribute(): base() { }
}

public class TheClass
{
    public int PropertyWithNoAttribute1 { get; set; }

    [Marker]
    public int PropertyWithAttribute { get; set; }

    public int PropertyWithNoAttribute2 { get; set; }
}
在这里,出于各种不有趣的原因,我想这样表示同一个类(这相当于标准表示):

有人知道是否有一些特殊的语法指示,以便可以像这样编写同一个类吗

public class TheClass
{
    public int PropertyWithNoAttribute1 { get; set; }

    public int PropertyWithAttribute { get; set; } [Marker]

    public int PropertyWithNoAttribute2 { get; set; }
}
我已经在下面的程序中对此进行了测试,不幸的是它不起作用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ScratchPad
{
    public class MarkerAttribute: Attribute
    {
        public MarkerAttribute(): base() { }
    }

    public class TheClass
    {
        public int PropertyWithNoAttribute1 { get; set; }

        public int PropertyWithAttribute { get; set; } [Marker]

        public int PropertyWithNoAttribute2 { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            TheClass a = new TheClass();

            System.Reflection.PropertyInfo PropertyWithNoAttribute1_Info =  a.GetType().GetProperty("PropertyWithNoAttribute1");
            System.Reflection.PropertyInfo PropertyWithAttribute_Info =     a.GetType().GetProperty("PropertyWithAttribute");
            System.Reflection.PropertyInfo PropertyWithNoAttribute2_Info =  a.GetType().GetProperty("PropertyWithNoAttribute2");

            object[] attributes = PropertyWithNoAttribute1_Info.GetCustomAttributes(typeof(MarkerAttribute), false);
            if (attributes.Length > 0)
            {
                System.Console.WriteLine("{0} has attribute {1}", PropertyWithNoAttribute1_Info.Name, attributes[0].GetType().Name);
            }

            attributes = PropertyWithAttribute_Info.GetCustomAttributes(typeof(MarkerAttribute), false);
            if (attributes.Length > 0)
            {
                System.Console.WriteLine("{0} has attribute {1}", PropertyWithAttribute_Info.Name, attributes[0].GetType().Name);
            }

            attributes = PropertyWithNoAttribute2_Info.GetCustomAttributes(typeof(MarkerAttribute), false);
            if (attributes.Length > 0)
            {
                System.Console.WriteLine("{0} has attribute {1}", PropertyWithNoAttribute2_Info.Name, attributes[0].GetType().Name);
            }
        }
    }
}

谢谢你

不,你不能。属性就在你所赋予的“元素”之前。答案是否定的。这不是c#的规范,编译器会永远犹豫,或者至少直到你说服某人将其添加到语言中。正如其他人所说,这是不可能的,您所做的只是将属性添加到属性PropertyWithNoAttribute2而不是PropertyWithAttribute。因此,如果在PropertyWithAttribute属性之后没有成员,这甚至不会编译。不,你不能。属性就在你所赋予的“元素”之前。答案是否定的。这不是c#的规范,编译器会永远犹豫,或者至少直到你说服某人将其添加到语言中。正如其他人所说,这是不可能的,您所做的只是将属性添加到属性PropertyWithNoAttribute2而不是PropertyWithAttribute。因此,如果在PropertyWithAttribute属性之后没有成员,这甚至不会编译。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ScratchPad
{
    public class MarkerAttribute: Attribute
    {
        public MarkerAttribute(): base() { }
    }

    public class TheClass
    {
        public int PropertyWithNoAttribute1 { get; set; }

        public int PropertyWithAttribute { get; set; } [Marker]

        public int PropertyWithNoAttribute2 { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            TheClass a = new TheClass();

            System.Reflection.PropertyInfo PropertyWithNoAttribute1_Info =  a.GetType().GetProperty("PropertyWithNoAttribute1");
            System.Reflection.PropertyInfo PropertyWithAttribute_Info =     a.GetType().GetProperty("PropertyWithAttribute");
            System.Reflection.PropertyInfo PropertyWithNoAttribute2_Info =  a.GetType().GetProperty("PropertyWithNoAttribute2");

            object[] attributes = PropertyWithNoAttribute1_Info.GetCustomAttributes(typeof(MarkerAttribute), false);
            if (attributes.Length > 0)
            {
                System.Console.WriteLine("{0} has attribute {1}", PropertyWithNoAttribute1_Info.Name, attributes[0].GetType().Name);
            }

            attributes = PropertyWithAttribute_Info.GetCustomAttributes(typeof(MarkerAttribute), false);
            if (attributes.Length > 0)
            {
                System.Console.WriteLine("{0} has attribute {1}", PropertyWithAttribute_Info.Name, attributes[0].GetType().Name);
            }

            attributes = PropertyWithNoAttribute2_Info.GetCustomAttributes(typeof(MarkerAttribute), false);
            if (attributes.Length > 0)
            {
                System.Console.WriteLine("{0} has attribute {1}", PropertyWithNoAttribute2_Info.Name, attributes[0].GetType().Name);
            }
        }
    }
}