Click here to Skip to main content
15,916,280 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to assign DataView to Datatable Pin
J$31-Jan-08 9:38
J$31-Jan-08 9:38 
GeneralComboBox TextChanged Event firing unexpectedly [modified] Pin
DaveyM6931-Jan-08 8:29
professionalDaveyM6931-Jan-08 8:29 
GeneralThreading help Pin
daphne7531-Jan-08 8:21
daphne7531-Jan-08 8:21 
GeneralRe: Threading help Pin
Luc Pattyn31-Jan-08 11:03
sitebuilderLuc Pattyn31-Jan-08 11:03 
GeneralRe: Threading help Pin
daphne751-Feb-08 9:19
daphne751-Feb-08 9:19 
GeneralLogging Shutdown event in Windows Service Pin
abupsman31-Jan-08 7:46
abupsman31-Jan-08 7:46 
GeneralRe: Logging Shutdown event in Windows Service Pin
CKnig31-Jan-08 18:56
CKnig31-Jan-08 18:56 
QuestionHow to instantiate an object with a generic parameter? Pin
michal.kreslik31-Jan-08 7:31
michal.kreslik31-Jan-08 7:31 
Hello,

I can't figure this one out, so I'll be happy if someone lends me a helping hand here.

How do I instantiate an object with a generic type parameter if I don't know what the type parameter will be at run time?

Example:

namespace GenericTypeInitialization
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Press 1 for FooA, press 2 for FooB:");
            ConsoleKeyInfo key = Console.ReadKey(true);

            Type TypeOfFoo;

            if (key.KeyChar == '1')
            {
                Console.WriteLine("FooA selected.");
                TypeOfFoo = typeof(FooA);
            }
            else if (key.KeyChar == '2')
            {
                Console.WriteLine("FooB selected.");
                TypeOfFoo = typeof(FooB);
            }

            ////
            Bar<TypeOfFoo> MyInstanceOfBar = new Bar<TypeOfFoo>();
        }
    }

    class FooA {}

    class FooB {}

    class Bar<TFoo> {}
}

Obviously, the above code doesn't compile. I'm getting an error of

The type or namespace name 'TypeOfFoo' could not be found (are you missing a using directive or an assembly reference?)

How do I get the type parameter at run time then? I've tried various things, but none worked. Also, you can't instantiate the Bar<> class before '////'.

Thanks a lot for any input,
Michal
AnswerRe: How to instantiate an object with a generic parameter? Pin
Giorgi Dalakishvili31-Jan-08 8:18
mentorGiorgi Dalakishvili31-Jan-08 8:18 
AnswerRe: How to instantiate an object with a generic parameter? Pin
Ennis Ray Lynch, Jr.31-Jan-08 9:00
Ennis Ray Lynch, Jr.31-Jan-08 9:00 
AnswerRe: How to instantiate an object with a generic parameter? Pin
Le centriste31-Jan-08 10:05
Le centriste31-Jan-08 10:05 
GeneralRe: How to instantiate an object with a generic parameter? Pin
michal.kreslik31-Jan-08 10:26
michal.kreslik31-Jan-08 10:26 
GeneralRe: How to instantiate an object with a generic parameter? Pin
Le centriste31-Jan-08 12:59
Le centriste31-Jan-08 12:59 
GeneralRe: How to instantiate an object with a generic parameter? Pin
michal.kreslik31-Jan-08 13:17
michal.kreslik31-Jan-08 13:17 
AnswerRe: How to instantiate an object with a generic parameter? Pin
darkelv31-Jan-08 14:18
darkelv31-Jan-08 14:18 
GeneralRe: How to instantiate an object with a generic parameter? Pin
michal.kreslik6-Feb-08 4:05
michal.kreslik6-Feb-08 4:05 
AnswerRe: How to instantiate an object with a generic parameter? Pin
Mark Churchill1-Feb-08 2:47
Mark Churchill1-Feb-08 2:47 
GeneralRe: How to instantiate an object with a generic parameter? Pin
michal.kreslik6-Feb-08 22:12
michal.kreslik6-Feb-08 22:12 
GeneralRe: How to instantiate an object with a generic parameter? [modified] Pin
Mark Churchill7-Feb-08 0:07
Mark Churchill7-Feb-08 0:07 
GeneralRefresh Pin
kibromg31-Jan-08 7:14
kibromg31-Jan-08 7:14 
GeneralRe: Refresh Pin
pmarfleet31-Jan-08 8:43
pmarfleet31-Jan-08 8:43 
Generalmarshaling an array of structs Pin
damianrda31-Jan-08 6:23
damianrda31-Jan-08 6:23 
GeneralDeleting Outlook Appointments Pin
NewToAspDotNet31-Jan-08 6:15
NewToAspDotNet31-Jan-08 6:15 
GeneralNesting panels Pin
DSdragondude31-Jan-08 5:29
DSdragondude31-Jan-08 5:29 
QuestionRe: Nesting panels Pin
TJoe31-Jan-08 5:38
TJoe31-Jan-08 5:38 

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.