【问题标题】:I Cannot Access Wrapper Class' methods from another class我无法从另一个类访问包装类的方法
【发布时间】:2014-04-16 15:43:19
【问题描述】:

我必须从 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
  {

    [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 类访问 Wrapper 类的方法。但是,当我从 TMDLLWrapper 类创建对象时,我只能访问这些方法:Equals、GetHashCode、GetType、ToString。 这应该是一件容易的事,我还没有弄清楚错误在哪里。 谁能帮帮我?

【问题讨论】:

  • 您需要将其称为静态方法而不是实例方法吗?

标签: c# class dll wrapper


【解决方案1】:

类默认为internal。将包装类公开:

public class TMDLLWrapper
{
...
}

然后您将以静态方式访问它的方法:

TMDLLWrapper.GetNumTiles(...);

【讨论】:

    【解决方案2】:

    由于TMDLLWrapper 中的方法是静态的,您应该使用语法来访问静态方法:

    TMDLLWrapper.GetTileSize(/* ... */);

    您不需要创建TMDLLWrapper 的实例。此外,最好将其设为静态类:

    static class TMDLLWrapper { /* ... */ }

    【讨论】:

      【解决方案3】:

      当您尝试以这种方式访问​​方法时:

      TMDLLWrapper obj = new TMDLLWrapper(); 
      

      您只能访问实例方法。所以你应该以静态方式使用静态方法:

      TMDLLWrapper.GetTileSize(a, b);
      

      【讨论】:

      • 您不能从extern 方法中删除static,这些方法用于从非托管dll 中导入函数。
      • 好的,然后创建第二个非静态方法并从那里调用它......我已经更新了答案。谢谢
      猜你喜欢
      • 1970-01-01
      • 2022-12-05
      • 1970-01-01
      • 1970-01-01
      • 2017-05-12
      • 2015-01-21
      • 2013-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多