本文介绍了如何动态获取模块的值并将其分配给调用的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在编写一个小程序,试图通过所谓的反射动态地从类中调用方法.

I'm making a little program in where i'm trying to invoke a method from a class dynamically with so called Reflection.

我要调用的类称为ContactList,我尝试调用该类中称为count的属性.程序集本身称为Contact.Exe

The class I'm trying to call is called ContactList and i try to invoke the property in this class called count. The assembly itself is called Contact.Exe

现在,我具有以下代码,可以在Contact.exe程序集中创建ContactList类的实例:

Now I have the following code where I make an instance of the ContactList class in the Contact.exe assembly:

 Public
 Function
 InvokeContacts() As
 Integer

  Dim
 ContactsAssembly As
 Assembly
 = Assembly
.LoadFrom("Contacts.exe"
)
  Dim
 Mycontactlist As
 Type = ContactsAssembly.GetType
("WindowsApplication1.ContactList"
)
  Dim
 argumentTypes() As
 Type = Type.EmptyTypes
  Dim
 MycontaclistConstructor As
 ConstructorInfo = Mycontactlist.GetConstructor(argumentTypes)
  Dim
 ContactList As
 Object
 = Activator.CreateInstance(Mycontactlist)

但是现在我想将此实例分配给Contacts.exe程序集中的模块中的值.该模块称为module1.这是因为如果我调用此类ContactList的对象,则ContactList再次为空,我需要使用它 当前值.因此,在module1中是一个公共值,我需要将其分配给空的ContactList对象.此值称为:

But Now I want to assign this instance to a value in a module in the Contacts.exe assembly. This module is called module1.  This is because if I invoke an object of this class of ContactList, the ContactList is empty again and I need to have it the current value. So in the module1 is a public value which i need to assign to the empty ContactList object. This value is called:

 将mycontacts公开为ContactList(

  Public mycontacts As ContactList(

此值是在Contacts.exe程序集中名为Module1的模块中分配的)

This value is assigned in a module called Module1 in the Contacts.exe assembly)

现在,我想将此值分配给ContactList类型的对象.所以我扩展了代码,如下所示:

Now I want to assign this value to the object of type ContactList. So I expanded the code as seen below:

 


Dim ContactsAssembly As Assembly = Assembly.LoadFrom("Contacts.exe")
    Dim mymodule As [Module] = ContactsAssembly.GetModule("_202L5.Module1")

    Dim Mycontactlist As Type = ContactsAssembly.GetType("_202L5.ContactList")
    Dim argumentTypes() As Type = Type.EmptyTypes
    Dim MycontaclistConstructor As ConstructorInfo = Mycontactlist.GetConstructor(argumentTypes)

    Dim ContactList As Object = Activator.CreateInstance(Mycontactlist)

    Dim myfield As FieldInfo = mymodule.GetField("mycontacts")

    ContactList = myfield.GetValue(myfield)

    Dim count As PropertyInfo = ContactList.GetProperty("Count")
    Dim CountCL As Integer = count.GetValue(ContactList, Nothing)
    Return CountCL

推荐答案


这篇关于如何动态获取模块的值并将其分配给调用的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 10:38