vendredi 19 juin 2020

c# generics cannot Convert From T to

If I have an interface

   public interface IAnimal
   {
      T Lick<T>(T entity) where T : class;
   }

And I have:

public class Dog:IAnimal
{          
     public void Lick<T>(T entity) where T : class
      {
        var result=someOtherFunction.GetObject<IObject>(entity); /// error 
        result.add(i=>i.ball==entity.ball);
      }
}

But I also have:

  public class IObject
    {
         public Ball ball{ get; set; }
    }

I get error cannot convet from T to IObject. but isnt T any class of which IObject belongs?

I dont want to change IAnimal to IAnimal because this will destroy the point of IAnimal dog = new Dog();

I Also dont want "Ball" models to derive IObject as IObject is specific to Dog class

Aucun commentaire:

Enregistrer un commentaire