Click here to Skip to main content
15,891,906 members
Home / Discussions / C#
   

C#

 
GeneralRe: About Reflecting and generics Pin
jschell14-Feb-12 13:40
jschell14-Feb-12 13:40 
AnswerRe: About Reflecting and generics Pin
AspDotNetDev14-Feb-12 13:00
protectorAspDotNetDev14-Feb-12 13:00 
GeneralRe: About Reflecting and generics Pin
PozzaVecia14-Feb-12 21:20
PozzaVecia14-Feb-12 21:20 
AnswerRe: About Reflecting and generics Pin
AspDotNetDev14-Feb-12 21:49
protectorAspDotNetDev14-Feb-12 21:49 
GeneralRe: About Reflecting and generics Pin
PozzaVecia14-Feb-12 22:26
PozzaVecia14-Feb-12 22:26 
GeneralRe: About Reflecting and generics Pin
AspDotNetDev15-Feb-12 7:09
protectorAspDotNetDev15-Feb-12 7:09 
GeneralRe: About Reflecting and generics Pin
AspDotNetDev14-Feb-12 21:52
protectorAspDotNetDev14-Feb-12 21:52 
AnswerRe: About Reflecting and generics Pin
BobJanova14-Feb-12 23:20
BobJanova14-Feb-12 23:20 
I can almost guarantee that you don't want to do what you think you do. If you create a type via reflection, you can't use it in a normal compile-time way because the type is not known until runtime. That means you need to do everything about it through reflection, which is slow.

I still don't have a good understanding of what exactly you're trying to do, but generally you want to use a List<BaseClass> or List<Interface>, where all the classes you're picking between at runtime inherit from/implement the class/interface in question. Then you can use compile time method binding (i.e. write normal code) against the class/interface, and use the magic of OO (polymorphism) to get different functionality.

In this case it appears you already have the base classes, and in fact you just want the (compile time) class MyClass to be written in terms of Car and Moto. (I hope that MyClass is a dummy name! Make sure your classes have meaningful names.) I'm not even sure you need generics here. Remember that if you have

class MyClass {
 void SomeMethod(Car car, Moto moto){ ... }
}


... you can pass any instance of a subclass of Car and/or Moto to it. The same applies if you're using a generic class at some point, for example if your implementation includes a List<Car> – you can still put an AudiA8 into that list, as long as that class inherits from Car, even if the class is loaded at runtime.

I suspect what you actually want is something like
class MyClass {
 public Car Car { get; set; }
 public Moto Moto { get; set; }

 ...
}

... and then to set the properties to instances of subclasses of Car and Moto as chosen at runtime.
GeneralRe: About Reflecting and generics Pin
PozzaVecia14-Feb-12 23:29
PozzaVecia14-Feb-12 23:29 
GeneralRe: About Reflecting and generics Pin
BobJanova15-Feb-12 0:50
BobJanova15-Feb-12 0:50 
GeneralRe: About Reflecting and generics Pin
PozzaVecia15-Feb-12 1:24
PozzaVecia15-Feb-12 1:24 
GeneralRe: About Reflecting and generics Pin
BobJanova15-Feb-12 1:59
BobJanova15-Feb-12 1:59 
GeneralRe: About Reflecting and generics Pin
PozzaVecia15-Feb-12 2:30
PozzaVecia15-Feb-12 2:30 
GeneralRe: About Reflecting and generics Pin
BobJanova15-Feb-12 2:37
BobJanova15-Feb-12 2:37 
GeneralRe: About Reflecting and generics Pin
GParkings15-Feb-12 2:52
GParkings15-Feb-12 2:52 
GeneralRe: About Reflecting and generics Pin
GParkings15-Feb-12 2:54
GParkings15-Feb-12 2:54 
GeneralRe: About Reflecting and generics Pin
PozzaVecia15-Feb-12 3:09
PozzaVecia15-Feb-12 3:09 
GeneralRe: About Reflecting and generics Pin
GParkings15-Feb-12 3:25
GParkings15-Feb-12 3:25 
GeneralRe: About Reflecting and generics Pin
BobJanova15-Feb-12 3:40
BobJanova15-Feb-12 3:40 
GeneralRe: About Reflecting and generics Pin
PozzaVecia15-Feb-12 10:17
PozzaVecia15-Feb-12 10:17 
GeneralRe: About Reflecting and generics Pin
BobJanova15-Feb-12 21:57
BobJanova15-Feb-12 21:57 
GeneralRe: About Reflecting and generics Pin
PozzaVecia16-Feb-12 0:02
PozzaVecia16-Feb-12 0:02 
GeneralRe: About Reflecting and generics Pin
GParkings15-Feb-12 22:54
GParkings15-Feb-12 22:54 
GeneralRe: About Reflecting and generics Pin
PozzaVecia16-Feb-12 0:05
PozzaVecia16-Feb-12 0:05 
QuestionTeam Project Pin
tryharder14-Feb-12 10:23
tryharder14-Feb-12 10:23 

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.