Posts

Showing posts from August, 2018
Sample code for promoting the method with switch case into Base class or interface and then using the same Client code to invoke correct behavior based on the instance it is dealing with.

public interface IA { int a { get; set; } } public class B : IA { public int a { get; set; } public int b { get; set; } } public class C : IA { public int a { get; set; } public int c { get; set; } } // snip IA myBObject = new B(); PerformAction(myBObject); IA myCObject = new C(); PerformAction(myCObject); // snip void PerformAction(object myObject) { Type objectType = myObject.GetType(); // Here is where I get typeof(IA) if ( objectType.IsInterface ) { // I want to determine the actual Concrete Type, i.e. either B or C // objectType = DetermineConcreteType(objectType); } // snip - other actions on objectType }public interface IA { int a { get; set; } void PerformAction(); } public class B: IA { public int a { get; set; } …