简体   繁体   English

C#Func <>和泛型

[英]C# Func<> and generics

So, I'm a bit out of my comfort zone when dealing with Func<>, Generics and lambda expressions but I think I get the general idea (sort of) but still a bit confused. 因此,在处理Func <>,Generics和lambda表达式时,我有点不在我的舒适区域,但我认为我得到了一般的想法(有点),但仍然有点困惑。

I've implemented the SortableObservableCollection class (taken from online somewhere - thanks to whoever it was I got it from!) and it is used like this: 我已经实现了SortableObservableCollection类(取自某个地方的在线 - 感谢无论是谁从我那里得到它!)它的使用方式如下:

_lookuplistViewModel.Sort(x => x.BrandName, ListSortDirection.Ascending);

where x is the object type implemented by the sortable collection. 其中x是可排序集合实现的对象类型。 In this instance, BrandName is a property of the type of object implemented, but I want to use the above code in a generic class and pass in the property on which to sort. 在这个例子中,BrandName是实现的对象类型的属性,但是我想在泛型类中使用上面的代码并传入要排序的属性。 The Sort method looks like this: Sort方法如下所示:

public void Sort<TKey>(Func<T, TKey> keySelector, ListSortDirection direction)
{
  switch (direction)
  {
    case ListSortDirection.Ascending:
      {
        ApplySort(Items.OrderBy(keySelector));
        break;
      }
    case System.ComponentModel.ListSortDirection.Descending:
      {
        ApplySort(Items.OrderByDescending(keySelector));
        break;
      }
  }
}

The generic class on which the Sort method is called is defined like this: 调用Sort方法的泛型类定义如下:

public class ExtendedLookupManagerViewModel<VMod, Mod> : LookupManagerViewModel
where VMod : ExtendedLookupViewModel
where Mod : ExtendedLookupModelBase

and I'd like to create an instance of it like this: 我想像这样创建一个它的实例:

_medProd = new ExtendedLookupManagerViewModel<MedicinalProductViewModel, MedicinalProduct>(string property);

where property is the property on which to sort. 其中property是要排序的属性。 Ideally this should be type safe, but a string will suffice. 理想情况下,这应该是类型安全的,但字符串就足够了。

Can anyone help steer me in the right direction please? 任何人都可以帮助引导我朝着正确的方向前进吗?

您不应该接受字符串属性参数,而是接受Func<T, IComparable> ,其中T可能是VMod或Mod,具体取决于您要排序的内容。

Just make your constructor sig match the sig for the sort method, and cache the params for using in the collection when Sort() is called. 只需使构造函数sig与sort方法的sig匹配,并在调用Sort()时缓存params以便在集合中使用。 So not "string property" but rather whatever the sig is for the sort method. 所以不是“字符串属性”,而是对于sort方法的sig。

The passed parameter then would be a func that could be type specific and directing you to the element, the instantiation would be 然后传递的参数将是一个func,它可以是特定于类型的,并将您引导到元素,实例化将是

_medProd = new ExtendedLookupManagerViewModel<MedicinalProductViewModel, MedicinalProduct>(x => x.BrandName, ListSortDirection.Ascending);

Well, your Sort method is only generic in TKey - where does T come from? 那么,你的Sort方法只在TKey是通用的--T来自哪里? I suspect it should either be Func<VMod, TKey> or Func<Mod, TKey> but I'm unsure which from what you've shown. 我怀疑它应该是Func<VMod, TKey>Func<Mod, TKey>但是我不确定你从哪个中显示出来。

What would BrandName be a property of - MedicinalProductViewModel or MedicinalProduct ? BrandName是什么属性 - MedicinalProductViewModelMedicinalProduct Assuming it's MedicinalProduct , your method should be declared as: 假设它是MedicinalProduct ,您的方法应声明为:

public void Sort<TKey>(Func<Mod, TKey> keySelector, ListSortDirection direction)

At that point I suspect it will work... 那时我怀疑它会起作用......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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