2012-12-21 114 views
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,但它可以幫助你:

還是挖自己。

+0

爲什麼你會在不同的命名空間中有兩個相同的類? – khellang

+0

[AutoMapper-CodeProject](http://www.codeproject.com/Articles/61629/AutoMapper) – Habib

回答

2

在C#中,您不能從一種類型轉換爲另一種類型,即使它們用於所有目的都是相同的,而不會複製所有適當的信息等,除非存在implicit轉換。

所以,你可以寫一個隱式轉換算子的如在上面的鏈接或者你可以使用一個工具,如AutoMapper兩個對象

+0

我正在跳上去! – Perfection

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

感謝您的幫助,我希望有人可能會發現這個解決方案有用。

0

查看本文爲了您自己的用途...但一種選擇是使用JSON解析器將一個類序列化爲JSON,然後將其反序列化回另一個類。簡短的答案,但如果你正在尋找的是從Contoso.Project.UrMom中獲取屬性,並將它們直接傳送到Albiet.Project.UrMom,那麼效果很好。

0

我發現這個基於對象序列化的other alternative。就我而言,它有訪問磁盤的缺點。

相關問題