Click here to Skip to main content
15,921,542 members
Home / Discussions / C#
   

C#

 
GeneralRe: GraphicsPaths and Scrolling Pin
Paul Conrad23-Dec-07 9:57
professionalPaul Conrad23-Dec-07 9:57 
GeneralCell phones application using c#.net Pin
mostafa_h12-Dec-07 5:26
mostafa_h12-Dec-07 5:26 
GeneralRe: Cell phones application using c#.net Pin
Sathesh Sakthivel12-Dec-07 6:57
Sathesh Sakthivel12-Dec-07 6:57 
GeneralRe: Cell phones application using c#.net Pin
mostafa_h12-Dec-07 9:49
mostafa_h12-Dec-07 9:49 
Generalc# event log size Pin
oi12-Dec-07 5:19
oi12-Dec-07 5:19 
GeneralDynamically instantiating a type and validating interface implementation Pin
Le centriste12-Dec-07 4:49
Le centriste12-Dec-07 4:49 
GeneralRe: Dynamically instantiating a type and validating interface implementation Pin
led mike12-Dec-07 5:11
led mike12-Dec-07 5:11 
GeneralRe: Dynamically instantiating a type and validating interface implementation Pin
m@u12-Dec-07 5:22
m@u12-Dec-07 5:22 
Hi
there are at least 2 ways of doing this

1) with a generic Method that looks something like this:

public T loadObject<t>(Type TypeToLoad)
{
    T RetVal = (T)TypeToLoad.GetConstructor(null).Invoke(null);
    return RetVal;
}
</t>


this will throw an CastException if the Type does not the interface T
and the call is quite simple:
myInterface Obj = loadObject<myinterface>(typeof(SomeType));<br />
<br />
2) with reflection<br />
<br />
<pre><br />
public object loadObject(Type TypeToLoad, Type Interface)<br />
{<br />
    Type[] ifs = TypeToLoad.GetInterfaces();<br />
    if (Array.IndexOf(ifs, Interface) == -1)<br />
    {<br />
        throw new Exception("It didn't work!");<br />
    }<br />
    return TypeToLoad.GetConstructor(null).Invoke(null);<br />
}<br />
</pre><br />
<br />
this will work with 1.1 (if you still use that one..) and it will fail *before* the instance is created. but you will have to cast the return - value to your interface type:<br />
<code>myInterface Obj = loadObject(typeof(SomeType), typeof(myInterface)) as myInterface;


greets
m@u
GeneralRe: Dynamically instantiating a type and validating interface implementation Pin
Le centriste12-Dec-07 5:54
Le centriste12-Dec-07 5:54 
Generaldeclaring form Pin
arkiboys12-Dec-07 4:35
arkiboys12-Dec-07 4:35 
GeneralRe: declaring form Pin
Kristian Sixhøj12-Dec-07 5:31
Kristian Sixhøj12-Dec-07 5:31 
GeneralRe: declaring form Pin
arkiboys12-Dec-07 5:50
arkiboys12-Dec-07 5:50 
GeneralRe: declaring form Pin
Kristian Sixhøj12-Dec-07 6:47
Kristian Sixhøj12-Dec-07 6:47 
GeneralRe: declaring form Pin
Sathesh Sakthivel12-Dec-07 6:58
Sathesh Sakthivel12-Dec-07 6:58 
GeneralRe: declaring form Pin
Luc Pattyn12-Dec-07 7:31
sitebuilderLuc Pattyn12-Dec-07 7:31 
QuestionCan radio button text be set at left instead of right Pin
MANOJ BATRA12-Dec-07 4:28
MANOJ BATRA12-Dec-07 4:28 
AnswerRe: Can radio button text be set at left instead of right Pin
c0ax_lx12-Dec-07 5:20
c0ax_lx12-Dec-07 5:20 
QuestionHow to change panel and form border color at design time Pin
MANOJ BATRA12-Dec-07 4:11
MANOJ BATRA12-Dec-07 4:11 
QuestionHow to get all ButtonItem in RibbonControl of DevComponents.DotNetBar Pin
RockPhoenix12-Dec-07 2:57
RockPhoenix12-Dec-07 2:57 
GeneralSerializaion and XML Serialization issue Pin
manustone12-Dec-07 1:45
manustone12-Dec-07 1:45 
GeneralRe: Serializaion and XML Serialization issue Pin
TJoe12-Dec-07 4:12
TJoe12-Dec-07 4:12 
GeneralRe: Serializaion and XML Serialization issue Pin
Skippums12-Dec-07 5:52
Skippums12-Dec-07 5:52 
GeneralCross Thread Calls Pin
MicealG12-Dec-07 0:55
MicealG12-Dec-07 0:55 
GeneralRe: Cross Thread Calls Pin
Pete O'Hanlon12-Dec-07 2:00
mvePete O'Hanlon12-Dec-07 2:00 
AnswerRe: Cross Thread Calls Pin
MicealG12-Dec-07 3:00
MicealG12-Dec-07 3:00 

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.