C # injection problem

I have 3 classes: A, B and C

All these classes implement the IMyInterface interface.

I would like the interface to be defined as follows:

internal IMyInterface<E> where E: class { E returnData(); } 

So that it can return data of type E. Type "E" will be a POCO object created using Entity Framework v4.

In a separate class I have:

 public class MyClass() { IMyInterface<??> businessLogic; public setBusinessLogic(IMyInterface<E> myObject) where E : class { businessLogic = myObject; } } 

I tried putting <object> instead of <??> , but it could not use my poco object type.

I tried my objects implement an empty IEntity interface, then using

 IMyInterface<IEntity> businessLogic; ... businessLogic = new A<POCOObject>(); 

leads to:

  Cannot implicitly convert type 'A<POCOObject>' to 'IMyInterface<IEntity>'. An explicit conversion exists (are you missing a cast?) 

Any recommendations?

Edit: I tried declaring classes A, B, and C as:

 internal class A : IBidManager<EntityObjectType> 

and

 internal class A<E> : IBidManager<E> where E : class 

leads to the same error.

+4
source share
1 answer

It should be either

 public class MyClass<E> where E : IEntity, class { IMyInterface<E> businessLogic; public setBusinessLogic(IMyInterface<E> myObject) { businessLogic = myObject; } } 

or

  public class MyClass { IMyInterface<POCOObject> businessLogic; public setBusinessLogic(IMyInterface<POCOObject> myObject) { businessLogic = myObject; } } 

If you want your business object to process any POCO object, and each of them has an interface, you need to specify where E : class, IEntity at the class level. Otherwise, you use a specific type for generic arg

+4
source

Source: https://habr.com/ru/post/1314385/


All Articles