Someone asked me the following today (paraphrased)
"If I have versions 1.0.0.0 and 2.2.2.2 of TestAsssebly both in the gac, and a program TestApp.exe that needs TestAssembly, can I instantiate classes from each assembly inside the same app domain. Can I use both versions of the Foo class in my program?"
So I sat down to do a simple experiment. Two versions of Test Assembly are created in the GAC and a console program is run. Foo.ToString() returns the full name of the assembly that the Foo instance comes from.
Program:
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using TestAssembly;
namespace TestApp
{
class Program
static void Main(string[] args)
try
Console.WriteLine("Loading Foo...");
Assembly foo1 =
AppDomain.CurrentDomain.Load("TestAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=612615ef384d0cd0, processorArchitecture=MSIL");
Foo f = new TestAssembly.Foo();
Console.WriteLine("Typeof Foo: " + f.ToString());
object f2 = foo1.CreateInstance("TestAssembly.Foo");
Console.WriteLine("Typeof f2: " + f2.ToString());
Foo f3 = (Foo)foo1.CreateInstance("TestAssembly.Foo");
Console.WriteLine("Typeof f3: " + f3.ToString());
}
catch (Exception ex)
Console.WriteLine(ex);
Output:
C:\Projects\StrongNameTest\TestApp\bin\Debug>TestApp.exeLoading Foo...Typeof Foo: TestAssembly.Foo TestAssembly, Version=2.2.2.2, Culture=neutral, PublicKeyToken=612615ef384d0cd0Typeof f2: TestAssembly.Foo TestAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=612615ef384d0cd0System.InvalidCastException: Unable to cast object of type 'TestAssembly.Foo' to type 'TestAssembly.Foo'. at TestApp.Program.Main(String[] args) in c:\Projects\StrongNameTest\TestApp\Program.cs:line 25
The part in bold is entertaining, at least. I have a strong suspician that if I interact with f3 as an Object only, using Reflection to Invoke properties and methods then I could in fact use all the version 1.0.0.0 Foo functionality (which is vast and important) within the same app domain.
I also hit a bit of a snag when testing this application. I was previously unaware that Visual Studio does not let you add references to assemblies added to the GAC via gacutil on the local machine. See: http://msdn2.microsoft.com/en-us/library/wkze6zky(VS.80).aspx
Remember Me
a@href@title, strike
Powered by: newtelligence dasBlog 2.0.7226.0
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2008, Damon Payne
E-mail