[英]c# methods that takes an Interface?
I am new to Interfaces. 我是界面的新手。
I have a lot of objects that I pass as DTOs through my layers to the UI. 我有很多对象作为DTO通过我的图层传递到UI。 Some of them are quite complex (Quite a few properties), but I only want to use them, in certain circumstances, in DropDown lists.
它们中的一些非常复杂(有一些属性),但是在某些情况下,我只想在DropDown列表中使用它们。 These DTOs all have an
int Id
, and a string Description
. 这些DTO都有一个
int Id
和一个string Description
。
I would like to create a static function that takes a List<>
of one of these objects, and returns a List<SelectListItem>
我想创建一个静态函数,该函数接受这些对象之一的
List<>
,并返回List<SelectListItem>
So, I am trying to use Interfaces for the first time. 因此,我尝试第一次使用接口。
I created an Interface: 我创建了一个接口:
public interface IListableItem
{
int Id { get; set; }
string Description { get; set; }
}
And then, I assigned that interface to one of my DTO objects I am trying to convert: 然后,我将该接口分配给我要转换的DTO对象之一:
public class CategoryDto : BaseDto , IListableItem
{
public int PortfolioId { get; set; }
public string Description { get; set; }
public List<ExtendedSubCategoryDto> SubCategories { get; set; }
public bool IsExpenseCategory { get; set; }
public CategoryDto()
{
SubCategories = new List<ExtendedSubCategoryDto>();
}
}
Then, I created my generic method that takes a list of the category dtos, and will hopefully return a list 然后,我创建了一个通用方法,该方法采用了dtos类别的列表,并有望返回一个列表
public static List<SelectListItem> TranslateToSelectList(List<IListableItem> source)
{
var reply = source.Select(item => new SelectListItem
{
Value = item.Id.ToString(CultureInfo.InvariantCulture), Text = item.Description
}).ToList();
return reply;
}
But, when I attempt to use this method, passing it a List, it fails. 但是,当我尝试使用此方法,将其传递给List时,它将失败。
model.Categories =
Translator.TranslateToSelectList(MyService.GetCategoriesByPortfolioId());
GetCategoriesByPortfolioId returns a List. GetCategoriesByPortfolioId返回一个列表。
It's failing with the error: 错误失败:
CategoryDto is not assignable to IListableItem
无法将CategoryDto分配给IListableItem
It's probably a basic Interface understanding issue on my part, but what am I doing wrong, and how can I fix it? 就我而言,这可能是一个基本的界面理解问题,但是我做错了什么,该如何解决?
If your method expects List<IListableItem>
, you can't pass List<CategoryDTO>
. 如果您的方法需要
List<IListableItem>
,则不能传递List<CategoryDTO>
。
If that would be possible, you could Add
different instances of elements that implement the interface IListableItem
into a collection that is holding CategoryDTO
elements, and read them. 如果可能的话,可以
Add
实现IListableItem
接口的元素的不同实例Add
到保存CategoryDTO
元素的集合中,然后读取它们。
Ultimately, that wouldn't make sense. 最终,这没有任何意义。
You can fix it, if you use IEnumerable
interface. 如果使用
IEnumerable
接口,则可以修复它。 That allows covariance(going from higher type to lower type in generic type parameter) . 这允许协方差(在泛型类型参数中从较高的类型到较低的类型) 。
The reason it works, is that IEnumerable
is a "read-only view" of collection, thus you can't really add anything to it - plus what's important, the type parameter
is marked as covariant . 之所以起作用,是因为
IEnumerable
是collection的“只读视图”,因此您无法真正向其添加任何内容-再加上重要的一点, type parameter
被标记为covariant 。
public static List<SelectListItem> TranslateToSelectList(
IEnumerable<IListableItem> source)
{..}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.