开发者

How to get property.value from reflection.assembly?

How to get property.value from reflection.assembly?

Dim assembly As Assembly = assembly.GetExecutingAssembly()

For Each assemblyType As Type In assembly.GetTypes()
        If assemblyType.IsSubclassOf(GetType(Form)) Then
            'Dim name As AssemblyName() = assembly.GetReferencedAssemblies()


            If assemblyType.BaseType.ToString.EndsWith("Form2") Then
                Dim props A开发者_运维知识库s PropertyInfo = _
                GetType(Form2).GetProperty("FriendlyName")


                If Not props Is Nothing Then
                    ComboBox1.Items.Add(assemblyType.Namespace )

                End If
'//Here I want to get Prop.value that is string type



End If


To get the value of a property via reflection, call PropertyInfo.GetValue.

However, assuming that this is an instance property, you will need an instance of the type for which the get the property value. For example, if FriendlyName is an instance property of the Form2 class, you will need to specify which instance of Form2 you want to get the FriendlyName for. (And you'll pass this as the obj argument to PropertyInfo.GetValue; you can pass null for the index argument in this case.)


Building on what Itowlson wrote:

From MCTS: .net 2.0: Application Development Foundation

First get the assembly:

Dim path As String = "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\" + _ "mscorlib.dll"

Dim theAssembly As Assembly = Assembly.LoadFile(path) Dim hashType As Type = theAssembly.GetType("System.Collections.Hashtable")

Once you have the type, you can ask it for a ConstructorInfo object to use to construct your new type:

Dim argumentTypes() As Type = Type.EmptyTypes ' Empty Constructor Dim ctor As ConstructorInfo = hashType.GetConstructor(argumentTypes)

The method represented in a ConstructorInfo object is a specialized MethodBase object that looks and acts like a typical method but always returns an instance of a specific type. In this example, you are asking the Type class to return an empty constructor. (You are supplying an empty Array of Types to specify the empty constructor.) You could also ask for a constructor with specific arguments by supplying an array of the constructor argument types, like so:

Dim argumentTypes() As Type = _ New Type() {GetType(System.Int32)} ' One argument of type Int32 Dim ctor As ConstructorInfo = hashType.GetConstructor(argumentTypes)

Once you have the ConstructorInfo object, creating an object is as simple as invoking the constructor. Here is how to invoke the empty constructor:

Dim newHash as Object = ctor.Invoke(New Object() {})

Once you have an instance of an object, you simply use reflection to get the info class you need to call, and then you invoke the info class to execute the code.

For example, call the Add method on your new Hashtable instance:

Dim meth As MethodInfo = hashType.GetMethod("Add")

meth.Invoke(newHash, New Object() {"Hi", "Hello"})

You can now use the PropertyInfo class to get the count of the items in your Hashtable to verify that the Add worked as you expected it to:

Dim prop As PropertyInfo = hashType.GetProperty("Count")

Dim count As Integer = CType(prop.GetValue(newHash, Nothing),Integer)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜