Click here to Skip to main content
15,892,643 members
Home / Discussions / C#
   

C#

 
GeneralRe: Tokenize which ignores quotes and parantheses etc. Pin
peterchen20-Apr-07 11:51
peterchen20-Apr-07 11:51 
GeneralRe: Tokenize which ignores quotes and parantheses etc. Pin
Judah Gabriel Himango22-Apr-07 8:21
sponsorJudah Gabriel Himango22-Apr-07 8:21 
QuestionExecuting exe from exe, closing the 1st one without closing he 2nd!!! Pin
Adeel Chaudhry19-Apr-07 7:02
Adeel Chaudhry19-Apr-07 7:02 
AnswerRe: Executing exe from exe, closing the 1st one without closing he 2nd!!! Pin
Tarakeshwar Reddy19-Apr-07 7:10
professionalTarakeshwar Reddy19-Apr-07 7:10 
GeneralRe: Executing exe from exe, closing the 1st one without closing he 2nd!!! Pin
Adeel Chaudhry19-Apr-07 7:14
Adeel Chaudhry19-Apr-07 7:14 
QuestionSystem.Type from class name string, interface support and instantiation Pin
peterchen19-Apr-07 6:54
peterchen19-Apr-07 6:54 
AnswerRe: System.Type from class name string, interface support and instantiation Pin
hamid_m19-Apr-07 7:41
hamid_m19-Apr-07 7:41 
AnswerRe: System.Type from class name string, interface support and instantiation [modified] Pin
Kythen20-Apr-07 10:57
Kythen20-Apr-07 10:57 
Here's how I do this task in my class factories:

First use System.Type.GetType("your class name") to get a Type object for the class. You can then use the Assembly property of that Type object to get the assembly that has that class. Use the GetInterface("your interface") method to see if the class implements a given interface. If the method returns null or the type object returned is not an instance of the given interface, the class does not implement the interface. Finally, you can use Activator.CreateInstance(class type object) to get an instance of your class. I've included some example code below.

<br />
public YourInterfaceOrClassType GetClassInstance(string classNameString)<br />
{<br />
    //  Get the actual Type object for the given type.<br />
    System.Type classType = System.Type.GetType(classNameString);<br />
<br />
    //  Throw an exception if the type could not be found.<br />
    if(classType == null)<br />
        throw new TypeLoadException(String.Format("Type {0} does not exist.", classNameString));<br />
<br />
    //  Make sure that the type implements the desired interface.<br />
    Type interfaceType = classType.GetInterface(yourInterfaceNameString, true);<br />
    if(interfaceType == null || !interfaceType.Equals(typeof(YourInterfaceType)))<br />
        throw new TypeLoadException(String.Format("Type {0} does not implement the {1} interface.", classNameString, yourInterfaceNameString));<br />
<br />
    //  Make sure that the type is not an abstract class that cannot be instantiated.<br />
    if(classType.IsAbstract)<br />
        throw new TypeLoadException(String.Format("Type {0} is abstract and cannot be instantiated.", classNameString);<br />
<br />
    //  Create an instance of the type.<br />
    return (YourInterfaceOrClassType)Activator.CreateInstance(classType);<br />
}<br />



-- modified at 18:51 Friday 20th April, 2007
GeneralRe: System.Type from class name string, interface support and instantiation Pin
peterchen20-Apr-07 11:44
peterchen20-Apr-07 11:44 
QuestionSimple question Does any have a nice control derived from IToolboxService? Pin
Christopher Stratmann19-Apr-07 6:28
Christopher Stratmann19-Apr-07 6:28 
QuestionMaster/details Pin
hadad19-Apr-07 5:26
hadad19-Apr-07 5:26 
AnswerRe: Master/details Pin
Dmitry Khudorozhkov19-Apr-07 14:12
Dmitry Khudorozhkov19-Apr-07 14:12 
QuestionIList implementation with change event? Pin
peterchen19-Apr-07 5:20
peterchen19-Apr-07 5:20 
Answer[sort of solved] Pin
peterchen19-Apr-07 6:59
peterchen19-Apr-07 6:59 
AnswerRe: IList implementation with change event? Pin
Leslie Sanford19-Apr-07 8:53
Leslie Sanford19-Apr-07 8:53 
GeneralRe: IList implementation with change event? Pin
peterchen19-Apr-07 13:29
peterchen19-Apr-07 13:29 
QuestionGenerate a form by using form name as string Pin
ayca19-Apr-07 5:02
ayca19-Apr-07 5:02 
AnswerRe: Generate a form by using form name as string [modified] Pin
Tarakeshwar Reddy19-Apr-07 5:09
professionalTarakeshwar Reddy19-Apr-07 5:09 
QuestionRe: Generate a form by using form name as string Pin
CPallini19-Apr-07 5:19
mveCPallini19-Apr-07 5:19 
AnswerRe: Generate a form by using form name as string Pin
Tarakeshwar Reddy19-Apr-07 5:22
professionalTarakeshwar Reddy19-Apr-07 5:22 
AnswerRe: Generate a form by using form name as string Pin
Colin Angus Mackay19-Apr-07 5:17
Colin Angus Mackay19-Apr-07 5:17 
Question&quot;pipe&quot; messages to a windows textbox (or other windows pane) [modified] Pin
Ashley Sanders19-Apr-07 4:32
Ashley Sanders19-Apr-07 4:32 
AnswerRe: "pipe" messages to a windows textbox (or other windows pane) Pin
Dave Kreskowiak19-Apr-07 5:10
mveDave Kreskowiak19-Apr-07 5:10 
AnswerRe: &amp;quot;pipe&amp;quot; messages to a windows textbox (or other windows pane) Pin
Luc Pattyn19-Apr-07 7:53
sitebuilderLuc Pattyn19-Apr-07 7:53 
QuestionFile transfer Pin
Niiiissssshhhhhuuuuu19-Apr-07 3:57
Niiiissssshhhhhuuuuu19-Apr-07 3:57 

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.