C# viewModel作为viewModelExtended,是否强制转换?
我不知道怎么做。我有两个班C# viewModel作为viewModelExtended,是否强制转换?,c#,asp.net-mvc,asp.net-mvc-2,C#,Asp.net Mvc,Asp.net Mvc 2,我不知道怎么做。我有两个班ProductViewModel和ProductExtendedViewModel 我的场景基本上是ProductExtendedViewModel继承ProductViewModel,我想将ProductViewModel转换为ProductExtendedViewModel。我该怎么做呢 这就是我迄今为止所做的尝试,但没有成功: viewModelExtended = (ViewModelExtended) viewModel; 我得到的提示是,当铸造一个数字时,
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; }
}
不幸的是,在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);
请注意,您仍然可以将派生类型强制转换回其基类型,因为这会丢失信息:
var basetype = (ProductViewModel)derived;
我认为“从数字[…]进行转换时”的“故障排除提示”在这种情况下是一个危险的提示,并且只是对一般转换的帮助。不幸的是,在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);
请注意,您仍然可以将派生类型强制转换回其基类型,因为这会丢失信息:
var basetype = (ProductViewModel)derived;
我相信“从数字[…]施放时”的“故障排除提示”在这种情况下是一个骗人的把戏,只是对一般施放的帮助。正如Zhaph所说,在C#中你不能沮丧。他的解决方案的另一种选择是使用创建继承类的实例。这看起来像这样:
var productExtendedViewModel = Mapper.Map<ProductViewModel, ProductExtendedViewModel>(productViewModel);
Mapper.CreateMap<ProductViewModel, ProductExtendedViewModel>();
var productExtendedViewModel=Mapper.Map(productViewModel);
有关如何配置映射的信息,请参阅文档,但基本映射的创建方式如下:
var productExtendedViewModel = Mapper.Map<ProductViewModel, ProductExtendedViewModel>(productViewModel);
Mapper.CreateMap<ProductViewModel, ProductExtendedViewModel>();
Mapper.CreateMap();
AutoMapper是处理业务对象和视图模型之间所有映射的好工具。正如Zhaph所说,在C#中不能向下转换。他的解决方案的另一种选择是使用创建继承类的实例。这看起来像这样:
var productExtendedViewModel = Mapper.Map<ProductViewModel, ProductExtendedViewModel>(productViewModel);
Mapper.CreateMap<ProductViewModel, ProductExtendedViewModel>();
var productExtendedViewModel=Mapper.Map(productViewModel);
有关如何配置映射的信息,请参阅文档,但基本映射的创建方式如下:
var productExtendedViewModel = Mapper.Map<ProductViewModel, ProductExtendedViewModel>(productViewModel);
Mapper.CreateMap<ProductViewModel, ProductExtendedViewModel>();
Mapper.CreateMap();
AutoMapper是处理业务对象和视图模型之间所有映射的好工具。是否可以发布ViewModel和ViewModel extended的详细信息,特别是它们的定义(即具有“class ViewModelExtended…”的行)及其属性?谢谢。可以发布ViewModel和ViewModel extended的详细信息吗?特别是它们的定义(即具有“类ViewModelExtended…”的行)及其属性?谢谢。看起来不错,我试试看。很好,我试试看