Click here to Skip to main content
15,902,275 members
Home / Discussions / C#
   

C#

 
QuestionGetting the type from a string, any magic for this in C#? Pin
ioctl514-Apr-09 22:30
ioctl514-Apr-09 22:30 
AnswerRe: Getting the type from a string, any magic for this in C#? Pin
Steve Echols4-Apr-09 23:15
Steve Echols4-Apr-09 23:15 
GeneralRe: Getting the type from a string, any magic for this in C#? Pin
ioctl514-Apr-09 23:26
ioctl514-Apr-09 23:26 
GeneralRe: Getting the type from a string, any magic for this in C#? Pin
Steve Echols4-Apr-09 23:46
Steve Echols4-Apr-09 23:46 
AnswerRe: Getting the type from a string, any magic for this in C#? Pin
Luc Pattyn4-Apr-09 23:34
sitebuilderLuc Pattyn4-Apr-09 23:34 
GeneralRe: Getting the type from a string, any magic for this in C#? Pin
ioctl514-Apr-09 23:38
ioctl514-Apr-09 23:38 
GeneralRe: Getting the type from a string, any magic for this in C#? Pin
Steve Echols4-Apr-09 23:59
Steve Echols4-Apr-09 23:59 
AnswerRe: Getting the type from a string, any magic for this in C#? Pin
PIEBALDconsult5-Apr-09 4:29
mvePIEBALDconsult5-Apr-09 4:29 
A) This may be a good place to use var.

B) Accessing Reflection can be time-consuming, so I prefer to cache the results.

C) Typically, in situations like this, I use a Dictionary. Here I might suggest a Dictionary<string,System.Reflection.ConstructorInfo>. Populate it with the expected/allowed types your application should support. Then something like:

var x = dic [ somestring ].Invoke() ;

would invoke the constructor and return an instance of whatever type was requested (or fail). However, it's Sunday morning and I'm still on my first cup of coffee, so I don't expect the above syntax to be correct (I'm pretty sure it isn't, but it should communicate the concept well enough).

D) If you want to support types that aren't known at compile time, you will need to learn how to dynamically load assemblies. There are several articles on that here.
JokeRe: Getting the type from a string, any magic for this in C#? Pin
Luc Pattyn5-Apr-09 4:54
sitebuilderLuc Pattyn5-Apr-09 4:54 
GeneralRe: Getting the type from a string, any magic for this in C#? Pin
PIEBALDconsult5-Apr-09 5:04
mvePIEBALDconsult5-Apr-09 5:04 
AnswerRe: Getting the type from a string, any magic for this in C#? Pin
PIEBALDconsult5-Apr-09 5:24
mvePIEBALDconsult5-Apr-09 5:24 
QuestionHow to change the color of second line in RichText Box? Pin
Lijo Rajan4-Apr-09 22:23
Lijo Rajan4-Apr-09 22:23 
AnswerRe: How to change the color of second line in RichText Box? Pin
Xmen Real 4-Apr-09 23:11
professional Xmen Real 4-Apr-09 23:11 
GeneralRe: How to change the color of second line in RichText Box? Pin
Bit_flipper6-Feb-10 7:42
Bit_flipper6-Feb-10 7:42 
Questionproblem in using namespaces Pin
yogesh_softworld1234-Apr-09 22:07
yogesh_softworld1234-Apr-09 22:07 
AnswerRe: problem in using namespaces Pin
Luc Pattyn4-Apr-09 22:49
sitebuilderLuc Pattyn4-Apr-09 22:49 
AnswerRe: problem in using namespaces Pin
ramz_g8-Apr-09 19:49
ramz_g8-Apr-09 19:49 
Questionproblem to importing DLL files Pin
cppwxwidgetsss4-Apr-09 21:35
cppwxwidgetsss4-Apr-09 21:35 
AnswerRe: problem to importing DLL files Pin
Luc Pattyn4-Apr-09 22:54
sitebuilderLuc Pattyn4-Apr-09 22:54 
GeneralRe: problem to importing DLL files Pin
PIEBALDconsult5-Apr-09 4:14
mvePIEBALDconsult5-Apr-09 4:14 
QuestionSeparator Line on form.....help guys! Pin
Rajdeep.NET is BACK4-Apr-09 21:21
Rajdeep.NET is BACK4-Apr-09 21:21 
AnswerRe: Separator Line on form.....help guys! Pin
Harvey Saayman4-Apr-09 22:02
Harvey Saayman4-Apr-09 22:02 
GeneralRe: Separator Line on form.....help guys! Pin
Rajdeep.NET is BACK4-Apr-09 22:13
Rajdeep.NET is BACK4-Apr-09 22:13 
GeneralRe: Separator Line on form.....help guys! Pin
Rajdeep.NET is BACK4-Apr-09 22:16
Rajdeep.NET is BACK4-Apr-09 22:16 
GeneralRe: Separator Line on form.....help guys! Pin
Luc Pattyn4-Apr-09 22:59
sitebuilderLuc Pattyn4-Apr-09 22:59 

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.