viewModel 为 viewModelExtended,强制转换?
我对如何执行此操作有疑问。我有两节课; ProductViewModel
和 ProductExtendedViewModel
。
我的场景基本上是 ProductExtendedViewModel
继承 ProductViewModel
并且我想将 ProductViewModel
转换为 ProductExtendedViewModel
。我该怎么做呢?
这是我到目前为止所尝试过但没有成功的:
viewModelExtended = (ViewModelExtended) viewModel;
我得到的提示是,在转换数字时,值必须是小于无穷大的数字,
我不太擅长继承和转换,所以这对我来说都是新的,所以请理解有点新手问题。
谢谢
编辑
public class ProductViewModel
{
public string Name { get; set; }
public Product Product { get; set; }
}
public class ProductExtendedViewModel : ProductViewModel
{
public string ExtendedName { get; set; }
}
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
}
I have problems with how to do this. I have two classes; ProductViewModel
and ProductExtendedViewModel
.
My scenario is basically that the ProductExtendedViewModel
inherits the ProductViewModel
and I want to cast ProductViewModel
to ProductExtendedViewModel
. How should I go about doing that?
This is what I have tried so far without success:
viewModelExtended = (ViewModelExtended) viewModel;
I get the tip that when casting a number the value must be a number less then infinity
I'm not that great at inherits and casting so this is all kinda new to me so please understand the kinda newbee question.
thanks
EDIT
public class ProductViewModel
{
public string Name { get; set; }
public Product Product { get; set; }
}
public class ProductExtendedViewModel : ProductViewModel
{
public string ExtendedName { get; set; }
}
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
遗憾的是,在 c# 中,您无法从基本类型转换为派生类型(“向下转型”),因为您将尝试生成信息(即 ExtendedName 应该具有什么值?)。
最好的选择是通过组合来做到这一点 - 在
ProductExtendedViewModel
上创建一个采用ProductViewModel
的构造函数:然后您可以调用该构造函数:
请注意,您仍然可以强制转换 Derived 类型回到它的基本类型,因为这涉及信息丢失:
我相信“当从数字 [...] 进行转换时”的“故障排除提示”在这种情况下是一个转移注意力的内容,只是对转换的帮助一般来说。
Sadly, in c# you can't cast from a Base Type to a Derived Type ("downcasting"), because you would be trying to generate information (i.e. what value should ExtendedName have?).
Your best bet would be to do this through composition - create a constructor on
ProductExtendedViewModel
that takes aProductViewModel
:Which you could then call:
Note that you can still cast the Derived type back to it's base type, as this involves a loss of information:
I believe that the "Troubleshooting tip" of "When casting from a number [...]" is a red herring in this case, and is just a help around casting in general.
正如 Zhaph 所说,在 C# 中你不能沮丧。他的解决方案的替代方案是使用 AutoMapper 创建继承类的实例。这看起来像这样:
请参阅文档了解如何配置映射,但基本映射将像这样创建:
AutoMapper 是处理业务对象和视图模型之间所有映射的好工具。
As Zhaph says you can't downcast in C#. An alternative to his solution would be to use AutoMapper to create the instance of the inheriting class. This would look like this:
See the documentation for how you can configure the mapping, but the basic mapping would be created like this:
AutoMapper is a good tool to handle all mappings between business objects and view models.