R
Robert
I'm working on a plug-in architecture whereby my application dynamically
loads assemblies based on runtime conditions.
I plan to use Activator.CreateInstance to instantiate objects from the
dynamically loaded assemblies. Activator.CreateInstance of course returns
the created type as 'object'. I therefore need to cast that returned
'object' to the real type that I want (e.g., MyDal, and not 'object' - where
MyDal is a class).
My question (yes, I think there ARE dumb questions - and this might be
one! - but here goes):
Why does the application that calls Activator.CreateInstance need to know
_at compile time_ the specific Type to convert the 'object' to? It makes
perfect sense to me that the application would have to _somehow_ know ahead
of time the definition of the class type named MyDal in order to cast
'object' variables to MyDal.
I am just hung up on the fact that that knowledge has to come [apparently to
me] via a static reference (project reference). Doesn't Reflection provide
us with a way to accomplish this without a static reference?
Why can't we use Reflection to equip an application with the ability to cast
an 'object' to it's "useful type" (e.g., MyDal) -- so that the application
that is calling Activator.CreateInstance can itself, at compile time, _not_
know anything about the MyDal type (a class), and instead acquire that
knowledge at runtime. Ideally, the application could somehow cast 'object'
types to "real types" without having any compile-time knowledge of the "real
type."
What am I missing?
Thanks in advance.
loads assemblies based on runtime conditions.
I plan to use Activator.CreateInstance to instantiate objects from the
dynamically loaded assemblies. Activator.CreateInstance of course returns
the created type as 'object'. I therefore need to cast that returned
'object' to the real type that I want (e.g., MyDal, and not 'object' - where
MyDal is a class).
My question (yes, I think there ARE dumb questions - and this might be
one! - but here goes):
Why does the application that calls Activator.CreateInstance need to know
_at compile time_ the specific Type to convert the 'object' to? It makes
perfect sense to me that the application would have to _somehow_ know ahead
of time the definition of the class type named MyDal in order to cast
'object' variables to MyDal.
I am just hung up on the fact that that knowledge has to come [apparently to
me] via a static reference (project reference). Doesn't Reflection provide
us with a way to accomplish this without a static reference?
Why can't we use Reflection to equip an application with the ability to cast
an 'object' to it's "useful type" (e.g., MyDal) -- so that the application
that is calling Activator.CreateInstance can itself, at compile time, _not_
know anything about the MyDal type (a class), and instead acquire that
knowledge at runtime. Ideally, the application could somehow cast 'object'
types to "real types" without having any compile-time knowledge of the "real
type."
What am I missing?
Thanks in advance.