This will probably throw you for a loop the first time you encounter it. If you have an interface that derives from another interface and you want to get all of the properties (including those from the parent interface) then you cannot simply call GetProperties (like you can do with classes).
Example:
public interface IPerson
{
string FirstName
{
get;
}
string LastName
{
get;
}
}
public interface IManager : IPerson
{
int ManagerID
{
get;
}
int DepartmentID
{
get;
}
}
If you call typeof(IManager).GetProperties() you will only have access to the properties that are directly part of IManager, i.e. ManagerID and DepartmentID. If you need all properties, including those from IPerson, then you must use the GetInterfaces method from Type. Here’s a helper method that does just that:
public static PropertyInfo[] GetAllProperties(Type type)
{
List<Type> typeList = new List<Type>();
typeList.Add(type);
if (type.IsInterface)
{
typeList.AddRange(type.GetInterfaces());
}
List<PropertyInfo> propertyList = new List<PropertyInfo>();
foreach (Type interfaceType in typeList)
{
foreach (PropertyInfo property in interfaceType.GetProperties())
{
propertyList.Add(property);
}
}
return propertyList.ToArray();
}
You can now simply call GetAllProperties(typeof(IManager)) to have all properties returned, including those from IPerson.