转换来自不同名称空间的相同对象
本文关键字:空间 对象 转换 | 更新日期: 2023-09-27 17:49:02
错误如下:
Error 1 Cannot implicitly convert type 'Plantool.xRoute.Point' to 'Plantool.xMap.Point'
Error 2 Cannot implicitly convert type 'Plantool.xRoute.Point' to 'Plantool.xMap.Point'
Error 3 Cannot implicitly convert type 'Plantool.xRoute.LineString' to 'Plantool.xMap.LineString'
我有这段代码,它带有一个命名空间。
using Plantool; //Contains xMap, xServer, xLocate
这就是我们讨论的函数。
/* createMap()
* Input: WaypointDesc[], Route
* Output: string mapURL
* Edited 21/12/12 - Davide Nguyen
*/
private static string createMap(xRoute.WaypointDesc[] waypointDesc, xRoute.Route route)
{
#region boundingBox
// Set boundingBox fand use corners from the calculated route
xMap.BoundingBox boundingBox = new xMap.BoundingBox();
boundingBox.leftTop = route.totalRectangle.rightTop;
boundingBox.rightBottom = route.totalRectangle.leftBottom;
#endregion
#region mapParams
// Build mapParams
xMap.MapParams mapParams = new xMap.MapParams();
mapParams.showScale = true;
mapParams.useMiles = false;
#endregion
#region imageInfo
// Create imageInfo and set the frame size and image format. NOTE: 1052; 863
xMap.ImageInfo imageInfo = new xMap.ImageInfo();
imageInfo.format = xMap.ImageFileFormat.PNG;
imageInfo.height = 1052;
imageInfo.width = 863;
imageInfo.imageParameter = "";
#endregion
#region layers
// Create a line from the calculated route
xMap.LineString[] lineStrings = new xMap.LineString[] { route.polygon };
xMap.Lines[] lines = new xMap.Lines[1];
xMap.LineOptions options = new xMap.LineOptions();
xMap.LinePartOptions partoptions = new xMap.LinePartOptions();
partoptions.color = new xMap.Color();
partoptions.visible = true;
partoptions.width = -10;
options.mainLine = partoptions;
lines[0] = new xMap.Lines();
lines[0].wrappedLines = lineStrings;
lines[0].options = options;
// Define customLayer that contains the object lines and set layers.
xMap.CustomLayer customLayer = new xMap.CustomLayer();
customLayer.visible = true;
customLayer.drawPriority = 100;
customLayer.wrappedLines = lines;
customLayer.objectInfos = xMap.ObjectInfoType.NONE;
customLayer.centerObjects = true;
xMap.Layer[] layers = new xMap.Layer[] { customLayer };
#endregion
#region includeImageInResponse
// Set argument includeImageInResponse to false (default).
Boolean includeImageInResponse = false;
#endregion
// Return object map using the following method.
xMap.Map map = xMapClient.renderMapBoundingBox(boundingBox, mapParams, imageInfo, layers, includeImageInResponse, null);
// Retrieve the image
string result = "http://" + map.image.url;
// Return the drawn map
return result;
}
问题在于boundingBox
对象和lineString
对象。route.totalRectangle
包含来自xRoute
命名空间的Point
对象,该对象与xMap
相同。有没有办法复制或转换它?
这个问题似乎不会在java示例中发生,但在c#中会发生。我相信如果我能解决这个错误,其他的也会解决的。我已经搜索了我的屁股上的API,但它可能会帮助你:
- xMap:http://xserver.ptvgroup.com/fileadmin/files/PTV-COMPONENTS/DeveloperZone/Documents/PTV_xServer/API/xMapAPI/pages/apidoc.html
- xRoute: http://xserver.ptvgroup.com/fileadmin/files/PTV-COMPONENTS/DeveloperZone/Documents/PTV_xServer/API/xRouteAPI/pages/apidoc.html
还在挖我自己
在c#中,除非存在隐式转换,否则不能从一种类型转换为另一种类型,即使它们在所有目的上都是相同的,也不能不复制所有属性等。
所以你可以像上面的链接那样写一个隐式转换操作符,或者你可以使用AutoMapper这样的工具在两个对象之间进行复制
我已经找到了这个问题的另一个解决方案,同时随机使用代码和API,这是通过将众所周知的文本值从一个对象复制到另一个对象来解决两个错误的部分解决方案。希望我能对线串部分做同样的处理。我张贴这只是以防其他人遇到这个,并发现它是一个有用的解决方案。
// Set boundingBox fand use corners from the calculated route
xMap.BoundingBox boundingBox = new xMap.BoundingBox();
xMap.Point rightTop = new xMap.Point();
rightTop.wkt = route.totalRectangle.rightTop.wkt;
xMap.Point leftBottom = new xMap.Point();
leftBottom.wkt = route.totalRectangle.leftBottom.wkt;
boundingBox.leftTop = rightTop;
boundingBox.rightBottom = leftBottom;
编辑:linestring的解决方案相同。
// Solution: Cannot implicitly conver ttype xRoute.LineString to xMap.LineString
xMap.LineString maproute = new xMap.LineString();
maproute.wkt = route.polygon.wkt;
// Create a line from the calculated route
xMap.LineString[] lineStrings = new xMap.LineString[] { maproute };
谢谢你的帮助,我希望有人会发现这个解决方案也很有用。
根据您自己的目的查看此内容…但一种选择是使用JSON解析器将一个类序列化为JSON,然后将其反序列化回另一个类。简短而简单的答案,但如果您想要的只是从Contoso.Project中获取属性。UrMom,并将它们直接转移到Albiet.Project。
我发现了另一种基于对象序列化的替代方法。我认为它的缺点是无法访问磁盘
如何从WSDL创建客户端类?我更喜欢通过命令行创建它们:
WSDL /sharetypes /out:"XServer.cs" /namespace:"Plantool"
"https://xroute-eu-n-test.cloud.ptvgroup.com/xlocate/ws/XLocate?WSDL"
"https://xroute-eu-n-test.cloud.ptvgroup.com/xroute/ws/XRoute?WSDL"
"https://xroute-eu-n-test.cloud.ptvgroup.com/xtour/ws/XTour?WSDL"
/sharetypes
确保像Point这样的类将被合并成一个单独的共享类
这也可以很好地与xServer2 API和它的wsdl。