viewModel 为 viewModelExtended,强制转换?

发布于 2024-10-24 09:35:57 字数 836 浏览 3 评论 0原文

我对如何执行此操作有疑问。我有两节课; ProductViewModelProductExtendedViewModel

我的场景基本上是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

澜川若宁 2024-10-31 09:35:57

遗憾的是,在 c# 中,您无法从基本类型转换为派生类型(“向下转型”),因为您将尝试生成信息(即 ExtendedName 应该具有什么值?)。

最好的选择是通过组合来做到这一点 - 在 ProductExtendedViewModel 上创建一个采用 ProductViewModel 的构造函数:

public class ProductExtendedViewModel : ProductViewModel
{
    public ProductExtendedViewModel()
    {
    }

    public ProductExtendedViewModel(ProductViewModel viewModel)
    {
        Name = viewModel.Name;
        Product = viewModel.Product;
    }

    public string ExtendedName { get; set; }
}

然后您可以调用该构造函数:

var productModel = new ProductViewModel {Name = product.Name, Product = product};

var derived = new ProductExtendedViewModel(productModel);

请注意,您仍然可以强制转换 Derived 类型回到它的基本类型,因为这涉及信息丢失:

var basetype = (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 a ProductViewModel:

public class ProductExtendedViewModel : ProductViewModel
{
    public ProductExtendedViewModel()
    {
    }

    public ProductExtendedViewModel(ProductViewModel viewModel)
    {
        Name = viewModel.Name;
        Product = viewModel.Product;
    }

    public string ExtendedName { get; set; }
}

Which you could then call:

var productModel = new ProductViewModel {Name = product.Name, Product = product};

var derived = new ProductExtendedViewModel(productModel);

Note that you can still cast the Derived type back to it's base type, as this involves a loss of information:

var basetype = (ProductViewModel)derived;

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.

终陌 2024-10-31 09:35:57

正如 Zhaph 所说,在 C# 中你不能沮丧。他的解决方案的替代方案是使用 AutoMapper 创建继承类的实例。这看起来像这样:

var productExtendedViewModel = Mapper.Map<ProductViewModel, ProductExtendedViewModel>(productViewModel);

请参阅文档了解如何配置映射,但基本映射将像这样创建:

Mapper.CreateMap<ProductViewModel, ProductExtendedViewModel>();

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:

var productExtendedViewModel = Mapper.Map<ProductViewModel, ProductExtendedViewModel>(productViewModel);

See the documentation for how you can configure the mapping, but the basic mapping would be created like this:

Mapper.CreateMap<ProductViewModel, ProductExtendedViewModel>();

AutoMapper is a good tool to handle all mappings between business objects and view models.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文