vendredi 27 novembre 2015

Why use PrivateObject when we can do the same with pure reflection

I read this thread and I wonder why anyone should use PrivateObject. I can´t see any value that cannot be achieved by pure reflection in as many steps. However it adds a further dependency to my project. Furthermore PrivateObject also uses reflection.

Are there any usings for that class in favour over Reflection? Consider this:

var myProp = typeof(MyClass).GetProperty("Name", BindingFlags.Instance | BindingFlags.NonPublic);

over

var myProp = new PrivateObject(typeof(MyClass)).GetFieldOrProperty("Name");

I notice there is a pretty small difference in that I won´t need to know the actual access-modifier, but I can´t see any further advantage on one over the other? Do you?





Aucun commentaire:

Enregistrer un commentaire