Click here to Skip to main content
15,880,543 members
Home / Discussions / C#
   

C#

 
Questioncreating dynamic classes Pin
Manu_8127-Jun-06 13:36
Manu_8127-Jun-06 13:36 
AnswerRe: creating dynamic classes Pin
Guffa27-Jun-06 13:47
Guffa27-Jun-06 13:47 
GeneralRe: creating dynamic classes Pin
Manu_8127-Jun-06 14:23
Manu_8127-Jun-06 14:23 
GeneralRe: creating dynamic classes Pin
Guffa27-Jun-06 18:56
Guffa27-Jun-06 18:56 
GeneralRe: creating dynamic classes Pin
Karl 200027-Jun-06 20:26
Karl 200027-Jun-06 20:26 
GeneralRe: creating dynamic classes Pin
J4amieC27-Jun-06 21:57
J4amieC27-Jun-06 21:57 
AnswerRe: creating dynamic classes Pin
J4amieC27-Jun-06 21:58
J4amieC27-Jun-06 21:58 
AnswerRe: creating dynamic classes Pin
Rob Philpott27-Jun-06 23:15
Rob Philpott27-Jun-06 23:15 
Hi,

As well as the class name, you need to know what assembly the class is contained in. There are different ways of doing this, but just lifting a bit of code from work:

Assembly assembly = Assembly.LoadFile(@"c:\myassembly.dll");
object dynamicClass = assembly.CreateInstance(classNameFromDb);

Not sure that helps as you end up with an untyped object. The way we do it here is to make the objects we create dynamically implement an interface, so the second line actually looks more like

IDynamicObject dynamicClass = assembly.CreateInstance(classNameFromDb) as IDynamicObject;


This way, we can then call the methods the interface specifies.

After all, creating an instance of a class is little use if you have no idea what the object does. This is why defining an interface is useful.

And don't forget the gotcha, the trap I always fall in with this, its not just the type name you need, but also the namespace. eg. "MyNamespace.MyClass" which you need to pass to CreateInstance.



Regards,
Rob Philpott.
QuestionSerial Communication (Loopback) Pin
Cadence2.027-Jun-06 12:48
Cadence2.027-Jun-06 12:48 
AnswerRe: Serial Communication (Loopback) Pin
led mike27-Jun-06 20:38
led mike27-Jun-06 20:38 
GeneralRe: Serial Communication (Loopback) Pin
Cadence2.028-Jun-06 5:39
Cadence2.028-Jun-06 5:39 
AnswerRe: Serial Communication (Loopback) Pin
Cadence2.028-Jun-06 6:24
Cadence2.028-Jun-06 6:24 
GeneralRe: Serial Communication (Loopback) Pin
led mike28-Jun-06 7:44
led mike28-Jun-06 7:44 
QuestionResolve UNC Names Pin
HahnTech27-Jun-06 12:13
HahnTech27-Jun-06 12:13 
AnswerRe: Resolve UNC Names Pin
Mike Poz27-Jun-06 14:12
Mike Poz27-Jun-06 14:12 
GeneralRe: Resolve UNC Names Pin
HahnTech27-Jun-06 18:44
HahnTech27-Jun-06 18:44 
GeneralRe: Resolve UNC Names Pin
Mike Poz28-Jun-06 8:13
Mike Poz28-Jun-06 8:13 
GeneralRe: Resolve UNC Names Pin
HahnTech28-Jun-06 8:30
HahnTech28-Jun-06 8:30 
QuestionInheritance Pin
Besinci27-Jun-06 11:24
Besinci27-Jun-06 11:24 
AnswerRe: Inheritance Pin
Wjousts27-Jun-06 11:31
Wjousts27-Jun-06 11:31 
GeneralRe: Inheritance Pin
Judah Gabriel Himango27-Jun-06 13:05
sponsorJudah Gabriel Himango27-Jun-06 13:05 
AnswerRe: Inheritance Pin
Elina Blank27-Jun-06 11:34
sitebuilderElina Blank27-Jun-06 11:34 
QuestionRe: Inheritance Pin
BoneSoft27-Jun-06 12:38
BoneSoft27-Jun-06 12:38 
Questionforms Pin
Mohammed Elkholy27-Jun-06 11:20
Mohammed Elkholy27-Jun-06 11:20 
AnswerRe: forms Pin
Jun Du27-Jun-06 13:36
Jun Du27-Jun-06 13:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.