SharpMap.Geometries.Geometry

SharpMap.Geometries.Geometry 
Tags: General Topics

Jan 30 2007 at 12:46 AM
Error 1 The best overloaded method match for 'SharpMap.Data.Providers.GeometryProvider.GeometryProvider(System.Collections.ObjectModel.Collection<SharpMap.Geometries.Geometry>)' has some invalid arguments D:/Documents and Settings/Ibra/Meus documentos/Visual Studio 2005/Projects/Mapa/Mapa/Form1.cs 151 44 Mapa




Help Me



Ibraim Mansur
BR

Coordinator
Jan 30 2007 at 1:21 AM
Hi,

Do you have some more code and description for me.

regards
Christian

Jan 30 2007 at 2:26 AM

System.Collections.Generic.List<SharpMap.Geometries.Geometry> Pontos = new System.Collections.Generic.List<SharpMap.Geometries.Geometry>();
.
.
.
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (map != null)
{
SharpMap.Geometries.Point ClickPnt = new SharpMap.Geometries.Point();
ClickPnt = map.ImageToWorld(new System.Drawing.Point(e.X, e.Y));
Pontos.Add(ClickPnt);

VectorLayer m_VectorLayer = new VectorLayer("Ponto");
m_VectorLayer.DataSource = new SharpMap.Data.Providers.GeometryProvider(Pontos); -> "Error"
m_VectorLayer.Style.Symbol = new Bitmap("D://BOLA1.BMP");
map.Layers.Add(m_VectorLayer);
pictureBox1.Image = map.GetMap();
}

}
Error 2 The best overloaded method match for 'SharpMap.Data.Providers.GeometryProvider.GeometryProvider(System.Collections.ObjectModel.Collection<SharpMap.Geometries.Geometry>)' has some invalid arguments D:/Documents and Settings/Ibra/Meus documentos/Visual Studio 2005/Projects/Mapa/Mapa/Form1.cs 175 32 Mapa

Error 3 Argument '1': cannot convert from 'System.Collections.Generic.List<SharpMap.Geometries.Geometry>' to 'System.Collections.ObjectModel.Collection<SharpMap.Geometries.Geometry>' D:/Documents and Settings/Ibra/Meus documentos/Visual Studio
2005/Projects/Mapa/Mapa/Form1.cs 175 77 Mapa

This error happened after I changed DLL to open the data through MsSqlSpatial


Help Me


Ibraim Mansur
Br

Jan 30 2007 at 8:12 AM
Ibraim,
I was getting the same sort of error as your error 3. It went away when I used the correct versions of the SharpMap.dll and SharpMap.UI.dll. The version numbering it was looking for was 0.9.2423.31552.
HTH
Best regards

Jan 31 2007 at 1:15 AM
If I change DLL, it doesn't locate the referêcia for MsSqlSpatial.
Where finds Dll's that it corrects my mistake and have reference for MsSqlSpatial.


Thank you



Developer
Jan 31 2007 at 2:52 AM
Download MsSqlSpatial data provider from this changeset:
http://www.codeplex.com/SharpMap/SourceControl/DownloadSourceCode.aspx?changeSetId=6695

Best regards,
Ricardo Stuven.

Developer
Jan 31 2007 at 2:55 AM
...And it's not needed a reference to MsSqlSpatialLibrary.dll. It's uniquely to be used by msscmd.exe and to be deployed in the database, it should be not used in other contexts.

Best regards,
Ricardo Stuven.
 
<think>我们正在处理一个C#类型转换问题:如何将字符串转换为SharpMap.Layers.ILayer类型?首先,需要明确:不能直接将字符串转换为接口类型(ILayer),除非字符串包含足够的信息来创建或获取一个实现了该接口的对象。通常,我们需要一个映射关系(如从字符串标识符到具体图层对象的映射)或者使用工厂模式、依赖注入容器等来创建对象。可能的解决方案:1.使用一个字典(Dictionary)来映射字符串键到ILayer对象。2.使用反射(Reflection)来根据字符串创建类型(如果字符串是类型名)。3.使用工厂方法,根据字符串参数返回相应的ILayer实现。根据问题描述,我们假设用户有一个字符串,希望得到对应的ILayer对象。下面给出具体实现方法:方法一:使用字典(适用于已知有限个图层的情况)我们可以预先创建一些图层对象,并存储在字典中,通过字符串键来获取。方法二:使用反射(适用于字符串是完整类型名的情况)如果字符串是一个实现了ILayer接口的类的完全限定名,那么我们可以使用反射来创建实例。方法三:工厂模式(推荐)定义一个工厂类,根据传入的字符串参数创建相应的图层对象。由于问题中没有提供具体的图层类型,我们假设有几种具体的图层类(如VectorLayer,WmsLayer等)实现了ILayer接口。下面给出一个示例代码:首先,我们定义一个简单的工厂:```csharpusingSystem;usingSharpMap.Layers;publicstaticclassLayerFactory{publicstaticILayerCreateLayer(stringlayerType){switch(layerType){case"VectorLayer":returnnewVectorLayer("NewVectorLayer");case"WmsLayer":returnnewWmsLayer("NewWMSLayer");//添加更多图层类型...default:thrownewArgumentException($"Unknownlayertype:{layerType}");}}}```然后,我们可以这样使用:```csharpstringlayerString="VectorLayer";ILayerlayer=LayerFactory.CreateLayer(layerString);```如果图层类型需要更多的参数,可以在工厂方法中传递,或者使用配置对象。方法二(反射)的示例:```csharppublicstaticILayerCreateLayerByReflection(stringtypeName){Typetype=Type.GetType(typeName);//获取类型,注意typeName必须是完全限定名(包括命名空间)if(type==null){thrownewTypeLoadException($"Type{typeName}notfound.");}//创建实例,假设类型有无参构造函数objectinstance=Activator.CreateInstance(type);if(instanceisILayerlayer){returnlayer;}thrownewInvalidCastException($"Type{typeName}doesnotimplementILayer.");}```使用反射时需要注意:-类型名必须是完整的(包括命名空间),例如:"SharpMap.Layers.VectorLayer,SharpMap"(如果类型在SharpMap程序集中)。-需要处理异常情况(如类型不存在、没有无参构造函数、不实现ILayer等)。总结:直接转换是不可能的,必须通过某种方式创建或获取对象。推荐使用工厂模式,因为它更可控,避免了反射的安全性和性能问题。如果用户的问题是基于某个特定框架(比如有注册图层的方式),那么可能需要根据框架提供的机制来实现。相关问题:1.如何在C#中实现工厂模式?2.反射在C#中如何安全使用?3.SharpMap中ILayer接口有哪些常用实现类?4.如何管理多个图层的创建和销毁?</think>### 如何解决字符串转换为 SharpMap.Layers.ILayer 的类型转换错误? 在 C# 中,直接将字符串转换为 `SharpMap.Layers.ILayer` 接口类型是不可能的,因为字符串与接口类型之间没有隐式或显式的转换关系。以下是系统化的解决方案: #### 1. **理解错误本质** ```csharp string layerName = "MyLayer"; SharpMap.Layers.ILayer layer = (SharpMap.Layers.ILayer)layerName; // ❌ 编译错误 ``` 错误原因:字符串是基础数据类型,而 `ILayer` 是接口,两者属于完全不同的类型体系[^1]。 #### 2. **正确解决方案** 需要通过**图层名称查找**或**工厂模式**创建对象: ##### 方案 1:通过图层集合查找(推荐) ```csharp // 假设 map 是 Map 对象,已包含图层集合 string targetLayerName = "RoadsLayer"; // 在图层集合中查找匹配名称的 ILayer 对象 var layer = map.Layers.FirstOrDefault(l => l.LayerName == targetLayerName); if (layer != null) { // 成功获取 ILayer 对象 Console.WriteLine($"Layer found: {layer.LayerName}"); } else { Console.WriteLine("Layer not found"); } ``` ##### 方案 2:使用工厂模式创建 ```csharp public static ILayer CreateLayer(string layerType) { switch (layerType.ToLower()) { case "vector": return new VectorLayer("New Vector Layer"); case "wms": return new WmsLayer("https://example.com/wms"); default: throw new ArgumentException($"Unknown layer type: {layerType}"); } } // 使用示例 ILayer roadLayer = CreateLayer("vector"); // ✅ 正确创建接口对象 ``` #### 3. **动态类型创建(高级场景)** ```csharp // 通过反射动态创建实例(需完整类型名称) string typeName = "SharpMap.Layers.VectorLayer, SharpMap"; Type layerType = Type.GetType(typeName); if (layerType != null && typeof(ILayer).IsAssignableFrom(layerType)) { ILayer layer = (ILayer)Activator.CreateInstance(layerType); layer.LayerName = "DynamicLayer"; } ``` #### 关键注意事项: 1. **接口无法实例化**:`ILayer` 是接口,必须通过具体实现类(如 `VectorLayer`)实例化 2. **名称映射**:字符串应作为标识符,而非转换目标 3. **依赖管理**:确保图层集合已初始化(通过 `Map.Layers` 属性) 4. **错误处理**:始终检查 `null` 引用 > 示例中 `VectorLayer` 和 `WmsLayer` 是 SharpMap 中实现 `ILayer` 的具体类[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值