PropertyCollections are a method of encapsulating a group of property values. Using PropertyCollections can simplify and make code more readable.
The following example creates a Sprite and sets its properties through a PropertyCollection.
Unlike assigning properties using regular code the values assigned to properties through PropertyCollections must match the type exactly. For example the following code is acceptable:
The value of 3 is an integer value, as opposed to 3.0f, but the compiler casts the value to a float without any problem. However, the following code would result in a crash:
Instead, XVelocity must be explicitly given a float:
Did this article leave any questions unanswered? Post any question in our forums for a rapid response.
The LateBinder class is a class which provides efficient, standardized access to properties and fields through reflection. In other words, the LateBinder can be used to get and set values on objects using their string names. The LateBinder is very effective when writing tools and performing high-level actions on objects.
The following code sets the X, Y, and Z properties of a Sprite using the LateBinder:
Add the following using statements:
Assuming mSprite is a valid Sprite:
If you are familiar with .NET then you may already be comfortable using the built-in methods for getting and setting values for reflection. Syntactically, the LateBinder may not appear to provide much in the way of convenience:
The LateBinder is most effective in situations where a particular property must be read or set multiple times. It caches off the getters and setters for properties and uses these delegates to invoke the properties on any subsequent calls, making it very fast. Keep in mind that the LateBinder does quite a bit of work internally when a property is read or set for the very first time. Therefore, if you measure the speed of the LateBinder when first calling GetValue or SetValue, you will notice a significant cost in speed. However subsequent calls usually take about 10% of the amount of time as using the raw reflection calls.
Also, using the LateBinder is also effective for performing reflection in a cross-platform manner. The LateBinder will attempt to cache off delegates if possible, but it will fall back to regular reflection calls on platforms which don't support the faster method (such as the 360).
The LateBinder provides efficient reflection because it dynamically creates instances for given types of objects and also dynamically retrieves their getters and setters for arguments when GetProperty and SetProperty are called. The initial instantiation and retrieval is slow compared to even raw reflection calls. If you are particularly sensitive to performance, you may want to consider calling GetProperty or SetProperty (depending on what you will be using) for each property you plan on setting in your Screen's Initialize (or CustomInitialize if using Glue) method. This will get the slower instantiation out of the way so the LateBinder will be very efficient every time you use it.
The GetValue method allows you to get the value of a variable using reflection by passing in a string. For example, you could get the X value form a Sprite by either hard-coding the value or by using the latebinder:
Did this article leave any questions unanswered? Post any question in our forums for a rapid response.