我有以下界面:
public interface ISapFunction
{
void Import<T>(T obj);
T Export<T>();
void Call(RfcRepository repo, RfcDestination dest);
}
Run Code Online (Sandbox Code Playgroud)
然后我尝试按如下方式实现它:
public class SapMaterialFormatter : ISapFunction
{
private static SapMaterialFormatter _self;
private string _formatted;
private string _raw;
private SapMaterialFormatter()
{
}
public void Import<string>(string obj)
{
_raw = obj;
}
public string Export<string>()
{
return _formatted;
}
public void Call(RfcRepository repo, RfcDestination dest)
{
var bapi = repo.CreateFunction("FUNCTION");
bapi.SetValue("IF_INPUT", _raw);
bapi.Invoke(dest);
_formatted = bapi.GetString("EF_OUTPUT");
}
public static SapMaterialFormatter Factory()
{
return _self ?? new SapMaterialFormatter();
}
}
Run Code Online (Sandbox Code Playgroud)
但编译器抱怨,生成语法错误:
实施有什么问题?
它们是通用方法参数,因此在调用这些接口方法时需要提供它们.
例如:impl.Import<string>(...).
您的实现应该只定义整个T泛型参数:
public void Import<T>(T obj)
{
}
Run Code Online (Sandbox Code Playgroud)
如果需要所需的效果,则需要定义泛型类型参数T并删除其方法范围的对应项:
public interface ISapFunction<T>
{
void Import(T obj);
T Export();
void Call(RfcRepository repo, RfcDestination dest);
}
Run Code Online (Sandbox Code Playgroud)