Note that there are some explanatory texts on larger screens.

plurals
  1. POAttempting to construct a generic type in C# fails with MissingMethodException
    primarykey
    data
    text
    <p>I have the following class definition:</p> <pre><code>public abstract class ControllerBase&lt;T, V&gt; : Controller where T : EntityBase&lt;T&gt; where V : GenericRepository&lt;T&gt; </code></pre> <p>Then later on in the class I have the following code:</p> <pre><code>private V _repo; ... _repo = (V)Activator.CreateInstance(typeof(V), _dbC, _c); </code></pre> <p>This compiles just fine. I then have a class with the following definition:</p> <pre><code>public class SecurityRoleController : ControllerBase&lt;SecurityRole, GenericRepository&lt;SecurityRole&gt;&gt; </code></pre> <p>That also compiles just fine. However, when I try to hit /SecurityRole in my browser, I get an exception that <code>Constructor on type GenericRepository'1 not found</code>. (Note that it's actually a backtick in the exception but that breaks SO formatting.) This is despite the fact that <code>GenericRepository&lt;T&gt;</code> has a public constructor that works just fine when I try to directly create an instance of the class.</p> <p>Anyone know how I can properly construct a generic instance of the class?</p> <p>TIA,<br> Benjy</p> <p>EDIT:</p> <p>Constructor of <code>GenericRepository</code>:</p> <pre><code>public GenericRepository(DbContext dbContext, Context c, string[] includes = null) { _dbContext = dbContext; _c = c; if (includes != null) { _includes = includes; } return; } </code></pre> <p>And, the types of <code>_dbC</code> and <code>_c</code> are of <code>DbContext</code> and <code>Context</code>, the types that the <code>GenericRepository</code> requires in its constructor - and yes, I got these from <code>.GetType().FullName</code>. Any thoughts?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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