<p>I'm working on an editor for files that are used by an important internal testing tool we use.  The tool itself is large, complicated, and refactoring or rewriting would take more resources than we are able to devote to it for the forseeable future, so my hands are tied when it comes to large modifications.  I must use a .NET language.</p>

<p>The files are XML serialized versions of four classes that are used by the tool (let's call them A, B, C, and D).  The classes form a tree structure when all is well.  Our editor works by loading a set of files, deserializing them, working out the relationships between them, and keeping track of any bad states it can find.  The idea is for us to move away from hand-editing these files, which introduces tons of errors.</p>

<p>For a particular type of error, I'd like to maintain a collection of all files that have the problem.  All four classes can have the problem, and I'd like to reduce duplication of code as much as possible.  An important requirement is the user needs to be able to get the items in sets; for example, they need to get all A objects with an error, and telling them to iterate over the whole collection and pick out what they want is unacceptable compared to a <code>GetAs()</code> method.  So, my first thought was to make a generic item that related the deserialized object and some metadata to indicate the error:</p>

<pre><code>public class ErrorItem&lt;T&gt;
{
    public T Item { get; set; }
    public Metadata Metadata { get; set; }
}
</code></pre>

<p>Then, I'd have a collection class that could hold all of the error items, with helper methods to extract the items of a specific class when the user needs them.  This is where the trouble starts.</p>

<p>None of the classes inherit from a common ancestor (other than <code>Object</code>).  This was probably a mistake of the initial design, but I've spent a few days thinking about it and the classes really don't have much in common other than a GUID property that uniquely identifies each item so I can see why the original designer did not relate them through inheritance.  This means that the unified error collection would need to store <code>ErrorItem&lt;Object&gt;</code> objects, since I don't have a base class or interface to restrict what comes in.  However, this makes the idea of this unified collection a little sketchy to me:</p>

<pre><code>Public Class ErrorCollection
{
    public ErrorItem&lt;Object&gt; AllItems { get; set; }
}
</code></pre>

<p>However, this has consequences on the public interface.  What I really want is to return the appropriate <code>ErrorItem</code> generic type like this:</p>

<pre><code>public ErrorItem&lt;A&gt;[] GetA()
</code></pre>

<p>This is impossible because I can only store <code>ErrorItem&lt;Object&gt;</code>!  I've gone over some workarounds in my head; mostly they include creating a new <code>ErrorItem</code> of the appropriate type on-the-fly, but it just feels kind of ugly.  Another thought has been using a <code>Dictionary</code> to keep items organized by type, but it still doesn't seem right.</p>

<p>Is there some kind of pattern that might help me here?  I know the easiest way to solve this is to add a base class that A, B, C, and D derive from, but I'm trying to have as small an impact on the original tool as possible.  Is the cost of any workaround great enough that I should push to change the initial tool?</p>

[{'answer_id': 54258, 'author': 'Mendelt', 'author_id': 3320, 'author_profile': 'https://Stackoverflow.com/users/3320', 'pm_score': 1, 'selected': True, 'text': "<p>If A, B, C and D have nothing in common then adding a base class won't really get you anything. It will just be an empty class and in effect will be the same as object.</p>\n\n<p>I'd just create an ErrorItem class without the generics, make Item an object and do some casting when you want to use the objects referenced. If you want to use any of the properties or methods of the A, B, C or D class other than the Guid you would have had to cast them anyway.</p>\n"}, {'answer_id': 54287, 'author': 'fryguybob', 'author_id': 4592, 'author_profile': 'https://Stackoverflow.com/users/4592', 'pm_score': 1, 'selected': False, 'text': '<p>Is this what you are looking for?</p>\n\n<pre><code>private List&lt;ErrorItem&lt;object&gt;&gt; _allObjects = new List&lt;ErrorItem&lt;object&gt;&gt;();\n\npublic IEnumerable&lt;ErrorItem&lt;A&gt;&gt; ItemsOfA\n{\n    get\n    {\n        foreach (ErrorItem&lt;object&gt; obj in _allObjects)\n        {\n            if (obj.Item is A)\n                yield return new ErrorItem&lt;A&gt;((A)obj.Item, obj.MetaData);\n        }\n    }\n}\n</code></pre>\n\n<p>If you want to cache the <code>ItemsOfA</code> you can easily do that:</p>\n\n<pre><code>private List&lt;ErrorItem&lt;A&gt;&gt; _itemsOfA = null;\n\npublic IEnumerable&lt;ErrorItem&lt;A&gt;&gt; ItemsOfACached\n{\n    if (_itemsOfA == null)\n        _itemsOfA = new List&lt;ErrorItem&lt;A&gt;&gt;(ItemsOfA);\n    return _itemsOfA;\n}\n</code></pre>\n'}, {'answer_id': 54649, 'author': 'OwenP', 'author_id': 2547, 'author_profile': 'https://Stackoverflow.com/users/2547', 'pm_score': 1, 'selected': False, 'text': "<p>The answer I'm going with so far is a combination of the answers from fryguybob and Mendelt Siebenga.</p>\n\n<p>Adding a base class would just pollute the namespace and introduce a similar problem, as Mendelt Siebenga pointed out.  I would get more control over what items can go into the collection, but I'd still need to store <code>ErrorItem&lt;BaseClass&gt;</code> and still do some casting, so I'd have a slightly different problem with the same root cause. This is why I selected the post as the answer: it points out that I'm going to have to do some casts no matter what, and KISS would dictate that the extra base class and generics are too much.</p>\n\n<p>I like fryguybob's answer not for the solution itself but for reminding me about <code>yield return</code>, which will make a non-cached version easier to write (I was going to use LINQ).  I think a cached version is a little bit more wise, though the expected performance parameters won't make the non-cached version noticably slower.</p>\n"}]
['https://Stackoverflow.com/questions/54219', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2547/']