My aim is to enumerate DLLs in a folder, look for classes that conform to a specific interface and then query those classes for enum values. The idea being that I want to query what 'Outcome' possibilities are supported.
I have my DLLs lined up and can see the classes which conform to the correct interface, that is working, but the next step is to search the class for a specific enum and get the values, I have been using the following code:
Private Sub GetOutcomeEnumValues(ByVal AssemblyPath As String, ByVal ClassName As String)
Dim ReflectedAssembly As Assembly
Dim ReflectedClass As Type
'Load DLL
ReflectedAssembly = System.Reflection.Assembly.LoadFrom(AssemblyPath)
'Load Class
ReflectedClass = ReflectedAssembly.GetType(ClassName)
'Load members
Dim Members() As MemberInfo
Members = ReflectedClass.开发者_开发问答GetMembers
For i As Integer = 0 To Members.Count - 1
'Check for the Outcomes enumeration
If (Members(i).Name = "Outcomes") Then
Dim Outcomes As System.Array
Outcomes = Members(i).GetType.GetEnumValues
End If
Next
End Sub
The problem I have is that I cannot seem to get Members(i) to give me the enum values - even though I KNOW this Member is an enumeration. When I call 'GetEnumValues' an exception is thrown:
"Type provided must be an Enum. Parameter name: enumType"
The class I am querying looks something like this
Public Class Foo
Public Enum Outcomes
OK
Cancel
End Enum
End Class
Got it, after experimenting a little, I used the following code to good effect:
Private Sub GetOutcomeEnumValues(ByVal AssemblyPath As String, ByVal ClassName As String)
Dim ReflectedAssembly As Assembly
Dim ReflectedClass As Type
'Load DLL
ReflectedAssembly = System.Reflection.Assembly.LoadFrom(AssemblyPath)
'Load Class
ReflectedClass = ReflectedAssembly.GetType(ClassName & "+ Outcomes")
'Load members
Dim OutcomeNames() As String = ReflectedClass.GetEnumNames
End Sub
精彩评论