Reflection

There are 5 entries for the tag Reflection

Finding Bad Controllers

In one mailing list I’m on, someone ran into a problem where they renamed a controller, but ASP.NET MVC could not for the life of it find it. They double checked everything. But ASP.NET MVC simply reported a 404. This is usually where I tell folks to run the following NuGet command: Install-Package RouteDebugger RouteDebugger is a great way to find out why a route isn’t matching a controller. In this particular case, the culprit was that the person renamed the controller and forgot to append the “Controller” suffix. So...

Get All Types in an Assembly

Sometimes, you need to scan all the types in an assembly for a certain reason. For example, ASP.NET MVC does this to look for potential controllers. One naïve implementation is to simply call Assembly.GetTypes() and hope for the best. But there’s a problem with this. As Suzanne Cook points out, If a type can't be loaded for some reason during a call to Module.GetTypes(), ReflectionTypeLoadException will be thrown. Assembly.GetTypes() also throws this because it calls Module.GetTypes(). In other words, if any type can’t be loaded, the entire method call blows up and...

A Sordid Little Tale Of Unexpected Security Exceptions

It was a dark and stormy coding session; the rain fell in torrents as my eyes were locked to two LCD screens in a furious display of coding … …sorry sorry, I just can’t continue. It’s all a lie. This actually a cautionary tale describing one subtle way that you can run afoul Code Access Security (CAS) when attempting to run an application in partial trust. But who wants to read about that? Right? Right? Well this isn’t a sordid tale, but if you bear with me, you may just find it interesting. Either...

Creating Copies of Attributes

I’ve been working on a lovely little prototype recently but ran into a problem where my code receives a collection of attributes and needs to change them in some way and then pass the changed collection along to another method that consumes the collection. I  want to avoid changing the attributes directly, because when you use reflection to retrieve attributes, those attributes may be cached by the framework. So changing an attribute is not a safe operation as you may be changing the attribute for everyone else who tries to retrieve them. What I really wanted...

How To Get The Calling Method And Type

Here are a couple of useful methods for getting information about the caller of a method. The first returns the calling method of the current method. The second returns the type of the caller. Both of these methods require declaring the System.Diagnostics namespace. private static MethodBase GetCallingMethod() { return new StackFrame(2, false).GetMethod(); } private static Type GetCallingType() { return new StackFrame(2, false).GetMethod().DeclaringType; } Pop Quiz! Why didn’t I apply the principle of code re-use and implement the second method like so? public static Type GetCallingType() { return GetCallingMethod().DeclaringType; } A virtual cigar and the admiration of your peers to the first person to answer...