So... I have this simple model called Employee
:
public class Employee {
public int WorkerID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Salary { get; set; }
}
And I want to "decorate" all properties with AccessLevel
like this:
[AccessLevel(1)]
public class Employee {
[AccessLevel(1)]
public int WorkerID { get; set; }
[AccessLevel(1)]
public string FirstName { get; set; }
[AccessLevel(1)]
public string LastName { get; set; }
[AccessLevel(2)]
public int Salary { get; set; }
}
I've read about Attributes, but still don't know how to create right one and somehow access it like
Employee.Salary.GetAccessLevel();
How can I make that kind of Attribute? Or maybe there is a better way to do this?
You can try to use reflection
get property attribute by GetCustomAttribute
method and use linq to check the auth value.
typeof(Employee)
.GetTypeInfo()
.GetProperties()
.Where(x=>x.GetCustomAttribute<AccessLevel>().val > 1) // here can write your logic.
.Select(x => x.Name);