Someone asked me the other day if you could reflect on other people's assemblies using the CLR. My answer was, "ABSOLUTELY!!!!!"
As it turns out, they were having some problems achieving this, and they were wondering if there was some kind of security mechanism in place that was preventing reflection on 3rd party assemblies. Here's the basic scenario. They had a 3rd party library with a type that defined several "constants" using fields. They needed to be able to specify a named constant via a string and return the value for the constant. Security and performance arguments aside (they had already been considered), they simply wanted to lookup the field by name via reflection and get its value. This can be accomplished via just a few fairly reasonable lines of code using one of the GetField family of methods on System.Type and then getting the value. I'll leave this as an exercise for the reader so they can have the fun of wresting with the silly BindingFlags enum.
After some discussion, I learned that the trouble actually revolved around getting the Type object in the first place. They were using Type.GetType to load the type with a namespace-qualified name as the string argument. It was returning null (Nothing in VB). They were validating that the string was correct using Intellisense, and concluding that since Intellisense could see the type, that Type.GetType should also "see" it (which seems like a perfectly reasonable assumption).
Type.GetType() takes a string argument specifying the type to retrieve. When specifying types in strings, you can usually use a namespace qualified name ("[namespace.]type"), or an assembly-qualified name ("[namespace.]type, assemblyName"). If you don't provide the assembly name, the API looks through the assemblies already loaded in the AppDomain for a type that matches the name. If an assembly name is specified, a bind occurs to the assembly and it is loaded if necessary.
In this case, there was a reference to the assembly containing the type, so Intellisense was picking it up and providing completion. The trouble was that, at runtime, the assembly had not yet been loaded into the AppDomain, so the type was unavailable.
So, the options were (in order of appropriateness in my opinion):
The real issue here is the number of samples (especially in VB) provided by MS that use Type.GetType() with a non-qualified name (ex. "System.String").
Remember Me
Page rendered at Monday, September 08, 2008 7:48:29 AM (Pacific Standard Time, UTC-08:00)
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.