访问静态方法中的非静态数据集



我是C#的新手,遇到了一个问题。我想做以下事情:

我有一个名为stateDataSet,我想从静态方法访问它。然而,插件im使用(SpecExplorer)不允许我将DataSet的实例声明为static。例如:

// This gives a long error from SpecExplorer
static State state = new State();
// This gives no error however I am not able to use `state` in any of the static functions.
State state = new State();

尝试访问中的state的函数必须是这样的,因为这是SpecExplorer:所必需的

[Rule]
static void create(int param) {
   // ACCESS STATE
}

我也尝试过中建议的解决方案:

访问静态方法中的非静态变量但尝试此操作时会出现完全相同的错误。

我得到的错误如下:

Syste中的成员无效。数据DataSet.set_Namespace(System.String):系统数据常见的ADP。IsEmpty(System.String)(可能是因为类型替换不完整)。

我的问题是:允许我在静态方法中访问state的解决方法是什么?

创建DataSet持有者类的static instance

 class classA {
  //...
  public DataSet state;
  //...
 }

你可以有:

static ClassA a=new ClassA():
[Rule]
static void create(int param) {
   //...
   a.state=//do something with it;
   //...
}

最好的方法是传入state作为附加参数:

[Rule]
static void create(State state, int param) {
   // ACCESS STATE
}

并从您有权访问实例成员的方法中调用它。

State s = new State();
State.create(s, 0);

或者从State实例内部:

create(this, 0);

问题似乎在于DataSet需要在探索过程中调用非托管函数。建议在表示状态时仅使用Microsoft.Modeling中的类型。

最新更新