不同命名空间中的相同对象如何转换?

3
这些是错误:
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 上搜索了很多,但它可能会对你有所帮助:

仍在自我探索中。


为什么会在不同的命名空间中有两个完全相同的类? - khellang
AutoMapper - CodeProject(自动映射器 - CodeProject) - Habib
5个回答

2

在C#中,即使两种类型在所有方面都相同,也不能进行转换,除非存在隐式转换。因此,您可以像上面链接中所示编写隐式转换运算符,或者使用类似AutoMapper的工具在两个对象之间进行复制。


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。


0

在随机尝试代码和API时,我已经找到了另一种解决此问题的方法,并通过将一个对象中的众所周知的文本值复制到另一个对象中来部分解决了两个错误。希望我也能对linestring部分做同样的事情。我发布这篇文章只是为了防止其他人遇到此问题并发现它是有用的解决方案。下面是新的代码区域。

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

感谢您的帮助,我希望其他人也能从这个解决方案中受益。


0

仅供参考...但其中一种选择是使用JSON解析器将一个类序列化为JSON,然后将其反序列化回不同的类。简短而简单的答案,但如果你只是想从Contoso.Project.UrMom中获取属性,并直接将它们传输到Albiet.Project.UrMom,那么它可以很好地工作。


0

我发现了这个另一种选择, 它基于对象的序列化。据我所知,它的缺点是需要访问磁盘。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接