[英]Func delegate as a constructor parameter
Trying to get my head round using delegates.试图使用代表来解决我的问题。
I have a class synchronise.cs
takes two collections of different types and edits them.我有一个类
synchronise.cs
需要两个不同类型的集合并编辑它们。 The collections are held in another other viewModel
class.集合保存在另一个其他
viewModel
类中。 I've written a constructor which takes two Func
delegates as parameters in synchronise.cs
:我编写了一个构造函数,它在
synchronise.cs
中将两个Func
委托作为参数:
public Synchronise(
Func<IEnumerable<T> , IEnumerable<S>> MappingStoT,
Func<IEnumerable<S>, IEnumerable<T>> MappingTtoS )
{
}
I'm then trying to new up a Synchronise
object in my ViewModel and pass two methods with the collections as parameters:然后,我尝试在我的 ViewModel 中新建一个
Synchronise
对象,并使用集合作为参数传递两个方法:
this.mySynchroniser = new Synchronise<IBindableData, object>(MappingStoT(CollectionA), MappingTtoS(CollectionB));
Finally my two methods:最后我的两种方法:
public IEnumerable<CollectionA> MappingStoT(IEnumerable<CollectionB> CollectionForEdit)
{
var query = blah blah
return query;
}
public IEnumerable<CollectionB> MappingTtoS(IEnumerable<CollectionA> CollectionForEdit)
{
var query = Blah blah
return query;
}
I'm getting not assignable to parameter type
on my new Synchronizer
.我
not assignable to parameter type
我的新Synchronizer
上的not assignable to parameter type
。 Am I going about this the right way?我会以正确的方式解决这个问题吗?
Your constructor expects Func
s, and not the result of a method call.您的构造函数需要
Func
,而不是方法调用的结果。 Try this:尝试这个:
class CollectionA { }
class CollectionB { }
class Synchronise<T, S>
{
public static Synchronise<CollectionB, CollectionA> Create()
{
return new Synchronise<CollectionB, CollectionA>(MappingStoT, MappingTtoS);
}
public Synchronise(Func<IEnumerable<T>, IEnumerable<S>> MappingStoT, Func<IEnumerable<S>, IEnumerable<T>> MappingTtoS)
{
}
public static IEnumerable<CollectionA> MappingStoT(IEnumerable<CollectionB> CollectionForEdit)
{
return null;
}
public static IEnumerable<CollectionB> MappingTtoS(IEnumerable<CollectionA> CollectionForEdit)
{
return null;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.