Note that there are some explanatory texts on larger screens.

plurals
  1. PORemove redundant C# code without using interface
    text
    copied!<p>Please consider two restrictions -</p> <ol> <li>Can't move MyProperty to interface or abstract class.</li> <li>FooEventHandler is a dotnet framework method hence can't change paramter type.</li> </ol> <p>I have MyProperty defined in several classes.</p> <pre><code>class A { public string MyProperty { get; set; } } class B { public string MyProperty { get; set; } } class C { public string MyProperty { get; set; } } </code></pre> <p>Method FooEventHandler updates this property for all parameters it receives.</p> <pre><code>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; } </code></pre> <p>And FooEventHandler is called repeatedly like this -</p> <pre><code>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); } </code></pre> <p>Please suggest a way to remove redundant code in Foo, considering above two restrictions in general.</p> <p>To be more precise, I have encountered this issue while using ParameterInspector in WCF. I am trying to modify propery of all the requests intercepted here and had to write Switch Case based on operationName.</p> <p><strong>A, B, C, D</strong> classes as said above are proxies. So don't want to modify them at first place. Since updating service Reference will overwrite my iterface changes.</p> <pre><code>public object BeforeCall(string operationName, object[] inputs){ // update inputs[0] properties } </code></pre> <p>Thank you for your help.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload