473,563 Members | 2,856 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

(Probably obvious) .NET question - creating objects dynamically

Hi all,

I have a solution, which contains several projects. Each project occupies
the same namespace, and compiles to s different .NET DLL

e.g.

MyNamespace.Thi sClass is in one DLL
MyNamespace.Tha tClass is in another DLL

I wish for a .NET EXE to be able to dynamically create an instance of either
on of the classes, given the classes name as a string.

I have tried using System.Reflecti on.Assembly calls, but watching in debug,
the Load call fails.

What would the mechanism be for doing this ?

thanks in advance.
Nov 20 '05 #1
2 1085
* "Jethro" <Je*******@hotm ail.com> scripsit:
I have a solution, which contains several projects. Each project occupies
the same namespace, and compiles to s different .NET DLL

e.g.

MyNamespace.Thi sClass is in one DLL
MyNamespace.Tha tClass is in another DLL

I wish for a .NET EXE to be able to dynamically create an instance of either
on of the classes, given the classes name as a string.

I have tried using System.Reflecti on.Assembly calls, but watching in debug,
the Load call fails.


\\\
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]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #2
Jethro,
A variation of the Activator.Creat eInstance that I normally use.

I use System.Type.Get Type to return a Type object for the concrete class I
want (determined via the app.config). The strings in the app.config should
to be in "myproject.mycl ass, myassembly" format.

I use System.Activato r.CreateInstanc e to create an instance of the concrete
class.

' VB.NET sample
Dim s As String
s = ConfigurationSe ttings.AppSetti ngs("myplugin")

Dim t As Type
t = Type.GetType(s)

Dim obj As Object
obj = Activator.Creat eInstance(t)
' Or if you want to pass parameters to the constructor
obj = Activator.Creat eInstance(t, New Object() {p1, p2})
' the above calls the constructor that has two parameters.
' there are 7 or so overloads of CreateInstance,
' if you have default constructors or other needs

Dim plugin As MyPlugInBase
plugin = DirectCast(obj, MyBaseClass)

I use this in a couple of projects, works very well.

Hope this helps
Jay


"Jethro" <Je*******@hotm ail.com> wrote in message
news:ca******** **@hercules.bti nternet.com...
Hi all,

I have a solution, which contains several projects. Each project occupies
the same namespace, and compiles to s different .NET DLL

e.g.

MyNamespace.Thi sClass is in one DLL
MyNamespace.Tha tClass is in another DLL

I wish for a .NET EXE to be able to dynamically create an instance of either on of the classes, given the classes name as a string.

I have tried using System.Reflecti on.Assembly calls, but watching in debug, the Load call fails.

What would the mechanism be for doing this ?

thanks in advance.

Nov 20 '05 #3

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

Similar topics

6
565
by: Bhavin | last post by:
Anybody quickly replies it, would be a great help! I try to create dynamic array of textboxes control in ASP.NET(C#). I got the following error while I tried to assign value to ID property of dynamically created textbox. (look at my code please ) Error: “ Object reference is not set to an instance of Object” totalRows – int...
2
1529
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...
5
2269
by: | last post by:
Trying to learn about manipulating collections of objects, and populating these objects dynamically from datasources. Could someone post a code sample that shows the following: Instantiating a collection object -- say, a dictionary. Populating that collection object with custom objects, say, Person. What I really want to see is how to...
1
1347
by: chris | last post by:
I know I've asked this before, but I didn't really get an answer and I bet it's because I didn't explain myself very well. Here goes again. I have this code: Dim arrData(intNoOfRows, intNoOfColumns) As Object Dim intR As Integer For intC As Integer = 0 To intNoOfColumns - 1
2
2686
by: Steven D'Aprano | last post by:
When using the timeit module, you pass the code you want to time as strings: import timeit t = timeit.Timer("foo(x, y)", \ """from module import foo x = 27 y = 45 """) elapsed_time = t.timeit()
9
2059
by: William | last post by:
Heya, I have been looking all over Google for ways to *create* an XML document using any Javascript object, and it seems that no one has ever tried or ever needed this. I want to send the server some info dynamically created on the page, and instead of sending a complicated GET request with the info, or creating an XML using my own set...
7
8208
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
6
5449
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...
1
1561
by: krishna81m | last post by:
I am unable to create a list of objects dynamically. I run a SQL query on the database and populate objects of types that I also know aprior. I read the query as a string, run it on the database and want to be able to populate these objects of classes whose names are known to me also based on the type of the query. In this query, SomeClass is a...
0
7665
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, well explore What is ONU, What Is Router, ONU & Routers main...
0
7583
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...
0
8106
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...
1
7642
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...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
924
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...

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.