Getting GAC Path Using Reflection

Recently I was working on a SharePoint timer job for some data synchronization across numerous WebParts. In order to do this I had to load through code the pertinent assemblies from the GAC (I know, I know. I should be using the bin for security purposes, but it is a client request to be in this form) so that I could get access to the various types dynamically.

I found an assortment of sundry techniques that people shared, however I didn’t really dig the sum of string literals that they were using to do it. I am finding that using reflection is much more efficient and clean, so I thought I would post the code snippet for others consumption:


private static string GacPath()
PropertyInfo pi = typeof (Environment).GetProperty(“GacPath”, BindingFlags.Static | BindingFlags.NonPublic);
if (pi == null)
return null;
MethodInfo mi = pi.GetGetMethod(true);
return (string) mi.Invoke(null, null);