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

How to dyanamically instance a class from a string containing the class name?

P: n/a
In the exampe below, the constructor of the container class is passed the
string called "foo". Within the container class, I would like to create an
instance(s) of foo and add them to the ArrayList that is returned. (Once I
figure this out I will use reflection on the instance to do other stuff)

Anyway, How to I dyanamically create a new instance of a class from
ClassName string?

Thanks for your Help

Earl

private void Button1_Click(object sender, System.EventArgs e)
{
container mycontainer = new container("foo");
ArrayList fooArrayList = mycontainer.GetListOfObjects();
}

public class container
{
public string ClassName;
public container(object ClassName)
{
this.ClassName=ClassName;
}

public ArrayList GetListOfObjects()
{
ArrayList myArrayList = new ArrayList();
for(int i=1;i<10;i++)
{
//Convert.ToClass(ClassName) myClass = new Convert.ToClass(ClassName)
//myArrayList.Add(myClass);
}
return myArrayList;
}
}

public class foo
{
public string a="ahhhh";
public string b="bee";
public string c="see";
}
}
Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Earl Teigrob <ea******@hotmail.com> wrote:
In the exampe below, the constructor of the container class is passed the
string called "foo". Within the container class, I would like to create an
instance(s) of foo and add them to the ArrayList that is returned. (Once I
figure this out I will use reflection on the instance to do other stuff)

Anyway, How to I dyanamically create a new instance of a class from
ClassName string?


Look at Type.GetType, Assembly.GetType and Activator.CreateInstance.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2

P: n/a
Hi,

Try Activator.CreateInstance( );

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Earl Teigrob" <ea******@hotmail.com> wrote in message
news:O1**************@tk2msftngp13.phx.gbl...
In the exampe below, the constructor of the container class is passed the
string called "foo". Within the container class, I would like to create an
instance(s) of foo and add them to the ArrayList that is returned. (Once I
figure this out I will use reflection on the instance to do other stuff)

Anyway, How to I dyanamically create a new instance of a class from
ClassName string?

Thanks for your Help

Earl

private void Button1_Click(object sender, System.EventArgs e)
{
container mycontainer = new container("foo");
ArrayList fooArrayList = mycontainer.GetListOfObjects();
}

public class container
{
public string ClassName;
public container(object ClassName)
{
this.ClassName=ClassName;
}

public ArrayList GetListOfObjects()
{
ArrayList myArrayList = new ArrayList();
for(int i=1;i<10;i++)
{
//Convert.ToClass(ClassName) myClass = new Convert.ToClass(ClassName)
//myArrayList.Add(myClass);
}
return myArrayList;
}
}

public class foo
{
public string a="ahhhh";
public string b="bee";
public string c="see";
}
}

Nov 15 '05 #3

P: n/a
Thanks for the replays

I have got it this far from what I have read but gettype is returning a null
value in the following code

private void Button1_Click(object sender, System.EventArgs e)
{
Type t = Type.GetType("foo"); //returns a null value
object classInstance = Activator.CreateInstance(t); //blows up because t
is null
}

public class foo
{
public string a="ahhhh";
public string b="bee";
public string c="see";
}

"Earl Teigrob" <ea******@hotmail.com> wrote in message
news:O1**************@tk2msftngp13.phx.gbl...
In the exampe below, the constructor of the container class is passed the
string called "foo". Within the container class, I would like to create an
instance(s) of foo and add them to the ArrayList that is returned. (Once I
figure this out I will use reflection on the instance to do other stuff)

Anyway, How to I dyanamically create a new instance of a class from
ClassName string?

Thanks for your Help

Earl

private void Button1_Click(object sender, System.EventArgs e)
{
container mycontainer = new container("foo");
ArrayList fooArrayList = mycontainer.GetListOfObjects();
}

public class container
{
public string ClassName;
public container(object ClassName)
{
this.ClassName=ClassName;
}

public ArrayList GetListOfObjects()
{
ArrayList myArrayList = new ArrayList();
for(int i=1;i<10;i++)
{
//Convert.ToClass(ClassName) myClass = new Convert.ToClass(ClassName)
//myArrayList.Add(myClass);
}
return myArrayList;
}
}

public class foo
{
public string a="ahhhh";
public string b="bee";
public string c="see";
}
}

Nov 15 '05 #4

P: n/a
You need to use the full name of the type: GetType("yourNamespace.foo")

--
Michael R
cr**@tampabay.rr.com

"Earl Teigrob" <ea******@hotmail.com> wrote in message
news:O7*************@tk2msftngp13.phx.gbl...
Thanks for the replays

I have got it this far from what I have read but gettype is returning a null value in the following code

private void Button1_Click(object sender, System.EventArgs e)
{
Type t = Type.GetType("foo"); //returns a null value
object classInstance = Activator.CreateInstance(t); //blows up because t is null
}

public class foo
{
public string a="ahhhh";
public string b="bee";
public string c="see";
}

"Earl Teigrob" <ea******@hotmail.com> wrote in message
news:O1**************@tk2msftngp13.phx.gbl...
In the exampe below, the constructor of the container class is passed the string called "foo". Within the container class, I would like to create an instance(s) of foo and add them to the ArrayList that is returned. (Once I figure this out I will use reflection on the instance to do other stuff)

Anyway, How to I dyanamically create a new instance of a class from
ClassName string?

Thanks for your Help

Earl

private void Button1_Click(object sender, System.EventArgs e)
{
container mycontainer = new container("foo");
ArrayList fooArrayList = mycontainer.GetListOfObjects();
}

public class container
{
public string ClassName;
public container(object ClassName)
{
this.ClassName=ClassName;
}

public ArrayList GetListOfObjects()
{
ArrayList myArrayList = new ArrayList();
for(int i=1;i<10;i++)
{
//Convert.ToClass(ClassName) myClass = new Convert.ToClass(ClassName) //myArrayList.Add(myClass);
}
return myArrayList;
}
}

public class foo
{
public string a="ahhhh";
public string b="bee";
public string c="see";
}
}


Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.