0

I am declaring the custom attribute that accept its property type. For example, let my attribute name as MyCustomAttribute, then

public class MyCustomAttribute : Attribute
{
    public Type PropertyType { get; }

    public MyCustomAttribute(Type propertyType)
    {
        PropertyType = propertyType;
    }
}

And its usage is

enum Gender
{
    Male,
    Female
}

[MyCustom(typeof(Gender))]
public Gender Gender { get; set; }

I want to know that is there a way that pass the property type automatically in C#. Like CallerMemberNameAttribute in RaisePropertyChanged implementation, can I register an attribute like the below code?

[MyCustom()] // typeof(Gender) is omitted
public Gender Gender { get; set; }
Allan Wind
  • 23,068
  • 5
  • 28
  • 38
gk2
  • 67
  • 4
  • When accessing your custom attribute via reflection, you will end up using something like this : ** member.GetCustomAttribute **. So you already know the type of the member ? – Mad hatter Jan 12 '23 at 13:09

0 Answers0