将基类转换为派生类[重复]

这个问题在这里已有答案:

在C#中是否可以将基类对象显式转换为其派生类之一?目前我认为我必须为我的派生类创建一个构造函数,它接受一个基类对象作为参数并复制属性值 . 我真的不喜欢这个想法,所以我想尽可能避免它 .

这似乎不应该工作(对象被实例化为新的基础,因此不应该为派生类的额外成员分配内存)但是C#似乎允许我这样做:

class BaseClass
{
  ... some stuff ...
}

class DerivedClass : BaseClass
{
    public bool MyDerivedProperty{ get; set; }
}


static void Main(string[] args)
{
    BaseClass myBaseObject = new BaseClass();
    DerivedClass myDerivedObject = myBaseObject as DerivedClass;

    myDerivedObject.MyDerivedProperty = true;
}

回答(6)

2 years ago

不,没有像你说的那样转换类的内置方法 . 最简单的方法是执行您的建议:创建一个 DerivedClass(BaseClass) 构造函数 . 其他选项基本上可以自动地将属性从基础复制到派生实例,例如,使用反射 .

您使用 as 发布的代码将编译,如我所见,但在运行时会抛出空引用异常,因为 myBaseObject as DerivedClass 将评估为 null ,因为它不是 DerivedClass 的实例 .

2 years ago

那是不可能的 . 但你可以使用像AutoMapper这样的Object Mapper

例:

class A
{
    public int IntProp { get; set; }
}
class B
{
    public int IntProp { get; set; }
    public string StrProp { get; set; }
}

在global.asax或应用程序启动中:

AutoMapper.Mapper.CreateMap<A, B>();

用法:

var b = AutoMapper.Mapper.Map<B>(a);

它可以通过流畅的API轻松配置 .

2 years ago

我找到了一个解决方案,并不是说它是最好的解决方案,但它对我来说很干净,并且不需要对我的代码进行任何重大更改 . 我的代码看起来与你的相似,直到我意识到它不起作用 .

My Base Class

public class MyBaseClass
{
   public string BaseProperty1 { get; set; }
   public string BaseProperty2 { get; set; }
   public string BaseProperty3 { get; set; }
   public string BaseProperty4 { get; set; }
   public string BaseProperty5 { get; set; }
}

My Derived Class

public class MyDerivedClass : MyBaseClass
{
   public string DerivedProperty1 { get; set; }
   public string DerivedProperty2 { get; set; }
   public string DerivedProperty3 { get; set; }
}

Previous method to get a populated base class

public MyBaseClass GetPopulatedBaseClass()
{
   var myBaseClass = new MyBaseClass();

   myBaseClass.BaseProperty1 = "Something"
   myBaseClass.BaseProperty2 = "Something else"
   myBaseClass.BaseProperty3 = "Something more"
   //etc...

   return myBaseClass;
}

Before I was trying this, which gave me a unable to cast error

public MyDerivedClass GetPopulatedDerivedClass()
{
   var newDerivedClass = (MyDerivedClass)GetPopulatedBaseClass();

   newDerivedClass.UniqueProperty1 = "Some One";
   newDerivedClass.UniqueProperty2 = "Some Thing";
   newDerivedClass.UniqueProperty3 = "Some Thing Else";

   return newDerivedClass;
}

我改变了我的代码,如下所示,它似乎工作,现在更有意义:

Old

public MyBaseClass GetPopulatedBaseClass()
{
   var myBaseClass = new MyBaseClass();

   myBaseClass.BaseProperty1 = "Something"
   myBaseClass.BaseProperty2 = "Something else"
   myBaseClass.BaseProperty3 = "Something more"
   //etc...

   return myBaseClass;
}

New

public void FillBaseClass(MyBaseClass myBaseClass)
{
   myBaseClass.BaseProperty1 = "Something"
   myBaseClass.BaseProperty2 = "Something else"
   myBaseClass.BaseProperty3 = "Something more"
   //etc...
}

Old

public MyDerivedClass GetPopulatedDerivedClass()
{
   var newDerivedClass = (MyDerivedClass)GetPopulatedBaseClass();

   newDerivedClass.UniqueProperty1 = "Some One";
   newDerivedClass.UniqueProperty2 = "Some Thing";
   newDerivedClass.UniqueProperty3 = "Some Thing Else";

   return newDerivedClass;
}

New

public MyDerivedClass GetPopulatedDerivedClass()
{
   var newDerivedClass = new MyDerivedClass();

   FillBaseClass(newDerivedClass);

   newDerivedClass.UniqueProperty1 = "Some One";
   newDerivedClass.UniqueProperty2 = "Some Thing";
   newDerivedClass.UniqueProperty3 = "Some Thing Else";

   return newDerivedClass;
}

2 years ago

您可以自己实现转换,但我不建议这样做 . 如果要执行此操作,请查看Decorator Pattern以扩展现有对象的功能 .

2 years ago

不,没有内置的转换 . 您需要创建一个构造函数,就像您提到的那样,或者其他一些转换方法 .

此外,由于BaseClass不是DerivedClass,myDerivedObject将为null,并且上面的最后一行将抛出空引用异常 .

2 years ago

不,这是不可能的 . 唯一可行的方法是

static void Main(string[] args)
{
    BaseClass myBaseObject = new DerivedClass();
    DerivedClass myDerivedObject = myBaseObject as DerivedClass;

    myDerivedObject.MyDerivedProperty = true;
}