C# inherit(kind off)/use the properties of multiple classes in a single class

c#

Solution

You can't, as multiple inheritance is only for interfaces in C#.

Hacks

I:

public class C : { public A; public B; }

II:

public interface IA { string P1, P2; }    
public interface IB { string P3, P4; }

public class C : IA, IB { string P1, P2, P3, P4; }

III:

public class A { public string P1, P2; }
public class B : A { public string P3, P4; }

public class C : B {}

As for the DTO, maybe these links 1; 2 could be useful:

If not, you could build your class dynamically using reflection.

Build dynamically a new `C` class containing all the public properties of both `A` and `B`.

Problem

Let's say I have 2 classes like this: ``` public class A { public string P1{ get; set; } public string P2{ get; set; } } public class B { public string P3{ get; set; } public string P4{ get; set; } } ``` and I need a class like this: ``` public class C { public string P1{ get; set; } public string P2{ get; set; } public string P3{ get; set; } public string P4{ get; set; } } ``` it it possible for the class C to use class A and B instead of re-declaring all the properties ? (I need this for DTOs)

Original source