Note that there are some explanatory texts on larger screens.

plurals
  1. POLooking for non-type-specific method of handling Generic Collections in c#
    text
    copied!<p>My situation is this. I need to run some validation and massage type code on multiple different types of objects, but for cleanliness (and code reuse), I'd like to make all the calls to this validation look basically the same regardless of object. I am attempting to solve this through overloading, which works fine until I get to Generic Collection objects.</p> <p>The following example should clarify what I'm talking about here:</p> <pre><code>private string DoStuff(string tmp) { ... } private ObjectA DoStuff(ObjectA tmp) { ... } private ObjectB DoStuff(ObjectB tmp) { ... } ... private Collection&lt;ObjectA&gt; DoStuff(Collection&lt;ObjectA&gt; tmp) { foreach (ObjectA obj in tmp) if (DoStuff(obj) == null) tmp.Remove(obj); if (tmp.Count == 0) return null; return tmp; } private Collection&lt;Object&gt; DoStuff(Collection&lt;ObjectB&gt; tmp) { foreach (ObjectB obj in tmp) if (DoStuff(obj) == null) tmp.Remove(obj); if (tmp.Count == 0) return null; return tmp; } ... </code></pre> <p>This seems like a real waste, as I have to duplicate the exact same code for every different <code>Collection&lt;T&gt;</code> type. I would like to make a single instance of <code>DoStuff</code> that handles any <code>Collection&lt;T&gt;</code>, rather than make a separate one for each.</p> <p>I have tried using <code>ICollection</code>, but this has two problems: first, <code>ICollection</code> does not expose the <code>.Remove</code> method, and I can't write the <code>foreach</code> loop because I don't know the type of the objects in the list. Using something more generic, like <code>object</code>, does not work because I don't have a method <code>DoStuff</code> that accepts an <code>object</code> - I need it to call the appropriate one for the actual object. Writing a <code>DoStuff</code> method which takes an <code>object</code> and does some kind of huge list of if statements to pick the right method and cast appropriately kind of defeats the whole idea of getting rid of redundant code - I might as well just copy and paste all those <code>Collection&lt;T&gt;</code> methods.</p> <p>I have tried using a generic <code>DoStuff&lt;T&gt;</code> method, but this has the same problem in the <code>foreach</code> loop. Because I don't know the object type at design time, the compiler won't let me call <code>DoStuff(obj)</code>.</p> <p>Technically, the compiler should be able to tell which call needs to be made at compile time, since these are all private methods, and the specific types of the objects being passed in the calls are all known at the point the method is being called. That knowledge just doesn't seem to bubble up to the later methods being called by this method.</p> <p>I really don't want to use reflection here, as that makes the code even more complicated than just copying and pasting all the <code>Collection&lt;T&gt;</code> methods, and it creates a performance slowdown. Any ideas?</p> <p>---EDIT 1--- I realized that my generic method references were not displaying correctly, because I had not used the html codes for the angle brackets. This should be fixed now.</p> <p>---EDIT 2--- Based on a response below, I have altered my <code>Collection&lt;T&gt;</code> method to look like this:</p> <pre><code>private Collection&lt;T&gt; DoStuff&lt;T&gt;(Collection&lt;T&gt; tmp) { for (int i = tmp.Count - 1; i &gt;= 0; i--) if (DoStuff(tmp[i]) == null) tmp.RemoveAt(i); if (tmp.Count == 0) return null; return tmp; } </code></pre> <p>This still does not work, however, as the compiler cannot figure out which overloaded method to call when I call <code>DoStuff(tmp[i])</code>.</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