转换来自不同命名空间的相同对象?

时间:2012-12-21 09:12:38

标签: c# api xserver

这些是错误:

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包含Point命名空间中的xRoute对象,该对象与xMap的对象相同。反正有没有复制或转换它?

这个问题在java示例中似乎没有发生,但在C#中也是如此。我相信如果我能解决这个错误,其他的也将被解决。我已经在API上搜索了我的屁股,但它可以帮助你:

还在挖自己。

5 个答案:

答案 0 :(得分:2)

在C#中,您无法从一种类型转换为另一种类型,即使它们用于所有目的相同,也不会复制所有属性等,除非存在implicit转换。

因此,您可以编写隐式转换opertor,如上面的链接所示,或者您可以使用AutoMapper之类的工具在两个对象之间进行复制

答案 1 :(得分:0)

我在此期间找到了另一个解决此问题的方法,同时随机播放代码和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;

编辑:相同的线串解决方案。

        // 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 };

感谢您的帮助,我希望有人可能会发现此解决方案也很有用。

答案 2 :(得分:0)

请查看此内容以供您自己使用...但一种选择是使用JSON Parser将一个类序列化为JSON,然后将其反序列化为另一个类。简短而简单的答案,但如果您要查找的只是从Contoso.Project.UrMom获取属性,并将它们直接传输到Albiet.Project.UrMom,它运行良好。

答案 3 :(得分:0)

我发现这个other alternative基于对象的序列化。就我而言,它具有访问磁盘的缺点。

答案 4 :(得分:0)

您是如何从 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。

相关问题