I did a custom attribute for fields that receives a string. Then I'm using the custom attribute in an enum. I need to get the result of "MyMethod" from FieldAttribute, but It doesn't return the string,
Here's what I have:
Enum and attribute:
public enum CustomEnum
{
[Field("first field")]
Field1,
[Field("second field")]
Field2,
[Field("third field")]
Field3
}
[AttributeUsage(AttributeTargets.Field)]
public class FieldAttribute : Attribute
{
private string name;
public FieldAttribute(string name)
{
this.name = name;
}
public string LastName { get; set; }
public string MyMethod()
{
return this.name + this.LastName;
}
}
Usage:
public class Program
{
public static void Main(string[] args)
{
PrintInfo(typeof(CustomEnum));
}
public static void PrintInfo(Type t)
{
Console.WriteLine($"Information for {t}");
Attribute[] attrs = Attribute.GetCustomAttributes(t);
foreach (Attribute attr in attrs)
{
if (attr is FieldAttribute)
{
FieldAttribute a = (FieldAttribute)attr;
Console.WriteLine($" {a.MyMethod()}");
}
}
}
}
Some help please!!!