How to create anonymous class so that it can be passed to generic constructor?
<p>I am porting tests of a non-generic repository to a generic one. Instead of using <code>Repo&lt;MyType&gt;</code>, I want to test the more abstract methods with <code>Repo&lt;object&gt;</code> or something.</p>

<p>I tried to use an anonymous <em>instance</em>, but it didn't work:</p>

<pre><code>    [Test]
    public void AddToRepoIncrementsByOne()
    {
        IFile file = new FileFake();
        IByteSerializer&lt;object&gt; serializer = new SerializerObjectFake();
        repo = new WriteableFileRepository&lt;object&gt;(file, serializer);

        var obj = new { Name = "test" };
        repo.Add(obj);

        Assert.True(repo.Items.Count() == 1);
        Assert.AreEqual(repo.Items.Single().Name, obj.Nome);
        //----------------------------------^^^^  
        //-------------------compiler error here
    }
</code></pre>

<p>I tried to infer the class reference from the object, without success too:</p>

<pre><code>        var obj = new { Nome = "teste" };

        Type T = obj.GetType();

        IArquivo arquivo = new ArquivoFake();
        IByteSerializer&lt;T&gt; serializer = new SerializerObjectFake();
        //--------------^
        repo = new RepositórioArquivoEscrita&lt;T&gt;(arquivo, serializer);
</code></pre>

<p>How can I solve this? I am not demanding to use anonymous objects, I just thought it would be a pragmatic way to make my tests run, but I sure could do it other way if this is not a good design.</p>