Text to Class

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am wanting to write a program which is going to all the user to select a
table and then copy the table values from one environment to another. There
are of course hunderds of tables, and I have created a class for each of
these tables, and DAL class which contains all the methods to read or update
any value for the given table.
Can I take a "Text" name and use it to somehow create and instance of a
class with that same name?

So say a had a Table name of "Account". For this table I have two classes,
one called "Account.cs" and another called "Account_DAL.cs". First I want to
create an instance of the "Account.cs". To do this specifically, my code
would look like the following:

Account myAccount = new Account(fields);

Account_Dal.cs is a class which ONLY contains STATIC methods. The code to
use a STATIC method to insert values into a Table might look like the
following:

bool ok = Account_Dal.InsertRow(myAccount);

Is there a way to create an instance of "Account.cs" with the text of
"Account"?
Is there a way to call a STATIC method of "Account_Dal.cs" with the text of
"Account_Dal" and pass the method called "InserRow" an instance of the
"Account.cs" created above?

Thanks in advance for your assistance!!!!
 
It is most likely that your classes are not called "Account.cs" and
"Account_DAL.cs" but rather, Account and Account_DAL (what you mentioned are
most likely the files that they are in).

In order to create an instance based on the name of the type, you will
have to get an instance of the Type that corresponds to the class first.
You can do this by calling the static GetType method on the Type class,
passing the name of the type. The type name has to include the namespace as
well. If you are making the call to GetType outside of the assembly that
contains the type, then you will need the assembly qualified name.

Once you have that, you can pass the Type instance to the static
CreateInstance method on the Activator class, passing the Type, and then an
object array with one element (in this case, fields). It will return an
object to you. Now, you can cast this object to your type, or to a base
type that all your classes share (if there is one). If those are not
available options, then to work with the instance, you will have to make
calls through reflection (which is what you have to do next as well).

Assuming that the naming is the same (table name + "_DAL") as well as
the method (InsertRow), you can find the type of the DAL class (constructing
the type name as above) and then calling GetType. With that, you then want
to call the GetMethod class, passing the name of the method ("InsertRow")
getting the MethodInfo instance that corresponds to the method.

Finally, you can pass your instance from before to the Invoke method on
the MethodInfo instance (passing null for the instance parameter, since it
is a static method) and then you should be done.
 

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

Back
Top