The best way to invoke every property in the class.

I have a class in the SDK for which every property I'm interested in calling. I know that the only way (I think the only way is) is to use reflection, which most people consider to be slow, etc. (Although I saw articles that illustrate how in some cases this is not as slow as originally intended).

Is there a better way than looping and calling each property in the target class?

Also, why is reflection considered so slow?

+4
source share
3 answers

It might be worth a look at TypeDescriptors. As far as I know, they have some performance advantages over using reflection and work a little differently (for example, they cache metadata). The MSDN article confused me in how it describes how reflection is used by type descriptors, so you may need a more detailed explanation (because the 3rd link may be more useful).

The API for type descriptors is similar to that used for reflection.

Switch to:

Soom debugs the answers to your questions: 1) Due to caching and a slightly different implementation for reflecting TypeDescriptors, I provide performance improvements only with respect to replication.

2) You can get properties and (invoke / set / get?) Properties in one fell swoop. Could this be the case of invoking a method of type invoke and writing a lambda operator to determine any action in the returned collection?

+1
source

You can use reflection to generate C # code, access to which can be obtained directly to all the properties you are interested in. This will be a faster way to make calls.

I think Reflection is a good option for you. It is not so slow.

0
source

I would use reflection to generate code that calls all properties. Then you do not need to worry about slow reflection.

0
source

All Articles