在泛型上调用实例方法的委托
我在寻找这个答案时运气不佳,因为我认为我对它的正确术语了解不够。
(为了清楚代码以及如何调用它而编辑)
我有一个实例化扩展方法的类:
public static class Foo
{
public static IList<T> Bar<T>(this DataTable table) where T : class, new()
{
IList<T> list = ... // do something with DataTable.
return list;
}
}
我这样调用这个方法:
DataTable table = SomehowGetTable();
IList<MyObject> list = table.Bar<MyObject>();
再次大大简化。
现在,我想做的是添加一个委托(?),以便在获得列表后,我可以在列表中的每个 T 上调用一个方法,如下所示:
public static class Foo
{
public static IList<T> Bar<T>(this DataTable table, MyDelegate postProcess)
{
IList<T> list = ... // do something with DataTable.
foreach(T item in list)
{
t.postProcess();
}
return list;
}
}
我不知道我需要做什么能够传递 postProcess.. 委托、谓词或操作,主要是因为我仍然面临 Linq 挑战。
或者,也许这根本不可能?
I'm not having much luck searching for this answer, as I think that I don't know enough about the proper terms for it.
(Edit for clarity of code and how I call it)
I have a class that instantiates an extension method:
public static class Foo
{
public static IList<T> Bar<T>(this DataTable table) where T : class, new()
{
IList<T> list = ... // do something with DataTable.
return list;
}
}
I call this method like this:
DataTable table = SomehowGetTable();
IList<MyObject> list = table.Bar<MyObject>();
Again, greatly simplified.
Now, what I'd like to do, is add a delegate(?) so that after I get the list, I can call a method on each T in the list, like so:
public static class Foo
{
public static IList<T> Bar<T>(this DataTable table, MyDelegate postProcess)
{
IList<T> list = ... // do something with DataTable.
foreach(T item in list)
{
t.postProcess();
}
return list;
}
}
I don't know what I need to be able to pass in postProcess.. a delegate, a Predicate, or an Action, mostly because I'm Linq-challenged still.
Or, maybe this isn't even possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你想要的是一个 Action< T>所以你可以这样做:
What you want is an Action< T > so you can do:
首先,您需要将
Bar
定义为泛型,否则无法编译。其次,如果您尝试对列表中的每个元素进行操作,则需要传入一个委托,该委托采用
T
类型的单个参数并且不返回任何值。内置的 .NET 委托类型是Action
,它会很好地工作。所以,
First, you need to define
Bar
as generic, otherwise it won't compile.Second, if you're trying to operate on each element within the list, you need to pass in a delegate that takes a single parameter of type
T
and returns no value. A built-in .NET delegate type isAction<T>
, and it'll do fine.So,
签名将是
如今,.Net 通过
Action
和Func
委托将几乎所有您可能需要的方法签名都带到了表中。虽然 action 涵盖了所有 void 返回类型的方法,但 Func 引入了非 void 返回。请注意,T 必须定义为方法中的类型参数。编译器也许能够从您提供给方法的操作中推断出 T:
例如,如果您实际上正在处理进入不同类型的值,则签名可能如下所示:
使用如下
The signature would be
These days .Net brings virtually all method signatures to the table that you may ever need through
Action
andFunc
delegates. While action covers all void return type methods, Func introduces non-void returns.Note that T must be defined as type argument on your method. The compiler may be able to infer T from the action you provide into the method:
For example if you are actually processing the values coming into a different type, the signature may look like :
Used like
您可以执行以下任一操作
或添加通用约束:
You can do either
or add a generic constraint:
尝试
Action
:或者使用接口,这样您就不必传递操作:(我更喜欢这个)
Try
Action<T>
:Or use an interface so you do not have to pass action: (I prefer this one)