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);
- }
Articles & Research
SharePoint Security
SharePoint Development
SharePoint Architecture
Claims Authentication
Forefront For SharePoint
AIS / Dynamics GP
Team Foundation Server
Pex And Moles
ISA/TMG/IAG/UAG
DPM
Cardspace
Research Methodology
Rural ICT Development
Numerical Analysis
Multi-Level Research
Knowledge Management
Personal/Off-Topic