Is it possible to access an object\'s private variables using an extension method?
Not recommended, but you could possibly access any private variable of any type using another extension method like so:
public static T GetFieldValue(this object obj, string name) {
var field = obj.GetType().GetField(name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
return (T)field?.GetValue(obj);
}
And then access a private field of an arbitrary type:
Foo foo = new Foo();
string privateBar = foo.GetFieldValue("_bar");