[英]Iterate over Methods defined in a Module
I have recently found out about VBIDE library, but I don't quite grap all of it's capabilities yet. 我最近发现了VBIDE库,但我还没有完全掌握它的所有功能。 I want to make a method that will instantiate correct Class based on classes found in my Project.
我想创建一个方法,根据我的Project中找到的类来实例化正确的Class。 Classes that fit as candidates to be instantiated
Implement
InterfaceA
, and the exact class that I want to instantiate has property MType
and it returns Enumerated Value
Correct
. 适合作为实例化候选者的类
Implement
InterfaceA
,我想要实例化的确切类具有属性MType
,并返回Enumerated Value
Correct
。
So to sumarize how do I iterate over classes defined in my project in order to find Class that returns Correct
for Property MType
, and Instantiate that Class. 因此,为了找到我的项目中定义的类,以便找到返回
Correct
for Property MType
Class,并实例化该Class,我要总结一下。
Thus far I know that I can Iterate over my modules with following code: 到目前为止,我知道我可以使用以下代码迭代我的模块:
Dim Part As VBComponent
For Each Part In Application.VBE.ActiveVBProject.VBComponents
Debug.Print Part.Name
Next Part
What I am missing now how do I iterate over methods/properties of each class to find out what these mehods return? 我现在缺少的是如何迭代每个类的方法/属性以找出这些方法返回的内容?
Here is method I need to find, it varies from class to class by value it returns: 这是我需要找到的方法,它因类返回而不同:
Public Property Get InterfaceA_MType() As Model_Types
IModel_MType = Severity
End Property
So as you can see this Property is pretty simple I am to assume that it will return same value all the time. 所以你可以看到这个属性非常简单我假设它将一直返回相同的值。
UPDATE: Per Dough Gancy's observation part of the answer is located in here I can use ProcBodyLine(InterfaceA_MType)
and ProcCountLines(InterfaceA_MType)
to iterate over the procedure lines, match those lines witch one that has IModel_MType = Correct
. 更新:Per Dough Gancy的观察部分答案位于此处我可以使用
ProcBodyLine(InterfaceA_MType)
和ProcCountLines(InterfaceA_MType)
迭代过程行,匹配那些具有IModel_MType = Correct
。
This leaves out only instantiating the Class based on the Code Module. 这样就不会基于代码模块实例化类。 how do I do that?
我怎么做?
First off, this doesn't iterate over your classes, it iterates over all modules in your file. 首先,这不会迭代您的类,它会迭代文件中的所有模块。
Dim Part As VBComponent For Each Part In Application.VBE.ActiveVBProject.VBComponents Debug.Print Part.Name Next Part
If you want to iterate over just the class modules, you'll need to check the component type. 如果要迭代只有类模块,则需要检查组件类型。
Dim Part As VBComponent
For Each Part In Application.VBE.ActiveVBProject.VBComponents
If Part.Type = vbext_ct_ClassModule Then
Debug.Print Part.Name
End If
End If
Now, to find any particular method in a code module, you'll need to use the Find method of the CodeModule object. 现在,要在代码模块中查找任何特定方法,您需要使用CodeModule对象的Find方法 。
Of special note, is the fact that startline
, endline
, startcol
, and endcol
are all passed by reference and are effectively "out" parameters . 需要特别注意的,是一个事实,即
startline
, endline
, startcol
和endcol
都按引用传递,并有效地“走出去”的参数 。
So, that code would look something like this. 所以,代码看起来像这样。
Dim startLine As Long
Dim endLine As Long
Dim startCol As Long
Dim endCol As Long
startLine = 1
startCol = 1
endLine = -1
endCol = -1
If Part.Find("Public Property Get InterfaceA_MType()", startLine, startCol, endLine, endCol) Then
' do something
End If
Finally, in order to create an instance of a class, it's going to necessarily be a global instance... which kind of smells, but whatever. 最后,为了创建一个类的实例,它必然是一个全局实例......哪种气味,但无论如何。 You've not really given us your end goal.
你没有真正给我们你的最终目标。 I have a feeling that your problem is better solved via some good OOP programming, but that's not the question you asked.
我有一种感觉,通过一些优秀的OOP编程可以更好地解决您的问题,但这不是您提出的问题。
To get an instance of the class to work with, you'll need to dynamically create a module and procedure that instantiates an instance of that class. 要获取要使用的类的实例,您需要动态创建一个实例化该类实例的模块和过程 。 Perhaps even re-writes the calling code on the fly.
甚至可能即时重写调用代码。
So, in this dynamically generated module, you'll need to write something like this. 因此,在这个动态生成的模块中,您需要编写类似这样的内容。
Public dynamic As ClassType
Public Sub InitalizeDynamic()
Set dynamic = new ClassType
End Sub
Then, somewhere else, you would call the plain jane module sub with Application.Run
. 然后,在其他地方,您将使用
Application.Run
调用plain jane模块子 。
Public Sub Test1()
Application.Run "VBAProject.Module1.IntializeDynamic"
End Sub
So, that finally you could use the instance of dynamic. 所以,最后你可以使用动态的实例。
foo = dynamic.InterfaceA_MType()
If you carefully read the questions I linked to with other answers of mine, you should be able to work out a solution. 如果你仔细阅读我链接的问题和我的其他答案,你应该能够找到解决方案。 Everything you need to know is in the combination of those two answers.
您需要知道的一切都是这两个答案的组合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.