public class SomeClass
{
public IBy Some1{ get { return By.CssSelector("span[id$=spanEarth]"); } }
public IBy Some2 { get { return By.CssSelector("span[id$=spanWorm]"); } }
public IBy Some3 { get { return By.CssSelector("span[id$=spanJim]"); } }
}
is the class, and I am using reflection in this fashion:
var gridRow = Type.GetType(typeof(SomeOtherClassInSameNamespace).AssemblyQualifiedName.Replace("SomeOtherClassInSameNamespace", "SomeClass"), true, true);
var rowList = gridRow.GetProperties().Where(p => p.PropertyType.Name.Contains("IBy")).ToList();
int i = 0;
foreach (var property in rowList)
{
string test = property.GetValue(gridRow, null).ToString();
}
Which gives a runtime error for an objectType exception. How do I get the values from the list of properties using Reflection?
gridRowis a reference to aTypeobject. The first parameter ofGetValueis meant to be the target object - so it's like you're trying to access theSomeClassproperties on aTypeobject. That's clearly not going to work.While there are hacky ways of evaluating an instance property without having a reference to an instance - so long as the property doesn't use
this- they really are pretty nasty.If a property doesn't need any state within an object, make it a static property instead. At that point you can use
nullas the target, and it'll be fine: