暫時忽略Assembly Two。您將無法實例化類C的實例,因爲它從B繼承而來,B沒有公共構造函數。
因此,如果您從類C中刪除了繼承問題,則由於保護級別的原因,您在創建B類程序集之外的類B的實例時仍然存在問題。不過,您可以使用反射來創建B的實例。
出於測試的目的,我改變了你的類如下:
public class A
{
internal A(string s)
{
PropSetByConstructor = s;
}
public string PropSetByConstructor { get; set; }
}
public class B : A
{
internal B(string s)
: base(s)
{
PropSetByConstructor = "Set by B:" + s;
}
}
然後我寫了一個控制檯應用程序進行測試:
static void Main(string[] args)
{
System.Reflection.ConstructorInfo ci = typeof(B).GetConstructors(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)[0];
B B_Object = (B)ci.Invoke(new object[]{"is reflection evil?"});
Console.WriteLine(B_Object.PropSetByConstructor);
Console.ReadLine();
}
我不建議這樣做,它只是壞實踐。我想在所有的事情上都有例外,這裏的例外可能不得不處理你不能擴展的第三方庫。這會給你一個實例化和調試的方法,如果不是繼承。