How can i loop over an object's properties and get the value of a property.
i have an object that has several properties filled with data. the user specifies what property he wants to view by providing the name of the property, and i need to search for the property in the object and return its value to the user.
How can i achieve this?
i wrote the following code to get the property but couldn't get the value of that prop:
public object FindObject(object OBJ, string PropName)
{
PropertyInfo[] pi = OBJ.GetType().GetProperties();
for (int i = 0; i < pi.Length; i++)
{
if (pi[i].PropertyType.Name.Contains(PropName))
return pi[i];//pi[i] is the property the user is searching for,
// how can i get its value?
}
return new object();
}