所以這裏是完整的細節。唯一的例外是
[A]SimpleClassLib.PageHandler cannot be cast to [B]SimpleClassLib.PageHandler. Type A originates from 'SimpleClassLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' at location 'D:...\bin\SimpleClassLib.dll'. Type B originates from 'SimpleClassLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'D:...\bin\Debug\SimpleClassLib.dll'
提到[A] d開發商:在應用程序配置文件之一... \ BIN \ SimpleClassLib.dll並建有[B] d真正的應用程序:... \ bin \ Debug \ SimpleClassLib.dll,所以應用程序的一部分從[A]創建PageHandler實例並填充列表,另一部分嘗試從[B]向PageHandler類型轉換。
以下示例將輕鬆觸發此錯誤。希望這可以幫助某人。 這是簡單的類庫。將其構建爲一個dll。
// SimpleClassLib.dll
namespace SimpleClassLib
{
public class Foo
{
string Prop1 { get { return "I am Foo!!"; } }
}
}
以下是控制檯應用程序。該應用程序鏈接到SimpleClassLib,就像來自VS 2008的普通添加引用一樣。此外,它還從另一個路徑加載實例。
// Separate console application App.exe
// Progoram.cs
using SimpleClassLib;
namespace App
{
class Program
{
List<object> myFooList;
Program()
{
myFooList = new List<object>();
Assembly a = Assembly.LoadFile(@"<differentpath>\SimpleClassLib.dll");
Type aFooType = a.GetType("SimpleClassLib.Foo");
ConstructorInfo aConstructor = aFooType.GetConstructor(new Type[] { });
myFooList.Add(aConstructor.Invoke(new object[]{}));
myFooList.Add(aConstructor.Invoke(new object[] { }));
myFooList.Add(aConstructor.Invoke(new object[] { }));
}
void DumpPeculiar()
{
for (int i = 0; i < myFooList.Count; i++)
{
// If one inspects the list in debugger will see a list of
// Foo but this Foo comes from a different load context so the
// following cast will fail. While if one executes the line
// f = myFooList[i] as Foo
// it will succeed
Foo f = myFooList[i] as Foo;
Foo f1 = (Foo)myFooList[i];
}
}
static void Main(string[] args)
{
Program p = new Program();
p.DumpPeculiar();
}
}
}
感謝您的快速回復。我發現了這個問題。我將添加另一篇文章,以提供完整的細節。 – ferosekhanj 2010-10-06 11:22:47
我已經給出了這個問題的答案。但是我仍然不明白相同類型的演員如何在即時窗口中工作。 – ferosekhanj 2010-10-06 12:42:13
如果您編寫一個特殊的裝配轉儲工具,Assembly.LoadFile()應該僅用於*。使用LoadFrom()。 – 2010-10-06 12:48:27