Use Reflection to dynamically get private fields or properties. The BindingFlags are important on private fields.
class ReflectionTest
{
//Private fields
private String stringField = "stringField string";
private Int32 int32Field = 12345;
//Public properties
public String StringProperty { get; set; }
public Int32 Int32Property { get; set; }
public Object GetFieldFromName(String fieldName)
{
return this.GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance).GetValue(this);
}
public Object GetPropertyFromName(String propertyName)
{
return this.GetType().GetProperty(propertyName).GetValue(this, null);
}
}
Fork
0 Feedback
You must log in before you can give any feedback
You must log in before you can post a comment


553
0




Mark '.net' tag as 'like'
Mark '.net' tag as 'ignore'