What is the purpose of IsFinal property in C# Reflection?

 Posted by Rajnilari2015 on 9/22/2015 | Category: C# Interview questions | Views: 272 | Points: 40
Answer:

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; }

e.g.

Suppose we have

public class Employee : IMyInterface

{
public int property1 { 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; }

}

And we want to get only the interface property name. Use the below code
typeof(Employee)

.GetProperties()
.Where(p => p.GetAccessors()[0].IsFinal)
.ToList()
.ForEach(i => Console.WriteLine(i.Name));


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response