dynamic classes

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

Guest

Hi

I have 2 classes, layout1 and layout2, both return different table layouts

In my controlling program I create one of these tables using

Table myTable = new layout1

I need to parameterise the 'layout1' part of this statement, so I can choose a different layout at runtime. Is this possible or is there a better way of doing this

Thank
Kieran
 
Table myTable;
if (...) myTable = new layout1 else myTable = new layout2;

I'm not really sure if I got your question...

Niki

Kieran said:
Hi,

I have 2 classes, layout1 and layout2, both return different table layouts.

In my controlling program I create one of these tables using:

Table myTable = new layout1;

I need to parameterise the 'layout1' part of this statement, so I can
choose a different layout at runtime. Is this possible or is there a better
way of doing this?
 
Hi

Thanks for your reply, I was hoping though to feed in the class name though, e.

string myClass = 'layout1

Table myTable = new myClas

I know the above wont work but I hope this explains what I'm trying to do. I'm effectively trying to decide the class to use at runtime?

Thank
Kieran
 
Ok, that explains at least what you want to do: You can use
Activator.CreateInstance("name-of-your-assembly", "layout1").Unwrap(), to
create an instance of a class by its name.

However, the more "type-safe" approach would be something like:

Type myClass = typeof(layout1);
....
Table myTable = Activator.CreateInstance(myClass);

Or you could implement IClonable in all your classes, and write:

ICloneable myClass = new layout1;
....
Table myTable = myClass.Clone();

(I think this one is GoF pattern, but don't remember it's name)

Niki

Kieran said:
Hi,

Thanks for your reply, I was hoping though to feed in the class name though, e.g

string myClass = 'layout1'

Table myTable = new myClass

I know the above wont work but I hope this explains what I'm trying to do.
I'm effectively trying to decide the class to use at runtime?.
 
Back
Top