简体   繁体   中英

How do I pass two similar concrete objects to a method with interface parameters that implement generics in C#?

I have the following interface declarations:

interface IOrder<T> where T: IOrderItem
{
     IList<T> Items { get; set; }
}

interface IDistro<T> : IOrder<T> where T: IOrderItem
{

}

I have two concrete classes, like so:

// DistroItem implements IOrderItem
public class Distro : IDistro<DistroItem>
{
    public IList<DistroItem> Items { get; set; }
}

// PerishableOrderItem implements IOrderItem
public class PerishableOrder : IDistro<PerishableOrderItem>
{       
    public IList<PerishableOrderItem> Items { get; set; }
}

Lastly, I have a static service method for saving to the database:

public static void UpdateDistro(IDistro<IOrderItem> distro)
{

}

My problem is, how do I pass a distro of either concrete type to my static method? The following doesn't compile:

Distro d = new Distro();
UpdateDistro(d);

The error is:

The best overloaded method match for UpdateDistro(IDistro<IOrderItem>)' has some invalid arguments

Is contravariance the answer? I tried adding <in T> to the original interface declaration, but that added more errors that I was unable to resolve. This is my first in depth foray into interfaces and I'm sure generics is adding complexity, so there might be a fundamental lack of understanding here.

Have you tried this:

public static void UpdateDistro<T>(IDistro<T> distro) 
  where T : IOrderItem
{  
} 

EDIT:

With empty implementations for DistroItem and PerishableItem classes (both implementing IOrderItem ), I've got the following compiling without an error:

Distro d = new Distro();
PerishableOrder p = new PerishableOrder();

UpdateDistro(d);
UpdateDistro(p);

You can define a covariant generic parameter in your interface, you need to change the interface a little bit though to ensure that T is not uses contravariantly:

public interface IOrder<out T> where T : IOrderItem
{
    IEnumerator<T> Items { get; }
}

public interface IDistro<out T> : IOrder<T> where T : IOrderItem
{

}

To define T as coverient parameter ( out ), allows for implicit conversion of classes that implement your variant interfaces.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM