Get Value From Custom Attribute

PHOTO EMBED

Fri Aug 26 2022 07:23:59 GMT+0000 (UTC)

Saved by @HristoT #c#

// Get the properties that have the attribute
var attrProps = modelType.GetProperties().Where(x => Attribute.IsDefined(x, typeof(/*Attribute*/))).ToList();

// Add the property name and the attribute value
var columns = new Dictionary<string, string>();

foreach (var prop in attrProps)
{
    var attr = (/*Attribute*/)prop.GetCustomAttributes(typeof(/*Attribute*/), false).FirstOrDefault();
    columns.Add(prop.Name, attr./*Attribute property*/);
}

// Attribute Example
public class XcelHeaderAttribute : Attribute
{
    public XcelHeaderAttribute(string headerName)
    {
        this.HeaderName = headerName;
    }

    public string HeaderName { get; set; }
}

// Property example
[XcelHeader("Твърда сума")]
public decimal hard_price { get; set; }
content_copyCOPY