mardi 29 août 2017

Why are Reflection's GetProperty() or GetField() not instance or extension methods?

I'm fairly new to Reflection in C#, and I think I understand how to use it to solve some problems.

However, what I find confusing is the syntax for methods like GetProperty() or GetField().

To get an instance field, you have to do typeof(MyClass).GetField("fieldName").GetValue(myClassInstance). Which is not the most straightforward thing.

Wouldn't it make more sense to get instance fields or properties by using something like an extension method? Something like:

myClassInstance.GetField("fieldName").Value

And use the previous example for things like static fields/properties/methods.

It just feels more natural than the first example, where you must pass your class instance.

Again, I am new to Reflection, so there might be some disadvantages I'm overlooking.





Aucun commentaire:

Enregistrer un commentaire