How to obtain only interface properties using reflection?

Rajnilari2015
Posted by Rajnilari2015 under C# category on | Points: 40 | Views : 934
Suppose we have a class as under

 public class Employee : IMyInterface
{
public int property1 { get; set; }
public string property2{get;set;}
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
public virtual int EmployeeAge { private set; get; } //read-only property
public virtual string EmployeeSalary { set; private get; } //write-only property
}

interface IMyInterface
{
int property1 { get; set; }
string property2 { get; set; }

}

The intention is to obtain only the interface properties.The below code does the work
 typeof(Employee)
.GetProperties()
.Where(p => p.GetAccessors()[0].IsFinal)
.ToList()
.ForEach(i => Console.WriteLine(i.Name));


The IsFinal property of MethodBase class gets a value indicating whether this method is final.It returns true if this method is final; otherwise, false. It is defined as

public bool IsFinal { get; }


More Info : https://msdn.microsoft.com/en-us/library/system.reflection.methodbase.isfinal(v=vs.110).aspx

Comments or Responses

Login to post response