473,322 Members | 1,398 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

intantiate an object using a type

hi,

anyone knows how to instantiate a class, using only the type?

for example:

public void Init(Type sssType)
{
object = new sssType ?????
}

the method Init will be called like this:

Sustem.Typ sss = typeof(myClass);
Init(sss);

Nov 16 '05 #1
4 1345
public void Init(Type sssType)
{
object = System.Activator.CreateInstance(sssType);
}

(You don't actually need the "System." in there, but I wanted to show which
namepspace it was in).
"rapataa" <dg@rapataa.frup> wrote in message
news:31**************************@msgid.xenosite.n et...
hi,

anyone knows how to instantiate a class, using only the type?

for example:

public void Init(Type sssType)
{
object = new sssType ?????
}

the method Init will be called like this:

Sustem.Typ sss = typeof(myClass);
Init(sss);

Nov 16 '05 #2
Assuming that the class has a public default constructor:

imports System.Reflection;

public void Init(Type sssType) {
ConstructorInfo constructor = sssType.GetConstructor(new Type[]{});
if(constructor != null) {
object myObject = constructor.Invoke(new object[]{});
// Use myObject
}
}
"rapataa" <dg@rapataa.frup> wrote in message
news:31**************************@msgid.xenosite.n et...
hi,

anyone knows how to instantiate a class, using only the type?

for example:

public void Init(Type sssType)
{
object = new sssType ?????
}

the method Init will be called like this:

Sustem.Typ sss = typeof(myClass);
Init(sss);

Nov 16 '05 #3
Try this:

public void CreateInstance(type myType)
{
Assembly assembly = Assembly.GetCallingAssembly();
object typeObject = assembly.CreateInstance(myType.FullName);
}

--
Manohar Kamath
Editor, .netWire
www.dotnetwire.com
"rapataa" <dg@rapataa.frup> wrote in message
news:31**************************@msgid.xenosite.n et...
hi,

anyone knows how to instantiate a class, using only the type?

for example:

public void Init(Type sssType)
{
object = new sssType ?????
}

the method Init will be called like this:

Sustem.Typ sss = typeof(myClass);
Init(sss);

Nov 16 '05 #4
"Manohar Kamath" <mk*****@TAKETHISOUTkamath.com> wrote in message
news:u8**************@TK2MSFTNGP15.phx.gbl...
Try this:

public void CreateInstance(type myType)
{
Assembly assembly = Assembly.GetCallingAssembly();
But you don't neccesarily know that the type will be in the calling
assembly. It could be in mscorlib...
object typeObject = assembly.CreateInstance(myType.FullName);
}

--
Manohar Kamath
Editor, .netWire
www.dotnetwire.com
"rapataa" <dg@rapataa.frup> wrote in message
news:31**************************@msgid.xenosite.n et...
hi,

anyone knows how to instantiate a class, using only the type?

for example:

public void Init(Type sssType)
{
object = new sssType ?????
}

the method Init will be called like this:

Sustem.Typ sss = typeof(myClass);
Init(sss);


Nov 16 '05 #5

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

Similar topics

1
by: Bijay Kumar | last post by:
Hi Guys, I was going through the source code of Object.cs in rotor. What I found is Equals() implemented as follows: public extern virtual bool Equals(Object obj); What I don't...
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
2
by: Ryan Mitchley | last post by:
Hi all I have code for an object factory, heavily based on an article by Jim Hyslop (although I've made minor modifications). The factory was working fine using g++, but since switching to the...
0
by: Bijay Kumar | last post by:
Hi Guys, I was going through the source code of Object class (Object.cs in rotor). What I found is Equals() implemented as follows: public extern virtual bool Equals(Object obj); What...
0
by: Matt S | last post by:
Hello, I'm trying to build a C# client to consume an AXIS Web Service (running SOAP over HTTP). The Web Service encodes full server-side exception traces in the Soap Fault > Detail element...
7
by: Martin Robins | last post by:
I am currently looking to be able to read information from Active Directory into a data warehouse using a C# solution. I have been able to access the active directory, and I have been able to return...
1
by: J. Askey | last post by:
I am implementing a web service and thought it may be a good idea to return a more complex class (which I have called 'ServiceResponse') in order to wrap the original return value along with two...
16
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener...
11
by: Andrus | last post by:
I'm implementing entity object which should populate its properties from database when property is first referenced. In RDL reports I use object properties like MyObject.MyProperty MyObject...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.