> loading a class defined in a separate assembly [...] create a system where
> different implementations of an interface I am desigining can be loaded
> at runtime using reflection [...] use the Activator.CreateInstanceFrom([...])
> [...] InvalidCastException.
Just to reassure you: this is a pretty standard way to do it. It's
sometimes called the object builder pattern, I think. If you get
casting errors, make sure your references are correct, and that you
have no redundant DLLs, or old DLLs lying around, accidentally being
used. If you load an existing assembly from disk, make sure you
specify the correct assembly details.
This is a typical scenario with three assemblies, from the top of my
head:
1. ObjectBuilder assembly references assembly which contains IClass
and assembly which contains Class
2. Client assembly references assembly which contains IClass and
assembly which contains IClass
3. Client does IClass classInstance = ObjectBuilder<IClass>.Build(),
upon which ObjectBuilder uses reflection to CreateInstance() of Class,
casting it as IClass, and returning it.
I've used a shortcut from System.Web.dll sometimes:
http://msdn2.microsoft.com/en-us/library/system.web.compilation.buildmanager_met
hods.aspx
It does iteration over assemblies and types to find a specified type,
which you might end up doing yourself otherwise, if you want a real
System.Type.
Mario Vargas - 14 Aug 2007 20:32 GMT
Thank you all for your contributions!
>> loading a class defined in a separate assembly [...] create a system
>> where
[quoted text clipped - 28 lines]
> which you might end up doing yourself otherwise, if you want a real
> System.Type.