Please consider two limitations -
- Cannot move MyProperty to an interface or abstract class.
- FooEventHandler is a dotnet infrastructure method, so it cannot change the parameter type.
I have MyProperty defined in several classes.
class A { public string MyProperty { get; set; } } class B { public string MyProperty { get; set; } } class C { public string MyProperty { get; set; } }
The FooEventHandler method updates this property for all parameters that it receives.
public object FooEventHandler(object obj) { object toReturn = null; if (obj.GetType() == typeof(A)) { (obj as A).MyProperty = "updated"; toReturn = obj; } else if (obj.GetType() == typeof(B)) { (obj as B).MyProperty = "updated"; toReturn = obj; } else if (obj.GetType() == typeof(C)) { (obj as C).MyProperty = "updated"; toReturn = obj; } return toReturn; }
And the FooEventHandler is called multiple times -
static void Main(string[] args) { Program program = new Program(); A objA = new A(); program.FooEventHandler(objA); B objB = new B(); program.FooEventHandler(objB); C objC = new C(); program.FooEventHandler(objC); }
Please suggest a way to remove redundant code in Foo, considering, first of all, two limitations.
To be more precise, I ran into this problem when using ParameterInspector in WCF. I am trying to change the properties of all requests intercepted here, and should have written a Switch Case based on operationName.
Classes A, B, C, D , as mentioned above, are proxies. Therefore, I do not want to change them in the first place. Since the Service Reference update will overwrite my changes in iterface.
public object BeforeCall(string operationName, object[] inputs){
Thank you for your help.
source share