我评论道-现在还不太清楚你到底想要实现什么,我怀疑你可以用一种比你想要的更好的方式来实现。。。但我要说的是
认为
下面这样的方法可能会奏效。
更改基参数类,使您不必依赖构造函数来设置其内部字段,然后将一些泛型方法仅约束为基类型,这样您最终会遇到这种情况:
public class BaseParamsClass
{
public virtual void SetParam(int pBaseParam)
{
baseParam = 0;
}
public int baseParam;
}
public class Parent1ParamsClass : BaseParamsClass
{
public override void SetParam(int pBaseParam)
{
base.SetParam(pBaseParam);
//do other stuff specific to this class...
}
public int parentParam1;
}
public class Parent2ParamsClass : BaseParamsClass
{
public override void SetParam(int pBaseParam)
{
base.SetParam(pBaseParam);
//do other stuff specific to this class...
}
public int parentParam2;
}
public delegate void GenericCallback<T>(T theParams) where T : BaseParamsClass, new();
private IEnumerator GenericCorFunction<T>(GenericCallback<T> callback) where T:BaseParamsClass, new()
{
// This demonstrate few actions i do before the call
yield return new WaitForSeconds(1);
// Now i get the result, it can be 0-10, each one should activate different callback
int result = 0;
//I assume you want result here.
//Also note that you can't use the constructor to set the base param as at compile time
//we're not sure which type will be being used. There are ways around this but it's
//probably clearer to use basic constructor then call the SetParam virtual/overridden method
var param = new T();
param.SetParam(result);
callback(param);
}
您可以这样使用它:
var newEnumerator = GenericCorFunction<Parent2ParamsClass>(p =>
{
//this is the callback function body. This will only run when
//called at the end of the GenericCorFunction
//Do things with p, which will be a PArent2ParamsClass object
//with its baseParam field set to whatever result was.
if (p.baseParam == 3)
{
throw new NotImplementedException();
}
});
//do stuff with newEnumerator...