我的解决方案中有 2 个项目,一个web-service
项目和一个win-forms
项目。我想将 web 服务的返回数据转换为 win-forms 数据。我在两个项目中都定义了类终端。在 win 应用程序中,我编写了这个演员表:
static public implicit operator List<Terminal>(EService.Terminal[] svcTerminals)
{
List<Terminal> terminals = new List<Terminal>();
foreach (var svcTerminal in svcTerminals)
{
Terminal terminal = new Terminal();
terminal.TerminalID = svcTerminal.TerminalID;
terminal.TerminalTypeID = svcTerminal.TerminalTypeID;
terminal.TerminalGUID = svcTerminal.TerminalGUID;
terminal.Description = svcTerminal.Description;
terminal.Name = svcTerminal.Name;
terminal.PortID = svcTerminal.PortID;
terminals.Add(terminal);
}
return terminals;
}
但它不起作用并给出错误user-defined conversion must convert to or from enclosure type,这发生在List cast 中。但是在终端演员中一切都很好
static public implicit operator Terminal(EService.Terminal svcTerminal)
{
Terminal terminal = new Terminal();
terminal.TerminalID = svcTerminal.TerminalID;
terminal.TerminalTypeID = svcTerminal.TerminalTypeID;
terminal.TerminalGUID = svcTerminal.TerminalGUID;
terminal.Description = svcTerminal.Description;
terminal.Name = svcTerminal.Name;
terminal.PortID = svcTerminal.PortID;
return terminal;
}
谁能帮我解决这个问题,以便我可以
return (List<Terminal>)eService.CheckTerminal(guid, ref cityName, ref portName);
代替
List<Terminal> terminals = new List<Terminal>();
var svcTerminals = eService.CheckTerminal(guid, ref cityName, ref portName);
foreach (var svcTerminal in svcTerminals)
{
Terminal terminal = new Terminal();
terminal.TerminalID = svcTerminal.TerminalID;
terminal.TerminalTypeID = svcTerminal.TerminalTypeID;
terminal.TerminalGUID = svcTerminal.TerminalGUID;
terminal.Description = svcTerminal.Description;
terminal.Name = svcTerminal.Name;
terminal.PortID = svcTerminal.PortID;
terminals.Add((Terminal)svcTerminal);
}
return terminals;