相关疑难解决方法(0)

C#不能从泛型方法调用重载的非泛型方法

我有一些遗留代码,方法foo有700多个重载:

[DllImport("3rdparty.dll")]
protected static extern void foo(int len, ref structA obj);
[DllImport("3rdparty.dll")]
protected static extern void foo(int len, ref structB obj);
[DllImport("3rdparty.dll")]
protected static extern void foo(int len, ref structC obj);
//and 700 similar overloads for foo...
Run Code Online (Sandbox Code Playgroud)

我想通过使用泛型的单个方法公开这些重载方法:

public void callFoo<T>(int len)
    where T : new()  //ensure an empty constructor so it can be activated
{
   T obj = Activator.CreateInstance<T>(); //foo expects obj to be empty, and fills it with data
   foo(len, ref obj);

   //...do stuff with obj... …
Run Code Online (Sandbox Code Playgroud)

c# generics reflection

16
推荐指数
3
解决办法
2469
查看次数

标签 统计

c# ×1

generics ×1

reflection ×1