By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,448 Members | 1,484 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,448 IT Pros & Developers. It's quick & easy.

intantiate an object using a type

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.