I want to create a custom attribute that when decorated on a property, it "sets" the property to the value of the attribute. In this case, I am reading an excel file and want to map its value to a property.
using System;
namespace Excel.DataBind
{
[AttributeUsage(AttributeTargets.Property)]
public class ExcelDataBindAttribute : Attribute
{
private ExcelReader _excelReader;
public ExcelDataBindAttribute(string rangeAddress)
{
_excelReader = new ExcelReader();
_excelReader.GetExcelValue(rangeAddress);
// some code here to map it to the property it decorates...?
}
}
}
namespace Excel.Models
{
public class Model
{
[ExcelDataBind("A2")]
public string Value { get; set; }
}
}
I'm searching online to find a way to achieve this, but reflection is said as a good approach. But as i'm new to this, i'm not sure if it would be the best approach. Can someone here direct me?
Thank you.