Stupid question of inheritance in C #

I am sure this is not possible, but here it goes.

I have a custom class in C # called Person, which has several properties like Age, Height, etc.

Then I create a new Employee class that inherits from Person, but I don't add any other Employee properties yet. Thus, his basically just a man is still, except for his called Employee.

Now say that I have an instance of Person called SomePerson. How can I create a new instance of Employee that has all the values ​​that it inherits from Person and set them to SomePerson. Like casting from Man to Worker. But without me, you need to manually specify each property that needs to be set.

Something like..

Employee NewEmployee = (Employee)SomePerson; 

But, of course, you get the error message "Unable to convert Person to Employee", etc.

Is AutoMapper the only practical solution for such actions if you say that the object had 300 properties?

UPDATE:

Auto-Mapper doesn't seem to process 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 } 

where "Employee" is automatically generated LINQ to SQL ..

+4
inheritance c #
source share
6 answers

AutoMapper is a good solution in this case. If you are not going to use the property mapping structure and do not want to create a copy constructor public Employee(Person person) or an implicit / explicit conversion, how else do you expect to copy properties over all. Really you could

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); } } 

Constructor 2.Copy

 public Employee(Person person) { // Copy properties } 

3. Explicit / Explicit Conversion

 public static implicit operator Employee(Person person) { // Build instance and return } 

4.AutoMapper

 Mapper.Map<Person, Employee>(person); 

5. Combination 3/4:

 public static implicit operator Employee(Person person) { return Mapper.Map<Person, Employee>(person); } 

Note on implicit / explicit conversion operators: I believe in their use, you will not generate CLS-compatible code.

As @Mitch Wheat already said, if you have an object with over 300 properties, I would rethink what this object actually represents. Refactor refactoring refractor.

+4
source share

You can use automaper for this purpose without any configuration.

here is an example:

 Employee employee = Mapper.Map<Person, Employee>(person); 
+1
source share

Create a constructor on Employee that accepts an instance of Person . Of course, you will need to fill in all the properties (maybe Resharper can help?)

0
source share

If you have relatively simple properties, reflection is likely to be the fastest and easiest solution to display property values.

Contrary to popular belief, reflection is really not so bad; Rick Stryle dispels this myth in this article: http://www.west-wind.com/weblog/posts/351.aspx .

Create the following constructor for Employee :

 public Employee(Person person) { // clone property values foreach (var property in person.GetType().GetProperties().Where(property => property.CanRead && property.CanWrite)) { property.SetValue(this, property.GetValue(user, null), null); } } 

Now just create an instance of the Employee object as follows:

 Employee NewEmployee = new Employee(SomePerson); 
0
source share

"they are complex LINQ to SQL Model classes" - Aaron

If you have a table that maps to one of several possible classes, you can use LINQ to SQL Inheritance Hierarchies . This means that if you use the same table for several types of conceptual objects, you can LINQ to SQL automatically create the appropriate class. Each class can have different properties, which can be a subset of the columns in the table.

There are some limitations. You can only use one table for each inheritance hierarchy, and you should have a discriminator column that tells LINQ to SQL which class to use.

0
source share

When you start a new job, a new person is born who is not you, but the old you killed?

If your design is based on this scenario, then it seems that the practice in your organization is very strange.

Instead, the person has an employment relationship with the employer.

Do not use inheritance for model roles; allow the same person to have roles that represent their relationship with other objects.

-one
source share

Source: https://habr.com/ru/post/651086/


All Articles