c#泛函 - 另一个泛型的扩展

时间:2014-04-07 05:11:57

标签: c# generics

这是c#中通用方法的典型示例:

ParseObject po;

int i = po.Get<int>("someField");
string s = po.Get<string>("anotherField");

我想写一个像这样工作的扩展名 ...

 int i = po.ExampleGet<int>("someField");
 string i = po.ExampleGet<string>("anotherField");

所以,

(a)扩展名ExampleGet必须能够接受&lt;课程&gt;与ParseObject.Get一样,和

(b)扩展ExampleGet必须能够调用ParseObject.Get(以及做其他工作)。

这种n扩展的语法是什么,它以这种方式使用泛型?

1 个答案:

答案 0 :(得分:5)

可能您正在寻找extension methods

static public class Extensions
{
  static T ExampleGet<T>(this ParseObject po, string name)
  {
      return po.Get<T>(name);
  }
}