Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy am I experiencing a name collision
    text
    copied!<p>We have a web service framework we use; which I had no part in creating and have no ability to modify, that requires, essentially, cookie-cutter code to add a "module" to the framework. A sample of the format of the code is below. </p> <p>In the example below, I added the <code>Helper</code> class to each of the classes below several months apart without realizing I had two classes named the same. The code compiles fine, but when deployed to the web servers it produces a run-time error. Whenever either of the Processor_ProcessCompleted event handlers get hits, it throws an invalid cast exception stating that an object of type <code>Helper</code> cannot be cast to type <code>Helper</code>. </p> <p>I understand the error, and have since remedied the situation by re-factoring the <code>Helper</code> class out. These helper classes are never referenced anywhere outside their own class, they are private child classes, they are in different namespaces, and neither of the namespaces, in any way, references the other namespace. My question is: why is this collision occurring?</p> <p>Obviously, I am incorrect, but I thought the mere act of declaring them private and internal (not to be confused with <code>internal</code>) to their parent classes would have insulated them from all possibility of collision.</p> <pre><code> namespace NameSpace1 { public class Client1 { Processor processor; private class Helper { public Property1 { get; set; } public Property2 { get; set; } } public Client1(Processor p) { processor = p; processor.ProcessCompleted += Processor_ProcessCompleted; } void Processor_Process(object sender, ProcessCompletedEventArgs e) { Helper helper = (Helper)e.UserState; // ... do a bunch of stuff } } } namespace NameSpace2 { public class Client2 { Processor processor; private class Helper { public Property1 { get; set; } } public Client2(Processor p) { processor = p; processor.ProcessCompleted += Processor_ProcessCompleted; } void Processor_Process(object sender, ProcessCompletedEventArgs e) { Helper helper = (Helper)e.UserState; // ... do a bunch of stuff } } } </code></pre> <p><strong>Re Jon Skeet:</strong> Jon, thanks for the reply. In this case, all the clients use the same processor. All of them assign their event handler to the event. I understand what you are saying, but since the cast occurs in the context of the specific method, it seems as though it should be able to infer the specific type from the context of the method.</p> <p>As for the assignemnt of the helper, the processor itself never actually looks at what it it stuffing into the EventArgs. It's just an <code>object</code> to the processor. The asynch call looks like:</p> <pre><code> processor.Process(request, helper); </code></pre> <p>and it occurrs inside the same class that is attempting to cast it at the response. The helper just contains some simple data that the service uses to determine path of execution.</p> <p><strong>Edit to address Jon's Edit:</strong> Jon, I now think I see the problem. The designer of the framework specifically designed it to allow all the clients to use the same processor. Thus I think you are perfectly correct about the events being fired for all subscribed clients. In fact, the person using the client is writing code like this:</p> <pre><code> Processor processor = new Processor(); Client1 client1 = new Client1(processor); Client2 client2 = new Client2(processor); client1.ExecuteSomeRequest(); // calls processor.Process(request, helper); client2.ExecuteSomeOtherRequest(); // calls processor.Process(request, helper); </code></pre> <p>I appreciate the diligent and comprehensive answer.</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