我想传递一个泛型函数作为参数,以便可以泛型调用它。这可能吗?像这样:
public class MyClass
{
private void DoSomething<T>(T value)
{
// Do something with value.
}
public MyClass CreateNewAndDoSomething(Action<Action<T>> doSomething)
{
MyClass newMyClass = new MyClass();
doSomething.Invoke(newMyClass.DoSomething);
return newMyClass;
}
}
public class MyOtherClass
{
MyClass myClass;
public bool someBool;
public void AssignNewClass()
{
myClass = myClass.CreateNewAndDoSomething(action =>
{
if (someBool)
{
action.Invoke<int>(0);
}
else
{
action.Invoke<string>("Hello");
}
});
}
}
显然我在这里没有足够的解释性文字,我想我希望的代码很短,问题很明显...