调用具有参数约束的函数时语法无效



某些上下文

我目前正在.NET框架4.7.2中尝试实现一种调用数据服务器的通用方法。我在使用约束和接口的通用方法时遇到了一些困难

我认为值得一提的是,在我的代码中错误或糟糕地使用接口是可能的,因为这是我第一次尝试使用它,请随时建议我可以改进

这是我创建的界面:

public interface IServerConfigDataBase<TProxy, TProxyInput, TProxyOutput> where TProxy : InheritedProxyClass<TProxyInput, TProxyOutput>
    where TProxyInput : class
    where TProxyOutput : StandardizedOutput
{
TProxy Proxy { get; set; }  // Generic class of the proxy I'll use
string FunctionName { get; set; }  // Used for information in case of error
void RequestErrorHandle(Exception e);  // Function to call in case of error during the request
}

这是一个使用我创建的接口的类:

public class ServerFunctionName : IServerConfigDataBase<ProxyName, ProxyInputName, ProxyOutputName>
{
public ProxyName Proxy { get; set; }
public string FunctionName { get; set; }
public ServerFunctionName(InputDataClass inputValues, string functionName)
{
Proxy        = ProxyName.Create();
Proxy.Input  = new InputContainer {InputData = inputValues};
FunctionName = functionName;
}
public void RequestErrorHandle(Exception e)
{
// Display an error message to the user
ServerRequestUtility.HandleServerException(Proxy.Output.NormalizedData, e, FunctionName);
}
}

我制作了一个函数,使用ServerFunctionName类请求服务器:

public static StandardizedOutput RequestServer<TServerBase, TProxyInput, TProxyOutput>(TServerBase server) where TServerBase : IServerConfigDataBase<InheritedProxyClass<TProxyInput, TProxyOutput>, TProxyInput, TProxyOutput>
                       where TProxyInput : class
                       where TProxyOutput : StandardizedOutput
{
try
{
server.Proxy.Execute();  // Calling the server and actualize the content of server.Proxy.Output
}
catch (Exception e)
{
server.RequestErrorHandle(e);
}
return server.Proxy.Output;
}

问题

现在,我很难调用最后一个函数。当我尝试这样做时:

ServerServiceRequest.RequestServer<ServerFunctionName, ProxyInputName, ProxyOutputName>(
new ServerFunctionName(
new InputContainer { Property = "" },
"nameOfFunction"));

我收到错误CS0311,消息如下:

类型"ServerFunctionName"不能用作泛型类型或方法"ServerServiceRequest.RequestServer<服务器功能名称、代理输入名称、代理输出名称>(TServerBase('。

没有从"ServerFunctionName"到"IServerConfigDataBase<继承的代理类<ProxyInputName、ProxyOutputName>、;,ProxyInputName,ProxyOutputName>'。

我知道我试图使用类ServerFunctionName作为接口IServerConfigDataBase,但它不起作用,但我不知道我做错了什么以及如何解决它。

非常感谢

InheritedProxyClass<TProxyInput, TProxyOutput>是您在ServerFunctionName而不是类ProxyName中需要的接口。

我不认为在这种情况下使用Generic是有用的。

最新更新