title: Generic interfaces for semi-ad hoc report
body: <p>I'm trying to create a simple reporting tool, where a user can select from a set of KPI's, charts, aggregate functions and other parameters, click a button, after which a wcf service is called, which then returns a custom model with all data. This could then be displayed in an MVC/WPF application (could be both). </p>
  
  <p>Since the users could be from several countries, I want to use data annotations to portrait all the numbers and headers in a fashion that is suited to the language and number formats the current user is used to.</p>
  
  <p>The loading of the data and all that stuff is working just fine, no problem whatsoever. Also, I use data annotations, so all the language/culture-specific settings are taken care for. The issues start when I try to put all data into the model that I want to display to the user. </p>
  
  <p>What I'm trying to do is have a Report class, that contains a collection of columns. Each column could be a list of int/double/... values. Now, since I'm dealing with WCF and the above explanation implies (as far as I understand) the use of generics, I assume I can use the [KnownType] or [ServiceKnownType] for the classes/wcf operations, while actually using a base type or interface as the return value. Never really tried this, but I found some good explanations that seem pretty logical to me, so I assume I won't have any big issues for this part (at least I hope not).</p>
  
  <p>Right now, my interfaces are as such (simplified to focus on the actual problem I have):</p>
  
  <pre><code>public interface IReport&lt;T&gt; where T: IConvertible { ICollection&lt;IColumn&lt;T&gt;&gt; Columns { get; set; } }
  public interface IColumn&lt;T&gt; where T: IConvertible { ICollection&lt;IValue&lt;T&gt;&gt; Values { get; set; } }
  public interface IValue&lt;T&gt; where T: IConvertible { T Value { get; set; } }
  </code></pre>
  
  <p>Since the value in each column could be an int/double/..., I assume I must have an actual class just for the value (I don't think I can use a data annotation attribute on a collection type), as such:</p>
  
  <pre><code>public class IntValue: IValue&lt;int&gt;
  {
      [DisplayFormat(DataFormatString = "{0:#,##0;-#,##0;'---'}", ApplyFormatInEditMode = true)]
      public int Value { get; set; }
  }
  </code></pre>
  
  <p>Of course, that looks weird, since you could just make it a generic class Value that implements IValue and be done with it, but if I do the silly thing and make a class for each possible type (now that I type it out, that sounds really bad, I know), I can use the DisplayFormat attribute and don't have to worry about the way it will present itself to the user, it'll always be appropriate.</p>
  
  <p>Now, for the classes that implement IColumn and IReport, that is simple:</p>
  
  <pre><code>public class Report&lt;T&gt;: IReport&lt;T&gt; where T: IConvertible 
  {
      public ICollection&lt;IColumn&lt;T&gt;&gt; Columns { get; set; }
      public Report() { Columns=new List&lt;IColumn&lt;T&gt;&gt;(); }
  }
  
  public class Column&lt;T&gt;: IColumn&lt;T&gt; where T: IConvertible 
  {
      public ICollection&lt;IValue&lt;T&gt;&gt; Values { get; set; }
      public Column() { Values = new List&lt;IValue&lt;T&gt;&gt;(); }
  }
  </code></pre>
  
  <p>From the list of interfaces and classes, you will immediately see that this makes it impossible to have a report where some columns have other types. So it's not possible to create a report where some columns are int, some are double,... Since the generic constraint in IReport makes you specify a type, you are stuck with that for all columns, since it propagates down to the value of each column... And that is exactly what I want really.</p>
  
  <p>I feel like I'm not getting anywhere, and probably missing something really simple, so a nudge in the right direction would be appreciated.</p>
  
  <p>TL;DR: How do I get a generic collection in a non-generic type?</p>