Create Instance Of Class Using text name

G

Guest

I want create an instance of an object which accepts a Data Reader as a
constructor. I realize that I could create a gigantic case statement in
which I test for each text name, but that case statement would be rather
larger and I would have to revise as new classes are added or removed.
Another variable will tell me the namespace that I will find the class in.
So I would have a case statement which would evaluate the variable for
namespace, but what would the code look like to create a class for that text
name.

Example:
string classNm = "Account_Tbl";
switch (myNameSpaces)
{
case "GL.Tables"
Now I want to create and instance of "GL.Tables.Account_Tbl" but
I want to
use the variable "classNm" to create the equivalent of the
statement below:
GL.Tables.Account_Tbl = new GL.Tables.Account(myDataReader);
break;
case "PACS.Tables"
Now I want to create and instance of "PACS.Tables.Account_Tbl"
but I want
to use the var "classNm" to create the equivalent of the
statement below:
PACS.Tables.Account_Tbl = new PACS.Tables.Account(myDataReader);
break;
 
P

Peter K

=?Utf-8?B?T2xkQnV0U3RpbGxMZWFybmluZw==?=
I want create an instance of an object which accepts a Data Reader as
a constructor. I realize that I could create a gigantic case
statement in which I test for each text name, but that case statement
would be rather larger and I would have to revise as new classes are
added or removed. Another variable will tell me the namespace that I
will find the class in. So I would have a case statement which would
evaluate the variable for namespace, but what would the code look like
to create a class for that text name.

Example:
string classNm = "Account_Tbl";
switch (myNameSpaces)
{
case "GL.Tables"
Now I want to create and instance of
"GL.Tables.Account_Tbl" but
I want to
use the variable "classNm" to create the equivalent of the
statement below:
GL.Tables.Account_Tbl = new
GL.Tables.Account(myDataReader);
break;
case "PACS.Tables"
Now I want to create and instance of
"PACS.Tables.Account_Tbl"
but I want
to use the var "classNm" to create the equivalent of the
statement below:
PACS.Tables.Account_Tbl = new
PACS.Tables.Account(myDataReader);
break;

System.Activator has several methods for instantiating objects for you.

For example:
Type myType = Type.GetType("GL.Tables.Account_Tbl");

object[] params = new object[1];
params[0] = myDataReader;

Activator.CreateInstance(myType, params);


(I haven't tried this, but something like the above should work).

/Peter
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

in message |I want create an instance of an object which accepts a Data Reader as a
| constructor. I realize that I could create a gigantic case statement in
| which I test for each text name, but that case statement would be rather
| larger and I would have to revise as new classes are added or removed.
| Another variable will tell me the namespace that I will find the class in.
| So I would have a case statement which would evaluate the variable for
| namespace, but what would the code look like to create a class for that
text
| name.

Take a look at CreateInstance , depending of where your classes resides, or
how you plan to instantiate them you will have to use the correct class like
Assembly, AppDomain, Activator, etc


Also you should pay attention to the return type, you will have to return
object unless all your classes share a common base
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top