C# 我无法访问包装类';来自另一个类的方法
我必须从C#中的DLL库访问一些函数。 我的第一步是创建一个包装器类来处理它:C# 我无法访问包装类';来自另一个类的方法,c#,class,dll,wrapper,C#,Class,Dll,Wrapper,我必须从C#中的DLL库访问一些函数。 我的第一步是创建一个包装器类来处理它: //Class TMDLLWrapper.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; namespace TrueMarbleData { class TMDLLWrapper { [Dll
//Class TMDLLWrapper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace TrueMarbleData
{
class TMDLLWrapper
{
[DllImport("TrueMarbleDLL.dll")]
public static extern int GetTileSize(out int width, out int height);
[DllImport("TrueMarbleDLL.dll")]
public static extern int GetNumTiles(int zoomLevel, out int numTilesX, out int numTilesY);
[DllImport("TrueMarbleDLL.dll")]
public static extern int GetTileImageAsRawJPG(int zoomLevel, int tileX, int tileY, out byte imageBuf, int bufSize, out int jpgSize);
}
}
然后,我创建了一个接口和一个类来访问包装类的方法:
//Interface ITMDataController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace TrueMarbleData
{
[ServiceContract]
public interface ITMDataController
{
[OperationContract]
int GetTileWidth();
[OperationContract]
int GetTileHeight();
[OperationContract]
int GetNumTilesAcross(int zoom);
[OperationContract]
int GetNumTilesDown(int zoom);
[OperationContract]
byte[] LoadTile(int zoom, int x, int y);
}
}
//Class TMDataControllerImpl.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using TrueMarbleData;
namespace TrueMarbleData
{
[ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Multiple,
UseSynchronizationContext=false)]
internal class TMDataControllerImpl : ITMDataController
{
TMDLLWrapper obj = new TMDLLWrapper(); //Problem here: I cannot access
//wrapper class' methods
//Methods available: Equals,
//GetHashCode, GetType, ToString
public int GetTileWidth()
{
return 0;
}
public int GetTileHeight();
public int GetNumTilesAcross(int zoom);
public int GetNumTilesDown(int zoom);
public byte[] LoadTile(int zoom, int x, int y);
}
}
我需要从TMDataControllerImpl类访问包装器类的方法。但是,当我从TMDLLWrapper类创建对象时,我只能访问以下方法:Equals、GetHashCode、GetType和ToString。
这应该是一件容易的事情,但我还没有弄清楚错误在哪里。
有人能帮我吗?当您尝试以这种方式访问方法时:
TMDLLWrapper obj = new TMDLLWrapper();
您只能访问实例方法。因此,您应该以静态方式使用静态方法:
TMDLLWrapper.GetTileSize(a, b);
由于
TMDLLWrapper
中的方法是静态的,因此应使用语法访问静态方法:
TMDLLWrapper.GetTileSize(/*…*/)代码>
您不需要创建TMDLLWrapper
的实例。此外,最好将其设为静态类:
静态类TMDLLWrapper{/*…*/}
默认情况下,类是内部类。将包装器类公开:
public class TMDLLWrapper
{
...
}
然后,您将以静态方式访问它的方法:
TMDLLWrapper.GetNumTiles(...);
您不能从extern
方法中删除static
,这些方法用于从非托管dll导入函数。确定,然后创建第二个非静态方法并从那里调用它。。。我已经更新了答案。Thanx是否需要将其称为静态方法而不是实例方法?