Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You say you want to cast the object to <code>IRepository&lt;TEntity&gt;</code>, but <code>TEntity</code> is only available at runtime (you have a reference to the <code>Type</code> object in the <code>entityType</code> variable).</p> <p>If <code>IRepository&lt;entityType&gt;</code> were possible, what would you expect to get back from methods such as <code>FindById</code>? Probably an <code>entityType</code> instance, I presume? Now, as <code>entityType</code> could be any type and is only determined at runtime, the compiler cannot possibly know how to compile any code involving <code>entityType</code> variables. Therefore, <code>IRepository&lt;entityType&gt;</code> is not possible.</p> <p>There are two possible solutions, depending on what you want to achieve:</p> <ol> <li>If you do not want to run any code that needs to know about your entity objects, implement an additional <code>IRepository</code>-like interface that handles entities of type <code>object</code> (and possibly throws <code>ArgumentException</code>s if anything is added that does not match the current <code>entityType</code>). Cast the result of your <code>Activator.CreateInstance</code> call to that new interface type.</li> <li>Create a new generic class that takes the entity type as a generic argument. Move all the code that deals with your entities and repositories into that new class, where it always uses the entity type supplied as a generic argument. Instantiate that generic class via <code>Activator.CreateInstance</code> similarly to what you're doing now directly with the <code>TradeSiftRepository&lt;T&gt;</code> class.</li> </ol>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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