如何从reflection.assembly获取property.value? [英] How to get property.value from reflection.assembly?

查看:54
本文介绍了如何从reflection.assembly获取property.value?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从reflection.assembly中获取property.value?

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 As 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

推荐答案

以 Itowlson 所写的内容为基础:

Building on what Itowlson wrote:

来自 MCTS:.net 2.0:应用程序开发基金会

首先得到程序集:

将路径变暗为字符串 ="C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\"+ _ "mscorlib.dll"

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

将组件调暗为组件 =Assembly.LoadFile(path) Dim hashType作为类型 =theAssembly.GetType("System.Collections.Hashtable")

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

一旦你有了类型,你就可以要求它提供一个 ConstructorInfo 对象来构造您的新类型:

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 的空构造函数Dim ctor As ConstructorInfo =hashType.GetConstructor(argumentTypes)

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

ConstructorInfo 对象中表示的方法是一个专门的 MethodBase 对象看起来和行为都像一个典型的方法,但总是返回一个特定的实例类型.在此示例中,您要求 Type 类返回一个空构造函数.(您正在提供一个空的类型数组来指定空的构造函数.)您也可以通过提供一个数组来请求一个带有特定参数的构造函数构造函数参数类型,如下所示:

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 = _ NewType() {GetType(System.Int32)} ' 一Int32 Dim ctor As 类型的参数构造函数信息 =hashType.GetConstructor(argumentTypes)

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

一旦你有了 ConstructorInfo 对象,创建一个对象就像调用一样简单构造函数.以下是调用空构造函数的方法:

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

将 newHash 淡化为对象 =ctor.Invoke(New Object() {})

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

一旦你有了一个对象的实例,你只需使用反射来获取信息类需要调用,然后调用info类来执行代码.

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.

例如,在新的 Hashtable 实例上调用 Add 方法:

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

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

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

meth.Invoke(newHash, New Object(){"你好", "你好"})

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

您现在可以使用 PropertyInfo 类来获取您的哈希表以验证 Add 是否按预期工作:

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:

将道具调暗为 PropertyInfo =hashType.GetProperty("Count")

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

暗计数为整数 =CType(prop.GetValue(newHash,无),整数)

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

这篇关于如何从reflection.assembly获取property.value?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆