在派生层次结构中向下投放 [英] Down cast in a derived hierarchy

查看:99
本文介绍了在派生层次结构中向下投放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,我想将不同类型的对象向下转换为派生层次结构中更长的对象,我认为最好的方法只是显示一些简单的代码:

Hi people, I''ve ran into a problem where i wanna downcast a object of a different type to a object longer down in a derived hierarchy, i think the best way is just to show some simple code:

class mammal {}
class dog : mammal{}
class cat : : mammal{}
class program
{
   public main()
   {
      mamal m = new mamal();
      cat c = new cat();
      c = (cat)m; //this give me an invalid cast exception!
      c = (m as cat); //same exception as above
   }
}



就像我想给猫对象一个哺乳动物对象/将哺乳动物中的所有信息放到猫对象中(这是从哺乳动物派生的)一样,我尝试了隐式运算符,但发现在派生类中这样做是无效的.因此,有没有一种方法可以手动完成所有操作?

谢谢-Jackie



as seen i wanna give a cat object a mammal object/have all the info in the mammal putted into the cat object, which is derived from mammal, i tried implicit operators but saw that this is invalid to do in derived classes. so is there a way to do this without doing everything manually?

thanks - Jackie

推荐答案

您不能将对象强制转换为不是.如果它属于不同的名称空间,则它不是同一类.您将必须创建一个转换器:

You can''t cast an object to a type it is not. If it belongs to a different namespace then it is not the same class. You will have to create a converter:

public static Namespace1.SomeClass Convert(Namespace2.SomeClass someClass) {
    Namespace1.SomeClass rtn = new Namespace1.SomeClass();
    rtn.SomeProp = someClass.SomeProp;
    rtn.SomeOtherProp = someClass.SomeOtherProp;
    return rtn;
}




您可以使用反射,也可以使一个反射自另一个反射,并使其具有相同的属性,并进行其他扩展


另外,如果您对其中一个类拥有代码,则可以检查类中显式和隐式的重载.

祝你好运,
OI


or

You can use reflection, or make one derive from another and have one with common properties and other extend that


Also, if you own the code to one of the classes, you can check into overloading explicit and implicit on your class.

Good luck,
OI


我认为向下投射对象没有任何意义,只是假设猫具有一个名为CapacityToDrinkMilk的额外属性,该属性是整数,可以告诉猫可以喝多少牛奶.当您在该物业垂头丧气时,您期望什么?这导致对象具有错误的属性.您可以创建一种将哺乳动物转换为猫的方法,您可以在其中复制调用属性,并为其他属性添加默认值.如下图所示

公共静态T ConvertMammalToSpecific(此哺乳动物哺乳动物对象),其中T:哺乳动物,new()
{
T obj = new T();
//将哺乳动物的所有属性设置为新对象
//在开关盒中设置其他属性/什么都不做取决于您的需求
}

调用此方法将哺乳动物转换为猫,如下所示
哺乳动物m =新的哺乳动物();
cat c = m.ConvertMammalToSpecific< cat>();

谢谢
I don''t think that make any sense downcasting an object, just assume cat has an extra property called CapacityToDrinkMilk which is integer that tells how much milk a cat can drink. What do u expect while downcasting in that property? which leads an object with wrong properties. You can create a method to convert mammal to cat where you can copy call the properties and add default values for the other properties. something like below

public static T ConvertMammalToSpecific<t>(this mammal mammalobject) where T : mammal, new()
{
T obj = new T();
//set all properties of mammal to new object
//set other properties in switch case/ dont do anything depends on your neeed
}

call this method to convert mammal to cat like below
mammal m = new mammal();
cat c = m.ConvertMammalToSpecific<cat>();

thanks


这篇关于在派生层次结构中向下投放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆