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
{
}