7
我想使用類型信息從一個數組到另一個通過繼承相關的顯式類型轉換。我的問題是,使用Type信息時,編譯器會拋出錯誤,但我的要求是根據所提供的Type信息進行動態投射。基於類型信息的動態鑄造
請幫助
class Program
{
static void Main(string[] args)
{
Parent[] objParent;
Child[] objChild = new Child[] { new Child(), new Child() };
Type TypParent = typeof(Parent);
//Works when i mention the class name
objParent = (Parent[])objChild;
//Doesn't work if I mention Type info
objParent = (TypParent[])objChild;
}
}
class Parent
{
}
class Child : Parent
{
}