c# - Restricting generic type fails in a particular situation -


I want to separate my definitions from my implementation. I have an interface unit:

 < Code> Public Interface Unit & lt; E & gt; Where e: unit & lt; E & gt; {EntityId EntityId {get; } Bool ReadOnly {get; } Zero FailIfReadOnly (); E copy (); }  

E is the actual unit type, such as customer:

  public interface customer: entity & lt; Customers & gt; {}  

I have an implementation of FailIfReadOnly (): If readOnly == is true, throw an EntityIsReadOnlyException.

  Public class EntityIsReadOnlyException & lt; E & gt; Where e: unit & lt; E & gt; {Public EntityIsReadOnlyException (E unit): base (string.format ("entity {0} is read only", entity.EntityId)} {}} public class EntityImpl & lt; E & gt; : Unit & lt; E & gt; Where e: unit & lt; E & gt; {Public EntityImpl (E other) {} Public Child Only Read (Receive; Preserved Set;} Public Zero FailIfReadOnly () {If (! Read Only) New EntityIsReadOnlyException Throw  (this) Throw the new EntityIsReadOnlyException  causes a compile error: 

The best overloaded method for 'EntityIsReadOnlyException.EntityIsReadOnlyException (E)' has some invalid arguments for the match.

Argument '1': Can not be changed from 'EntityImpl' to 'E'

I can:

  EntityIsReadOnlyExcetion  Exc = New EntityIsReadOnlyException & lt; Customer & gt; (customerImpl);  

and even:

  entity  entity = new EntityImpl  (this);  

but not:

  EntityIsReadOnlyException  Exc = New EntityIsReadOnlyException & lt; E & gt; (this);  

In both cases, e is limited to sub-sections of the unit. My question is, why do I get this compilation error? Perhaps this is something very simple.

First of all, the exception is not obtained with the exception:

  Public Sector EntityIsReadOnlyException & lt; E & gt; : Exception where e: unit & lt; E & gt;  

Then notice that the manufacturer of your exception is a entity & lt; E & gt; do not take into account, but e

Method 1:

Your enterprise readonly expedition (unit ) for the method :

 Change your constructor to pick  2: Pass  Other  for your exception: 

  E other; Public EntityImpl (E other) {this.other = Other; } ... if (Read Only) New EntityIsReadOnlyException Throw & lt; E & gt; (other);  

Non functionalism: Try putting it in E:

  If (read only) the new unit will read the readonly exposition; E & gt; ((E) (entity  E & gt; other);  

It compiles but fails at runtime because your implementation object is not same as parameter e and can not be inserted.

Another short point: Your if (! Read only) check is incorrect, it should be (read only) .


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -