Assembly.GlobalAssemblyCache returning FALSE although assembly is loaded from GAC
I have created a test project to understand reflection. I'm loading an assembly from .NET 4.0 GAC. (As I understand, .NET 4.0 maintains it's GAC in C:\WINDOWS\Microsoft.NET\assembly)
I wrote the code like this:
Assembly testAssembly = Assembly.ReflectionOnlyLoadFrom(@"C:\WINDOWS\Microsoft.NET\assembly\GAC_32\TestReflection\v4.0_1.0.0.0__7ff2353191526e8c\TestReflection.dll");
开发者_StackOverflow if(testAssembly.GlobalAssemblyCache)
Console.WriteLine(testAssembly.FullName);
When I run this code, GlobalAssemblyCache
property always return FALSE although I'm loading assembly from GAC.
Can someone tell me the reason? Or Am I missing something?
You may first want to make sure your assembly is in fact loaded onto the GAC. For doing that you can try the following:
gacutil /l System.Data
Which would provide with the details of the assembly in the GAC as:
Microsoft (R) .NET Global Assembly Cache Utility. Version 4.0.30319.1
Copyright (c) Microsoft Corporation. All rights reserved.
The Global Assembly Cache contains the following assemblies:
System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=x86
System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=x86
Number of items = 3
Then for loading the assembly from GAC for ReflectionOnly usage you can try the ReflectionOnlyLoad
instead of ReflectionOnlyLoadFrom
example:
Assembly testAssembly = Assembly.ReflectionOnlyLoad(@"System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=x86");
if (testAssembly.GlobalAssemblyCache) {
Console.WriteLine(testAssembly.FullName);
Console.WriteLine(testAssembly.Location);
} else {
Console.WriteLine("Not found in GAC");
}
The above gives the following output:
System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll
Hope that helps!
精彩评论