真正的问题出现在反射和程序集补丁/钩子上。我将举一个简单的例子,以便不太难以理解主要问题。
那么,让我们想象一下我有这些基本类:
那么,让我们想象一下我有这些基本类:
public class Vehicle
{
public string Name;
public string Price;
public void DoSomething()
{
Main.Test(this);
}
}
public class Car : Vehicle
{
public int Wheels;
public int Doors;
}
在主代码上,我运行了以下命令:
public class Main
{
public void Start()
{
Car testCar = new Car()
{
Name = "Test Car",
Price = "4000",
Wheels = 4,
Doors = 4
};
testCar.DoSomething();
}
public static void Test(Vehicle test)
{
// Is this allowed ?
Car helloWorld = (Car) test;
}
}
好的,问题是:
这种类型转换(在静态方法Test中)被允许吗?我会失去Car属性但保留Vehicle属性吗?
如果不正确,是否有其他方法可以实现?
谢谢。