I have recently made the transition from a Java web developer to a C# application developer doing mostly WPF applications. I used to use Spring MVC with Java, where a lot of the code structure was preempted and setup for me. Since I have made the transition to WPF, my applications are reliant on my ability to setup reusable, decoupled code.
I have been trying to improve my skills with certain areas of design including Generics,Design Patterns and Reflection.
I am well aware of what all these are and for Generics and Design patterns, I am fairly well at applying what I consider best practices(although this is up in the air).
What I am not too familiar with is reflection. I know what reflection is, and I know how to do it to do tasks such as dynamic assembly loading,method and class invocation,etc.
What I do not understand is examples how this might be able to help me.
I constantly hear how reflection can be really helpful if you know how to use it. When I try to research the topic, I only find tutorials and reference of how to do it, instead of the good that it can be used for.
My question is what can I look to as a WPF Developer in using reflection for that will help me, and/or is there a place, or reference that can give me more then just the syntax of using reflection, but also real world examples and/or best practices?
Reflection is a bit like anonymous methods: It’s really hard to see where it’s useful until you see the first use case; after that, it’s usually easier to figure out.
Here’s an actual example from code I wrote about a year and a half ago. For a student project at my university, we were writing a bot in C#, for the purpose of playing Texas Hold ’em. We also wrote an event-based engine which handled the game itself, and included network support to allow both humans and computers to join in from any number of different machines. To allow us to see what was going on, we wrote a separate GUI application which was used to start, join, and participate in games. All players would get added through this application, bots and humans alike.
We implemented our bot to act based on a model, but as we progressed through the project, we made changes to that model, essentially creating different versions of that bot. In our system, we had one class per version of our model.
Once you have different versions, it’s nice to be able to compare them against each other to see if your changes have any effect. To do that, we tagged each of our computer player classes with a custom attribute, which we called
CPUAttribute.That’s where reflection came into play: on startup, our GUI would use reflection look at the appropriate assembly and find all of the types we had tagged. Those types would then get added to a dropdown box, and reflection was used to instantiate the selected type when you created the player.