Re: Activator Question
- From: Jon Skeet [C# MVP] <skeet@xxxxxxxxx>
- Date: Tue, 7 Aug 2007 18:14:08 +0100
Dan Dorey <dandorey1@xxxxxxxxx> wrote:
My goal is to pass in an assembly and have the method return an
instance of any classes that inherit from the generic type T.
My problem is that I'm unable to cast from the result of
Activator.CreateInstance(curType) to T even though curType is a
concrete class of T (T is abstract in this case). I'm unsure why I
can't make this cast. In another method where I don't have the
restriction of T being a class, this approch works fine when T is an
interface.
I kind of feel like I'm going about this the wrong way, so if anyone
has suggestions on a better approach, please let me know :)
Well, instead of InheritsFrom, you'd be better off using
Type.IsAssignableFrom or Type.IsSubclassOf.
However, I don't get any compile-time errors with your code.
Could you post a short but complete program which demonstrates the
problem?
See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.
- Follow-Ups:
- Re: Activator Question
- From: Dan Dorey
- Re: Activator Question
- References:
- Activator Question
- From: Dan Dorey
- Activator Question
- Prev by Date: Activator Question
- Next by Date: Reflection for Comments?
- Previous by thread: Activator Question
- Next by thread: Re: Activator Question
- Index(es):
Relevant Pages
|