J
jerryau
Hi,
I am trying to dynamically create object instances based on a string
class name, and then I need to dynamically set values to these
objects, but I have no idea how to do this in C#. Here is the example
of what I need to do:
******Dog.cs******
namespace MySystem
{
public class Dog
{
private string name;
public string name
{
get { return name; }
set { name = value; }
}
}
}
******Cat.cs******
namespace MySystem
{
public class Cat
{
private string name;
public string name
{
get { return name; }
set { name = value; }
}
}
}
******SetAnimals.cs******
namespace MySystem
{
public class SetAnimals
{
string[,] animals = new string[,] {{"Dog","dogname1"},
{"Dog","dogname2"},{"Cat","catname1"}};
public IList setAnimals()
{
IList animalList = new ArrayList();
// Somehow dynamically create a new instance of an object and
add it to the animalList
// e.g. animals[0,0] is a Dog type, therefore dynamically
create a Dog class, and set the name
// for the just created Dog class taken from animals[0,1]
"dogname1"
return animalList;
}
}
}
Is what I'm trying to do possible in C#? Does anyone have any ideas
how this can be done?
Thanks,
Jerry
I am trying to dynamically create object instances based on a string
class name, and then I need to dynamically set values to these
objects, but I have no idea how to do this in C#. Here is the example
of what I need to do:
******Dog.cs******
namespace MySystem
{
public class Dog
{
private string name;
public string name
{
get { return name; }
set { name = value; }
}
}
}
******Cat.cs******
namespace MySystem
{
public class Cat
{
private string name;
public string name
{
get { return name; }
set { name = value; }
}
}
}
******SetAnimals.cs******
namespace MySystem
{
public class SetAnimals
{
string[,] animals = new string[,] {{"Dog","dogname1"},
{"Dog","dogname2"},{"Cat","catname1"}};
public IList setAnimals()
{
IList animalList = new ArrayList();
// Somehow dynamically create a new instance of an object and
add it to the animalList
// e.g. animals[0,0] is a Dog type, therefore dynamically
create a Dog class, and set the name
// for the just created Dog class taken from animals[0,1]
"dogname1"
return animalList;
}
}
}
Is what I'm trying to do possible in C#? Does anyone have any ideas
how this can be done?
Thanks,
Jerry