简体   繁体   English

从 XML 中读取的可配置方法在调用时用于 VB.NET

[英]Configurable method read from XML used in VB.NET on calling

I have a question regarding how can i use a method name configurable to be called.我有一个关于如何使用可配置的方法名称的问题。 Eg例如

1.I have a xml file with three elements looking like this: 1.我有一个 xml 文件,其中包含如下所示的三个元素:

<?xml version="1.0" encoding="iso-8859-1"?>
<root>
    <element attribute1="a" attribute2="b" attribute3="Send" />
</root>

2.I am using a List (i made a class with three elements and create an object type list of that class) to store the elements from the xml file. 2.我正在使用一个列表(我制作了一个包含三个元素的 class 并创建了一个该类的 object 类型列表)来存储 xml 文件中的元素。

3.Then using a For statement: For Each element As ClassList In GetList i want to call the configurable method from the xml file instead: 3.然后使用 For 语句:对于在 GetList 中作为 ClassList 的每个元素,我想改为从 xml 文件调用可配置方法:

Theoratically instead of SendWait i want to have the value of attribute3(the value is SendWait) to be called.理论上而不是 SendWait 我想调用 attribute3 的值(值为 SendWait)。

instead of SendKeys.SendWait("{ENTER}") something like this:而不是 SendKeys.SendWait("{ENTER}") 这样的东西:

SendKeys.element.Thirdelement()("{ENTER}")
  • the value of element.Thirdelement() is SendWait element.Thirdelement() 的值为 SendWait

I know that attribute3 could have 2 values: Send or Sendwait Should i use a if statement, or is there any solution available?我知道 attribute3 可能有 2 个值:Send 或 Sendwait 我应该使用 if 语句,还是有可用的解决方案?

If element.ThirdElement() = "SendWait" Then
                                SendKeys.SendWait("{ENTER}")
                            Else
                                SendKeys.Send("{ENTER}")

I am new in programming so please excuse me if one of the statement above is an aberration!我是编程新手,所以如果上面的陈述有误,请原谅!

LE: I have a new class ListClass1 with three members and properties: LE:我有一个新的 class ListClass1,它包含三个成员和属性:

firstElement()
secondElement()
thirdElement()

I am using this class in order to store the data from xml file我正在使用这个 class 来存储 xml 文件中的数据

<?xml version="1.0" encoding="iso-8859-1"?>
<root>
<element attribute1="a" attribute2="b" attribute3="Send" />
</root>

So after i add the values to the list, for example: element.ThirdElement() will have the value from attribute3 ("Send").因此,在我将值添加到列表后,例如: element.ThirdElement()将具有来自 attribute3(“发送”)的值。 Also element.secondElement() will retain the value from attribute2 and so on. element.secondElement()也将保留 attribute2 的值,依此类推。

You can use reflection to do the task.您可以使用反射来完成任务。 First Define a class holding your methods:首先定义一个 class 持有你的方法:

Public Class MethodsToExecute
    Public Sub Send(par1)
       SendKeys.Send("{ENTER}") 
    End Sub
    Public Sub SendWait(par1)
       SendKeys.SendWait("{ENTER}")
    End Sub
End Class

Then create a Sub for handling:然后创建一个 Sub 来处理:

Public Shared Sub ExecuteMethodByName(ObjectToInvoke As Object, MethodName As String, ParamArray Parameters() As Object) 
     Dim m As Reflection.MethodInfo = ObjectToInvoke.GetType.GetMethod(MethodName)
     m.Invoke(ObjectToInvoke, Parameters)
End Sub

and now execution现在执行

dim mte as new MethodsToExecute

iterate thorough xml file and do遍历 xml 文件并执行

 ExecuteMethodByName(mte,element.ThirdElement(), element.firsElement(),element.secondElement())

The problem is that solution is dependent on attribute order not by their names but with more extensive reflection usage you can handle it too.问题是解决方案依赖于属性顺序而不是它们的名称,但是通过更广泛的反射使用你也可以处理它。 ParamArray Example 参数数组示例

Is it understandable?可以理解吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM