How to Get the Value of a Private Property with Reflection

Get private property of a private property using reflection

You can use the GetProperty method along with the NonPublic and Instance binding flags.

Assuming you have an instance of Foo, f:

PropertyInfo prop =
typeof(Foo).GetProperty("FooBar", BindingFlags.NonPublic | BindingFlags.Instance);

MethodInfo getter = prop.GetGetMethod(nonPublic: true);
object bar = getter.Invoke(f, null);

Update:

If you want to access the Str property, just do the same thing on the bar object that's retrieved:

PropertyInfo strProperty = 
bar.GetType().GetProperty("Str", BindingFlags.NonPublic | BindingFlags.Instance);

MethodInfo strGetter = strProperty.GetGetMethod(nonPublic: true);

string val = (string)strGetter.Invoke(bar, null);

Can I get the value of a private property with Reflection?

class A
{
private $b = 'c';
}

$obj = new A();

$r = new ReflectionObject($obj);
$p = $r->getProperty('b');
$p->setAccessible(true); // <--- you set the property to public before you read the value

var_dump($p->getValue($obj));

How to get the value of private field using reflection?

As others have said, since the field is private you should not be trying to get it with normal code.
The only time this is acceptable is during unit testing, and even then you need a good reason to do it (such as setting a private variable to null so that code in an exception block will be hit and can be tested).

You could use something like the method below to get the field:

/// <summary>
/// Uses reflection to get the field value from an object.
/// </summary>
///
/// <param name="type">The instance type.</param>
/// <param name="instance">The instance object.</param>
/// <param name="fieldName">The field's name which is to be fetched.</param>
///
/// <returns>The field value from the object.</returns>
internal static object GetInstanceField(Type type, object instance, string fieldName)
{
BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
| BindingFlags.Static;
FieldInfo field = type.GetField(fieldName, bindFlags);
return field.GetValue(instance);
}

So you could call this like:

string str = GetInstanceField(typeof(YourClass), instance, "someString") as string;

Again, this should not be used in most cases.

How to call Method of Private Property using Reflection in c#?

clsBUS_DEMO prop_DEMO is not a property, it's a Field. You need to use GetFields to get it and also since it's private you need to pass BindingFlags.NonPublic and BindingFlags.Instance.

Here is a sample of how you should do it

 static void Main(string[] args)
{
Bar bar = new Bar();
Foo foo = (Foo)bar.GetType().GetField("a", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(bar);
foo.GetType().GetMethod("ShowMessage").Invoke(foo,new object[] { });
}
public class Bar
{
private Foo a;
public Bar()
{
a = new Foo();
}
}
public class Foo
{
public void ShowMessage()
{
Console.WriteLine("Hello World!");
}
}

Find a private field with Reflection?

Use BindingFlags.NonPublic and BindingFlags.Instance flags

FieldInfo[] fields = myType.GetFields(
BindingFlags.NonPublic |
BindingFlags.Instance);

Set private field value with reflection

To access a private field you need to set Field::setAccessible to true. You can pull the field off the super class. This code works:

Class<?> clazz = Child.class;
Object cc = clazz.newInstance();

Field f1 = cc.getClass().getSuperclass().getDeclaredField("a_field");
f1.setAccessible(true);
f1.set(cc, "reflecting on life");
String str1 = (String) f1.get(cc);
System.out.println("field: " + str1);

.NET Get private property via Reflection

The two privates you refer to are fields, not properties, naturally you won't find them with GetProperties (you can use GetFields for that).

Is it possible to set private property via reflection?

t.GetProperty("CreatedOn")
.SetValue(obj, new DateTime(2009, 10, 14), null);

EDIT: Since the property itself is public, you apparently don't need to use BindingFlags.NonPublic to find it. Calling SetValue despite the the setter having less accessibility still does what you expect.



Related Topics



Leave a reply



Submit