在C#愚蠢的继承问题 [英] Stupid inheritance question in C#
问题描述
我敢肯定,这是不可能的,但在这里不用..
I'm pretty sure this is impossible, but here goes..
我在C#中的自定义类叫做Person其中有一些属性如年龄,身高等。
I have a custom class in C# called Person which has a few properties such as Age, Height etc.
然后我做了一个名为雇员的新类,它继承Person,但我还没有添加任何其他属性员工。因此,它基本上只是一个人还在,但其所谓的员工。
I then make a new class called Employee which inherits from Person but I don't yet add any other properties to Employee. So its basically just a Person still, except its called an Employee.
现在说我有一个名为SomePerson人的一个实例。如何创建具有所有从它继承了人的价值观的新Employee实例,设置里面的人SomePerson。就像从个人浇铸成一个雇员。但是没有我不必手动指定需要设置..
Now say I have an instance of Person called SomePerson. How can I create a new Employee instance that has all of the values it inherited from Person, set to those inside SomePerson. Like casting from a Person into an Employee.. But without me having to manually specify each and every property that needs to be set..
喜欢的东西每一个属性。
Something like..
Employee NewEmployee = (Employee)SomePerson;
不过,当然,你得到错误说无法一个人转换成员工等。
But of course you get the error saying "Can't convert a Person into an Employee" etc.
时AutoMapper唯一可行的解决方案,做这样的事情,如果你说有在所涉及的对象??
Is AutoMapper the only practical solution to do things like this if you say had 300 properties in the involved objects??
更新:
自动映射似乎不处理我的对象。
Auto-Mapper doesn't seem to handle my objects..
Employee SomeEmployee = EmployeeRepository.GetEmployee(SomeEmployeeID);
// Populate the ViewModel with the Person fetched from the db, ready for editing..
VMEmployee EmployeeToEdit = Mapper.Map<Employee, VMEmployee>(SomeEmployee);
// ViewModel based on Employee with Validation applied..
[MetadataType(typeof(Employee_Validation))]
public class VMEmployee : Employee
{
// Absolutely nothing here
}
,其中员工,是自动通过LINQ生成到SQL ..
where "Employee" is auto-generated by LINQ to SQL..
推荐答案
AutoMapper是在这种情况下,一个很好的解决方案。如果你不打算使用属性映射框架,你不想创建一个拷贝构造函数公职人员(人人)
,或隐式/显式转换,怎么回事你期望在整个复制的属性。现实你可以
AutoMapper is a good solution in this case. If you're not going to use a property mapping framework, and you're not willing to create a copy constructor public Employee(Person person)
, or an implicit/explicit conversion, how else do you expect to copy the properties across. Realistically you could
1.Reflection
1.Reflection
public void Map<TSource, TDestination>(TSource source, TDestination destination)
{
var props = typeof(TSource).GetProperties(BindingFlags.Public | BindingFlags.Instance);
var type = typeof(TDestination);
foreach (var prop in props)
{
object value = prop.GetValue(source, null);
var prop2 = type.GetProperty(prop.Name);
if (prop2 == null)
continue;
if (prop.PropertyType != prop2.PropertyType)
continue;
prop2.SetValue(destination, value, null);
}
}
2.复印构造
2.Copy Constructor
public Employee(Person person)
{
// Copy properties
}
3.Implicit /显式转换
3.Implicit/Explicit Conversion
public static implicit operator Employee(Person person)
{
// Build instance and return
}
4.AutoMapper
4.AutoMapper
Mapper.Map<Person, Employee>(person);
3/4 5.Combination:
5.Combination of 3/4:
public static implicit operator Employee(Person person)
{
return Mapper.Map<Person, Employee>(person);
}
隐式/显式转换操作符的说明:我相信在使用这些你赢了'T被生成符合CLS的代码。
A note on implicit/explicit conversion operators: I believe in using these you won't be generating CLS-compliant code.
作为@米奇小麦已经表示,如果你有超过300个属性的对象,我会重新考虑是什么对象实际上代表。重构重构重构。
As @Mitch Wheat has already said, if you have an object with over 300 properties, I would reconsider what that object actually represents. Refactor refactor refactor.
这篇关于在C#愚蠢的继承问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!