最佳方法“转换”共享基础? [英] Best way to 'convert' a shared base?

查看:192
本文介绍了最佳方法“转换”共享基础?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-edit- Jason做一个好点,我可以添加两个类型到A的列表。但我不能使用A由于反射。因此,我想知道一种方法将基本数据从一个导出转换到另一个。我知道在C ++在某些情况下,我可以简单地做 *(A *)this =(A)。我希望在C#中有一些简单的东西。

-edit- Jason makes a good point that i could add both types to a list of A. But i cant use A due to reflections. Thus i want to know a way to convert the base data from one derive to another. I know in C++ in certain cases i could simply do *(A*)this = (A)that. I am hoping there is something just as simple in C#.

我认真地有下面的代码,只是不同的名称和类型。

I seriously do have the code below, just different names and types.

因为我需要添加/创建一个列表,我不能使用A(对象应该是B或C.也是A是抽象的)。
所以我只是做了一个列表< B> ,我正在寻找最简单或最好的方式来做这个代码明智。我可以添加一个ctor到C,需要B,但我必须复制粘贴到D,E,F,如果我选择了他们。所以这不是一个伟大的方式。什么是?

Since I need to add/create a list i cant use A (objects are expected to be B or C. Also A is abstract). So i just made a List<B> and i am looking for the easiest or 'best' way to do this code wise. I could add a ctor to C that takes in B but i would have to copy paste it to D, E, F if i choose to have them. So that isnt a great way. What is?

我希望Convert.ChangeType可以工作,但太过分了,没有工作。您建议哪些人?

I was hoping Convert.ChangeType would work but that was too farfetched and didnt work. What are you suggestion guys?

abstract class A
{
    public int a;
    public SomeClass b;
    public string c;
}

class B : A {}
class C : A {}


List<B> process(int data);
void func()
{
    var b = process(...);
    var cTemp = process(...);
    //now to convert cTemp to C[] or any IEnumerable<C>
}


推荐答案

这个问题的一般方法。只要想起

There is not going to be a general approach to this problem. Just think of

class Animal { }
class Cat : Animal { }
class Dog : Animal { }

认为继承意味着是一个上面说的 Cat 是一个是一个动物动物。但这并不意味着将 Cat 转换或转换为或反之亦然。 1

Thinking of inheritance as meaning "is a" the above says that Cat "is a" Animal and Dog "is a" Animal. But that doesn't mean it makes sense to cast or convert a Cat to a Dog or vice versa1. So in general, no dice.

现在也许有一些具体的你的 B

Now maybe there is something specific about your B and C that could help but you haven't given us enough to detail to know.

但是,你需要这样做的事实可以帮助你,但是你没有给我们足够的细节。这可能是你的设计有缺陷,应该退一步,看看你的问题,看看你是否正确建模。

But the fact that you need to do this says that maybe your design is flawed and should take a step back and look at your problem and see if you are modeling it correctly.

最后,什么问题 List< A> ?然后,您可以将 B s和 C 添加到列表中:

Finally, what's wrong with List<A>? Then you can add both Bs and Cs to the list:

List<A> list = new List<A>();
B b = new B();
C c = new C();
list.Add(b); // okay!
list.Add(c); // okay!

1 :另一种方式,我的儿子可能会提醒我,狗去到天堂和猫不。所以,如果有一个简单的方法来将猫转换成狗,猫会有一个便宜的方式进入天堂。没有骰子。

1: Reasoning another way as my son might remind me, dogs go to heaven and cats don't. So, if there were an easy way to convert cats to dogs, cats would have a cheap way to get into heaven. No dice.

这篇关于最佳方法“转换”共享基础?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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