473,769 Members | 4,846 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating an object dynamically using Reflection and running in a seperate thread

I would normally use code such as :

Dim Customer as new Customer
Dim t as new threading.threa d(AddressOf Customer.Displa yCustomer)
Customer.Custom erId=MyCustomer Id
t.start

Which would create a new thread to display a customer on the screen for example.

However, I have a problem with circular references in my objects which means that I have to load the customer object using reflection ie :

Dim Customer As Object

Dim dmsobj As New DmsObjects.Func tions

Customer = dmsobj.LoadMeBy Name("DMSCustom er.dll", "DMSCustomer.Cu stomer", Nothing) <--- this is a wrapper round some reflection code to load up the object

Customer.Custom er = dms.data("Activ ityLog", 5, RowNo)

customer.displa ycustomer

This code works.

However, I want to do :

Dim Customer As Object

Dim dmsobj As New DmsObjects.Func tions

Customer = dmsobj.LoadMeBy Name("DMSCustom er.dll", "DMSCustomer.Cu stomer", Nothing)

Customer.Custom er = dms.data("Activ ityLog", 5, RowNo)

Dim t As New Threading.Threa d(AddressOf Customer.Displa yCustomer)

t.Start()

This doesn't compile, due to latebinding, it complains that "Customer.Displ ayCustomer" is not a valid method.

How can I acheive this??

Many thanks in advance.

Simon

Nov 20 '05 #1
6 1919
Activator.Creat eInstance
"Simon Verona" <ne**@aphrodite uk.com> wrote in message news:uQ******** ******@TK2MSFTN GP09.phx.gbl...
I would normally use code such as :

Dim Customer as new Customer
Dim t as new threading.threa d(AddressOf Customer.Displa yCustomer)
Customer.Custom erId=MyCustomer Id
t.start

Which would create a new thread to display a customer on the screen for example.

However, I have a problem with circular references in my objects which means that I have to load the customer object using reflection ie :

Dim Customer As Object

Dim dmsobj As New DmsObjects.Func tions

Customer = dmsobj.LoadMeBy Name("DMSCustom er.dll", "DMSCustomer.Cu stomer", Nothing) <--- this is a wrapper round some reflection code to load up the object

Customer.Custom er = dms.data("Activ ityLog", 5, RowNo)

customer.displa ycustomer

This code works.

However, I want to do :

Dim Customer As Object

Dim dmsobj As New DmsObjects.Func tions

Customer = dmsobj.LoadMeBy Name("DMSCustom er.dll", "DMSCustomer.Cu stomer", Nothing)

Customer.Custom er = dms.data("Activ ityLog", 5, RowNo)

Dim t As New Threading.Threa d(AddressOf Customer.Displa yCustomer)

t.Start()

This doesn't compile, due to latebinding, it complains that "Customer.Displ ayCustomer" is not a valid method.

How can I acheive this??

Many thanks in advance.

Simon

Nov 20 '05 #2
Thanks for the pointer.. Unfortunately, I can't quite work out the syntax for using this.

Is it possible to put together an example of use ? Perhaps using my code as the example????

Thanks in advance.

Simon
"CJ Taylor" <no****@blowgoa ts.com> wrote in message news:vs******** ****@corp.super news.com...
Activator.Creat eInstance
"Simon Verona" <ne**@aphrodite uk.com> wrote in message news:uQ******** ******@TK2MSFTN GP09.phx.gbl...
I would normally use code such as :

Dim Customer as new Customer
Dim t as new threading.threa d(AddressOf Customer.Displa yCustomer)
Customer.Custom erId=MyCustomer Id
t.start

Which would create a new thread to display a customer on the screen for example.

However, I have a problem with circular references in my objects which means that I have to load the customer object using reflection ie :

Dim Customer As Object

Dim dmsobj As New DmsObjects.Func tions

Customer = dmsobj.LoadMeBy Name("DMSCustom er.dll", "DMSCustomer.Cu stomer", Nothing) <--- this is a wrapper round some reflection code to load up the object

Customer.Custom er = dms.data("Activ ityLog", 5, RowNo)

customer.displa ycustomer

This code works.

However, I want to do :

Dim Customer As Object

Dim dmsobj As New DmsObjects.Func tions

Customer = dmsobj.LoadMeBy Name("DMSCustom er.dll", "DMSCustomer.Cu stomer", Nothing)

Customer.Custom er = dms.data("Activ ityLog", 5, RowNo)

Dim t As New Threading.Threa d(AddressOf Customer.Displa yCustomer)

t.Start()

This doesn't compile, due to latebinding, it complains that "Customer.Displ ayCustomer" is not a valid method.

How can I acheive this??

Many thanks in advance.

Simon

Nov 20 '05 #3
* "Simon Verona" <ne**@aphrodite uk.com> scripsit:
Thanks for the pointer.. Unfortunately, I can't quite work out the syntax for using this.


For example (there are many other ways to use 'CreateInstance '):

\\\
Private Function CreateClassByNa me( _
ByVal PartialAssembly Name As String, _
ByVal QualifiedClassN ame As String _
) As Object
Return _
Activator.Creat eInstance( _
[Assembly].LoadWithPartia lName( _
PartialAssembly Name _
).GetType(Quali fiedClassName) _
)
End Function
///

Usage:

\\\
Dim c As Control = _
DirectCast( _
CreateClassByNa me( _
"System.Windows .Forms", _
"System.Windows .Forms.Button" _
), _
Control _
)
With c
.Location = New Point(10, 10)
.Size = New Size(80, 26)
.Text = "Hello World"
End With
Me.Controls.Add (c)
///

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #4
I'm getting very confused.

I think the code that you've put in is the code to construct the object in the first place - I have very similar code that does this.

I can't work out how to write the code that is used when creating the thread:

ie in my code:

Dim Customer As Object
Dim dmsobj As New DmsObjects.Func tions
Customer = dmsobj.LoadMeBy Name("DMSCustom er.dll", "DMSCustomer.Cu stomer", Nothing)
Customer.Custom er = dms.data("Activ ityLog", 5, RowNo)
Dim t As New Threading.Threa d(AddressOf Customer.Displa yCustomer)
t.Start()

I need to know what to put in place of the AddressOf Customer.Displa yCustomer when creating the thread. I'm sure that the reflection class allows you to create a delegate pointer to a specific method, but I can't work out how! It's getting very frustrating.

Out of interest, the function dmsobj.LoadMeBy Name is as follows:

Public Shared Function LoadMeByName(By Val vstrAssemblyNam e As String, ByVal vstrClassName As String, ByVal vArgs() As Object) As Object
'************** *************** *************** *************** *************** **********
'dynamically load object from assembly for late binding purpose
' *************** *************** *************** *************** *************** **********
Dim objAssembly As Reflection.Asse mbly
Dim objtemp As Object
Dim Params() As Object
'MsgBox(Applica tion.StartupPat h)
If Dir(Application .StartupPath & "\" & vstrAssemblyNam e).Equals("") Then
Err.Raise(-1, "LoadMeByNa me", "assembly could not found, wrong assembly name given ")
Exit Function
End If
objAssembly = objAssembly.Loa dFrom(Applicati on.StartupPath & "\" & vstrAssemblyNam e)
objtemp = objAssembly.Cre ateInstance(vst rClassName, True, Reflection.Bind ingFlags.Create Instance, Nothing, vArgs, Nothing, Nothing)
If objtemp Is Nothing Then
Err.Raise(-1, "LoadMeByNa me", "assembly could not found, wrong assembly name given ")
Exit Function
End If
' LoadMeByName = objtemp
Return objtemp
End Function

Hopefully, somebody will take pity on me on a Friday!!

Many thanks in advance,
Simon
"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message news:bq******** *****@ID-208219.news.uni-berlin.de...
* "Simon Verona" <ne**@aphrodite uk.com> scripsit:
Thanks for the pointer.. Unfortunately, I can't quite work out the syntax for using this.


For example (there are many other ways to use 'CreateInstance '):

\\\
Private Function CreateClassByNa me( _
ByVal PartialAssembly Name As String, _
ByVal QualifiedClassN ame As String _
) As Object
Return _
Activator.Creat eInstance( _
[Assembly].LoadWithPartia lName( _
PartialAssembly Name _
).GetType(Quali fiedClassName) _
)
End Function
///

Usage:

\\\
Dim c As Control = _
DirectCast( _
CreateClassByNa me( _
"System.Windows .Forms", _
"System.Windows .Forms.Button" _
), _
Control _
)
With c
.Location = New Point(10, 10)
.Size = New Size(80, 26)
.Text = "Hello World"
End With
Me.Controls.Add (c)
///

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>

Nov 20 '05 #5
Ahh

You can create a delegate, however, I wouldn't free thread it using AddressOf.

What does your threading code look like? Do you have it yet?

i.e.

Dim myThread as new System.Threadin g.Thread(Addres sOf myFunctionCall)

Now, you can't pass parameters to Threading start calls...

So,if your myFunctionCall is in another class, you have to instantiate it and set the proper "parameter" values... i.e using properties and then the myFunctionCall (which actually starts the execution on a new thread)

such that

Dim myObject as New myObject

myObject.Parame ter1 = [a paramter]
myObject.Parame ter2 = [another parameter]

Dim myThread as System.Threadin g.Thread

myThread = new System.Threadin g.Thread (AddressOf myObject.MyThre adStarterFuncti on)

myThread.Start( )

' do some stuff - new thread executes its stuff, you do whatever else you want... Join
'just shuts the thread down (read about thread states).

myThread.join()
"Simon Verona" <ne**@aphrodite uk.com> wrote in message news:ux******** ******@tk2msftn gp13.phx.gbl...
I'm getting very confused.

I think the code that you've put in is the code to construct the object in the first place - I have very similar code that does this.

I can't work out how to write the code that is used when creating the thread:

ie in my code:

Dim Customer As Object
Dim dmsobj As New DmsObjects.Func tions
Customer = dmsobj.LoadMeBy Name("DMSCustom er.dll", "DMSCustomer.Cu stomer", Nothing)
Customer.Custom er = dms.data("Activ ityLog", 5, RowNo)
Dim t As New Threading.Threa d(AddressOf Customer.Displa yCustomer)
t.Start()

I need to know what to put in place of the AddressOf Customer.Displa yCustomer when creating the thread. I'm sure that the reflection class allows you to create a delegate pointer to a specific method, but I can't work out how! It's getting very frustrating.

Out of interest, the function dmsobj.LoadMeBy Name is as follows:

Public Shared Function LoadMeByName(By Val vstrAssemblyNam e As String, ByVal vstrClassName As String, ByVal vArgs() As Object) As Object
'************** *************** *************** *************** *************** **********
'dynamically load object from assembly for late binding purpose
' *************** *************** *************** *************** *************** **********
Dim objAssembly As Reflection.Asse mbly
Dim objtemp As Object
Dim Params() As Object
'MsgBox(Applica tion.StartupPat h)
If Dir(Application .StartupPath & "\" & vstrAssemblyNam e).Equals("") Then
Err.Raise(-1, "LoadMeByNa me", "assembly could not found, wrong assembly name given ")
Exit Function
End If
objAssembly = objAssembly.Loa dFrom(Applicati on.StartupPath & "\" & vstrAssemblyNam e)
objtemp = objAssembly.Cre ateInstance(vst rClassName, True, Reflection.Bind ingFlags.Create Instance, Nothing, vArgs, Nothing, Nothing)
If objtemp Is Nothing Then
Err.Raise(-1, "LoadMeByNa me", "assembly could not found, wrong assembly name given ")
Exit Function
End If
' LoadMeByName = objtemp
Return objtemp
End Function

Hopefully, somebody will take pity on me on a Friday!!

Many thanks in advance,
Simon
"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message news:bq******** *****@ID-208219.news.uni-berlin.de...
* "Simon Verona" <ne**@aphrodite uk.com> scripsit:
Thanks for the pointer.. Unfortunately, I can't quite work out the syntax for using this.


For example (there are many other ways to use 'CreateInstance '):

\\\
Private Function CreateClassByNa me( _
ByVal PartialAssembly Name As String, _
ByVal QualifiedClassN ame As String _
) As Object
Return _
Activator.Creat eInstance( _
[Assembly].LoadWithPartia lName( _
PartialAssembly Name _
).GetType(Quali fiedClassName) _
)
End Function
///

Usage:

\\\
Dim c As Control = _
DirectCast( _
CreateClassByNa me( _
"System.Windows .Forms", _
"System.Windows .Forms.Button" _
), _
Control _
)
With c
.Location = New Point(10, 10)
.Size = New Size(80, 26)
.Text = "Hello World"
End With
Me.Controls.Add (c)
///

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>

Nov 20 '05 #6
Ahhh

my bad... sorry had to re read it again...

help://MS.VSCC.2003/MS.MSDNQTR.2003 FEB.1033

has instructions on how to create a delegate... I hope this helps.
"Simon Verona" <ne**@aphrodite uk.com> wrote in message news:ux******** ******@tk2msftn gp13.phx.gbl...
I'm getting very confused.

I think the code that you've put in is the code to construct the object in the first place - I have very similar code that does this.

I can't work out how to write the code that is used when creating the thread:

ie in my code:

Dim Customer As Object
Dim dmsobj As New DmsObjects.Func tions
Customer = dmsobj.LoadMeBy Name("DMSCustom er.dll", "DMSCustomer.Cu stomer", Nothing)
Customer.Custom er = dms.data("Activ ityLog", 5, RowNo)
Dim t As New Threading.Threa d(AddressOf Customer.Displa yCustomer)
t.Start()

I need to know what to put in place of the AddressOf Customer.Displa yCustomer when creating the thread. I'm sure that the reflection class allows you to create a delegate pointer to a specific method, but I can't work out how! It's getting very frustrating.

Out of interest, the function dmsobj.LoadMeBy Name is as follows:

Public Shared Function LoadMeByName(By Val vstrAssemblyNam e As String, ByVal vstrClassName As String, ByVal vArgs() As Object) As Object
'************** *************** *************** *************** *************** **********
'dynamically load object from assembly for late binding purpose
' *************** *************** *************** *************** *************** **********
Dim objAssembly As Reflection.Asse mbly
Dim objtemp As Object
Dim Params() As Object
'MsgBox(Applica tion.StartupPat h)
If Dir(Application .StartupPath & "\" & vstrAssemblyNam e).Equals("") Then
Err.Raise(-1, "LoadMeByNa me", "assembly could not found, wrong assembly name given ")
Exit Function
End If
objAssembly = objAssembly.Loa dFrom(Applicati on.StartupPath & "\" & vstrAssemblyNam e)
objtemp = objAssembly.Cre ateInstance(vst rClassName, True, Reflection.Bind ingFlags.Create Instance, Nothing, vArgs, Nothing, Nothing)
If objtemp Is Nothing Then
Err.Raise(-1, "LoadMeByNa me", "assembly could not found, wrong assembly name given ")
Exit Function
End If
' LoadMeByName = objtemp
Return objtemp
End Function

Hopefully, somebody will take pity on me on a Friday!!

Many thanks in advance,
Simon
"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message news:bq******** *****@ID-208219.news.uni-berlin.de...
* "Simon Verona" <ne**@aphrodite uk.com> scripsit:
Thanks for the pointer.. Unfortunately, I can't quite work out the syntax for using this.


For example (there are many other ways to use 'CreateInstance '):

\\\
Private Function CreateClassByNa me( _
ByVal PartialAssembly Name As String, _
ByVal QualifiedClassN ame As String _
) As Object
Return _
Activator.Creat eInstance( _
[Assembly].LoadWithPartia lName( _
PartialAssembly Name _
).GetType(Quali fiedClassName) _
)
End Function
///

Usage:

\\\
Dim c As Control = _
DirectCast( _
CreateClassByNa me( _
"System.Windows .Forms", _
"System.Windows .Forms.Button" _
), _
Control _
)
With c
.Location = New Point(10, 10)
.Size = New Size(80, 26)
.Text = "Hello World"
End With
Me.Controls.Add (c)
///

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>

Nov 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1538
by: JJ L. | last post by:
Hello. I have a project that consists of nine different objects, each serving their own purpose. In the past I have just created a form for each one, and then whenever you call, say, object.Display(), it would call up the form associated with that object. This form only displays information, it doesn't allow the user to edit any information. Is it possible to find the properties of a certain object, and then loop through creating labels...
2
1401
by: salportaro | last post by:
Hello: Here is the situation. VB.NET I have several forms and I have a XML file that can start with a particular for for debugging/layout reasons. This is a mdi project. Normaly you would load a form as follows: Dim frmT As frmTrans frmT = New frmTrans
12
3175
by: Mats Lycken | last post by:
Hi, I'm creating a CMS that I would like to be plug-in based with different plugins handling different kinds of content. What I really want is to be able to load/unload plugins on the fly without restarting the application. What I did was to create an AppDomain that loaded the plugins and everything was great, until I tried to pass something else that strings between the domains...
0
1488
by: Jan | last post by:
I just started using .NET Remoting. I have a test system (client) which connects to our device (server) running XP Embedded. When my server object is called it uses a library where reflection (Type.InvokeMethod) is used to dynamically invoke a "command-object" in a list. The list of objects are created when server is started (in UI-thread). Each entry in the command list stores a Type reference, an object reference and the method name.
3
1906
by: MarkusJNZ | last post by:
Hi, given a name of an class e.g. "MyClass" which is stored as a string is it possible to create a new instance of this type dynamically and then reference the attributes by name in code?? e.g. If my class is called Person and it has attributes FirstName and LastName is it possible to do something like this; public void GeneratePerson()
6
5459
by: RSH | last post by:
Hi, i have a situation where I need to dynamically create objects in a loop. My question surrounds intantiation naming in such a scenerio. Below is a snippet that is basically hardcoding each object. My problem is that I would like to create the objects dynamically but I can't figure out how to do it How would I go about dynamically creating the required objects so that I could be using anywhere from 2 - 10 etc. ?
2
2999
by: FFrozTT | last post by:
I am having a problem creating a DLL with an entry point. I've been trying sub Main, DllMain, and I get nothing. When I run dumpbin - exports mydll.dll I see no entry points, also the dll when executed by rundll32 is not giving expected results. I have the project set to Class Library with Sub Main as the startup object, which I can't change to anything else anyway without modifying the .vbproj file which also seems to have no effect. ...
8
1451
by: Andrzej Lipski | last post by:
I am new to dotnet and I'll tried searching Google for a solution to my problem. I am hoping that it is possible to do, or am I going down a dead end? I have a User class that has known properties from a DB table (EmployeeID, FirstName, LastName) and a set of unknown properties that need to be appended to it. Is it possible to create a function that I can pass a class object and string containing the unknown property names into so...
7
2424
by: Robert | last post by:
Thanks George, I really am grateful for attempts to be helpful, but this really doesn't answer the question in my OP. What I am looking for is an explanation of WHY things are this way (I was not looking for a work-around). Again, I am appreciative of the feedback. I will note, that even though I can use Interfaces, the calling application and the dynamically loaded assembly both need compile-time refrences to the assembly that...
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10047
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9995
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.