1

Topic: Inheritance changeover on and a facade

Whether there is a simple syntax for  it (without writing of the stupid code manually and usages of generators from libraries for testing)? I.e. public class ObservableMyClass {public void MyMethod1 () {...} public void MyProperty1 {get; set;}} public class MyClass {ObservableMyClass myObject; public MyClass (ObservableMyClass myObject) {this.myObject = myObject;} public void MyMethod1 () {myObject. MyMethod1 ();} public void MyProperty1 {get {return myObject. MyProperty1;} set {myObject. MyProperty1 = value;}}} it would not be desirable to write hands for each method and property a wrapper, and a lot of the exterior code to pull. Inheritance is impossible for me, because objects MyClass at me are connected to objects ObservableMyClass relation N to 1, instead of 1:1

2

Re: Inheritance changeover on and a facade

Hello, Ejnstok Fajr, you wrote: whether > There is a simple syntax for  it (without writing of the stupid code manually and usages of generators from libraries for testing)? Simple is not present. From this that can approach: 1.  through T4 2.  through Roslyn API 3. The bicycle on Code DOM or Reflection. Emit (is possible to take any Castle Dynamic Proxy if with zero to write laziness)

3

Re: Inheritance changeover on and a facade

Hello, Ejnstok Fajr, you wrote: whether > There is a simple syntax for  it (without writing of the stupid code manually and usages of generators from libraries for testing)? As colleague RushDevion prompted it is possible to use Mixins from libraries with DynamicProxy. The Same Castle. Approximately the decision looks so. Let we have 2 classes which we want to envelop in one : public class ObservableMyClass1 {public void MyMethod1 () {Console. WriteLine ("1");} public int MyProperty1 {get; set;}} public class ObservableMyClass2 {public void MyMethod2 () {Console. WriteLine ("2");} public int MyProperty2 {get; set;} } First of all we select interfaces for these classes (it can be made in the same studio through refactoring). Since we will need to save communication between the interface and implementations, I made special attribute which labeled  (it is possible to make differently - simply to search through a reflection for classes in which these interfaces are implemented or to set communication in the code... - a heap of variants). public class ImplementationAttribute: Attribute {public ImplementationAttribute (Type type) {Type = type;} public Type Type {get; set;}} [Implementation (typeof (ObservableMyClass1))] public interface IObservableMyClass1 {int MyProperty1 {get; set;} void MyMethod1 ();} public class ObservableMyClass1: IObservableMyClass1 {public void MyMethod1 () {Console. WriteLine ("1");} public int MyProperty1 {get; set;}} [Implementation (typeof (ObservableMyClass2))] public interface IObservableMyClass2 {int MyProperty2 {get; set;} void MyMethod2 ();} public class ObservableMyClass2: IObservableMyClass2 {public void MyMethod2 () {Console. WriteLine ("2");} public int MyProperty2 {get; set;} } To describe  we use multiple inheritance in interfaces public interface IMyClass: IObservableMyClass1, IObservableMyClass2 {} Actually it is necessary to generate a proxy-class which will implement interface IMyClass, but  calls on implementation of interfaces IObservableMyClass1 and IObservableMyClass2 public T GetMixedClass <T> () where T: class {var generator = new ProxyGenerator (); var options = new ProxyGenerationOptions (); var type = typeof (T); var interfaces = type. GetInterfaces (); foreach (var interf in interfaces) {var imp = interf. GetCustomAttributes (typeof (ImplementationAttribute), true).OfType <ImplementationAttribute> ().SingleOrDefault (); if (imp! = null) {options. AddMixinInstance (Activator. CreateInstance (imp. Type));}} return (T) generator. CreateClassProxy (typeof (object), new [] {typeof (T)}, options);} Everything, we can use: var a = GetMixedClass <IMyClass> (); a. MyMethod1 (); a. MyMethod2 ();