1

因此,有两个具有各种类和方法的 .NET 类库。

现在有一组静态操作。例如:

// Create and connect to device objects
classLibrary1.device objectDevice1 = new classLibrary1.device();
classLibrary2.device objectDevice2 = new classLibrary2.device();

// First Operation
objectDevice1.setSetting1(uiInput1.Text);
objectDevice1.doOperation1();

// Second Operation
// Create parameter object
classLibrary2.operationParam parameters = new classLibrary2.operationParam();
parameters.setParam1(uiInput2.Text);
parameters.setParam2(uiInput3.Text);
// Do operation with parameters
objectDevice2.doOperation1(parameters);

所以用户可以在界面中输入某些参数,但操作是静态的。

目标是让用户使用以下操作构建自定义事件:

Set property on device1
Call method 'doOperation1' on device1
Create operationParameter 'parameter'
Set 'setParam1' on 'parameter' as '100'
Set 'setParam2' on 'parameter' as '200'
Call method 'doOperation1' on device2 with 'parameter'
Create operationParameter 'parameter2'
Set 'setParam1' on 'parameter2' as '500'
Set 'setParam2' on 'parameter2' as '600'
Call method 'doOperation1' on device2 with 'parameter2'
Call method 'doOperation2' on device1

用户的可能选项是:

  1. 运行一个向导,用户将选择事件的类型,设置参数,然后它将通过适当的操作构建事件。
  2. 手动添加每个操作构建事件。
  3. 首先使用向导构建,然后编辑事件以在生成的事件之间添加操作。

该界面将包括一个用于访问方法/属性的下拉列表,当用户从下拉列表中选择时,输入字段用于所需参数。我不知道是否有可能构建或实现一个解释器,以允许用户“编写”这样的事件。

大约有 40 种不同的方法,每种方法都有不同的属性或需要单独的参数对象。

构建此代码的最佳方法是什么?我已经能够通过创建一个包含“操作”对象列表的“事件”对象来反射设置属性。数据作为方法/属性名称和参数从 SQLite DB 中提取。由于它作为字符串在数据库中,因此我没有使用多个参数测试 Convert.ChangeType,因此方法目前无法正常工作。

        MethodInfo method = this.objectType.GetMethod(this.methodName);
        if (method != null)
        {
            object result = null;
            ParameterInfo[] paramInfo = method.GetParameters();
            if (paramInfo.Length == 0)
            {
                result = method.Invoke(instance, null);
            }
            else
            {
                result = method.Invoke(instance, parameters);
            }
        }
        else
        {                
            PropertyInfo pi = objectType.GetProperty(methodName);
            if (pi != null)
            {
                pi.SetValue(instance, Convert.ChangeType(parameters[0],pi.PropertyType), null);
            }
        }

我更关心动态创建参数对象,并能够在方法中引用它们。IE:

// Create parameter object
classLibrary2.operationParam parameters = new classLibrary2.operationParam();
parameters.setParam1(uiInput2.Text);
parameters.setParam2(uiInput3.Text);
// Do operation with parameters
objectDevice2.doOperation1(parameters); 

有没有办法利用 .NET 库的公开方法,或者是否有必要为所有方法编写一个包装器。

我还没有找到类似的东西。任何帮助将不胜感激。

4

0 回答 0