Posted by Adam Buenz

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:

C#:
  1. private static string GacPath()
  2. {
  3. PropertyInfo pi = typeof (Environment).GetProperty("GacPath", BindingFlags.Static | BindingFlags.NonPublic);
  4. if (pi == null)
  5. {
  6. return null;
  7. }
  8. MethodInfo mi = pi.GetGetMethod(true);
  9. return (string) mi.Invoke(null, null);
  10. }

Leave a Reply