Ik heb een klein vraagje over reflectie in VB.net. Aangezien ik de vraag elders op een Engels forum ook al gesteld heb heb ik het even in de Engels gelaten. Ik hoop dat dit geen probleem is.
I'm making a little program in where i'm trying to invoke a method from a class dynamically with so called Reflection.
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
Now I have the following code where I make an instance of the ContactList class using reflection:
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:
(This value is assigned in a module called Module1 in the Contacts.exe assembly)
Now I want to assign this value to the object of type ContactList. So I expanded the code as seen below:
But now I get a Nullexception in this line of code:
So is it possible anyway to assign a public value in a module to a dynamically created object like the way I tried to do it? :
I'm making a little program in where i'm trying to invoke a method from a class dynamically with so called Reflection.
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
Now I have the following code where I make an instance of the ContactList class using reflection:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| Public Function InvokeContacts() As Integer Dim ContactsAssembly As Assembly = Assembly .LoadFrom("Contacts.exe" ) Dim Mycontactlist As Type = ContactsAssembly.GetType ("_windowsapplication.ContactList" ) Dim argumentTypes() As Type = Type.EmptyTypes Dim MycontaclistConstructor As ConstructorInfo = Mycontactlist.GetConstructor(argumentTypes) Dim ContactList As Object = Activator.CreateInstance(Mycontactlist) |
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:
code:
1
| Public mycontacts As ContactList |
(This value is assigned in a module called Module1 in the Contacts.exe assembly)
Now I want to assign this value to the object of type ContactList. So I expanded the code as seen below:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| Dim ContactsAssembly As Assembly = Assembly.LoadFrom("Contacts.exe") Dim mymodule As [Module] = ContactsAssembly.GetModule("_windowsapplication.Module1") Dim Mycontactlist As Type = ContactsAssembly.GetType("_windowsapplication.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 |
But now I get a Nullexception in this line of code:
code:
1
| Dim myfield As FieldInfo = mymodule.GetField("mycontacts") |
So is it possible anyway to assign a public value in a module to a dynamically created object like the way I tried to do it? :