<p>Is there a way to collect (e.g. in a List) multiple 'generic' objects that don't share a common super class? If so, how can I access their common properties?</p>

<p>For example:</p>

<pre><code>class MyObject&lt;T&gt;
{
   public T Value { get; set; }
   public string Name { get; set; }

   public MyObject(string name, T value)
   {
      Name = name;
      Value = value;
   }
}

var fst = new MyObject&lt;int&gt;("fst", 42);
var snd = new MyObject&lt;bool&gt;("snd", true);

List&lt;MyObject&lt;?&gt;&gt; list = new List&lt;MyObject&lt;?&gt;&gt;(){fst, snd};

foreach (MyObject&lt;?&gt; o in list)
   Console.WriteLine(o.Name);
</code></pre>

<p>Obviously, this is pseudo code, this doesn't work.</p>

<p>Also I don't need to access the .Value property (since that wouldn't be type-safe).</p>

<p><strong>EDIT:</strong> Now that I've been thinking about this, It would be possible to use sub-classes for this. However, I think that would mean I'd have to write a new subclass for every new type.</p>

<hr>

<p>@<a href="https://stackoverflow.com/questions/51586/accessing-non-generic-members-of-a-generic-object#51621">Grzenio</a>
Yes, that exactly answered my question. Of course, now I need to duplicate the entire shared interface, but that's not a big problem. I should have thought of that...</p>

<p>@<a href="https://stackoverflow.com/questions/51586/accessing-non-generic-members-of-a-generic-object#51611">aku</a>
You are right about the duck typing. I wouldn't expect two completely random types of objects to be accessible.</p>

<p>But I thought generic objects would share some kind of common interface, since they are exactly the same, apart from the type they are parametrized by. Apparently, this is not the case automatically.</p>

[{'answer_id': 51611, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 2, 'selected': False, 'text': "<p>C# doesn't support duck typing. You have 2 choices: interfaces and inheritance, otherwise you can't access similar properties of different types of objects.</p>\n"}, {'answer_id': 51621, 'author': 'Grzenio', 'author_id': 5363, 'author_profile': 'https://Stackoverflow.com/users/5363', 'pm_score': 4, 'selected': True, 'text': "<p>I don't think it is possible in C#, because MyObject is not a baseclass of MyObject. What I usually do is to define an interface (a 'normal' one, not generic) and make MyObject implement that interface, e.g.</p>\n\n<pre><code>interface INamedObject\n{\n    string Name {get;}\n}\n</code></pre>\n\n<p>and then you can use the interface:</p>\n\n<pre><code>List&lt;INamedObject&gt; list = new List&lt;INamedObject&gt;(){fst, snd};\n\nforeach (INamedObject o in list)\n   Console.WriteLine(o.Name);\n</code></pre>\n\n<p>Did it answer your question?</p>\n"}, {'answer_id': 51634, 'author': 'Motti', 'author_id': 3848, 'author_profile': 'https://Stackoverflow.com/users/3848', 'pm_score': 0, 'selected': False, 'text': '<p>The best way would be to add a common base class, otherwise you can fall back to reflection.</p>\n'}]
['https://Stackoverflow.com/questions/51586', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2597/']