By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,458 Members | 1,278 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,458 IT Pros & Developers. It's quick & easy.

Difference Between assembly.CreateInstance and System.Activator.CreateInstance

P: n/a
Hi,
I have a fairly simeple question. What are the differences between
Assembly.CreateInstance and System.Activator.CreateInstance? I had read that
one maps to the other, however when I use Assembly.CreateInstance to create
an object that implements an interface it cannot cast to the appropriate
interface type before returning. e.g.

ISubmit lo_MessagingObject=null;

string
l_MessagingType=ConfigurationSettings.AppSettings. Get("messagingtype");

Assembly assembly = Assembly.LoadFrom
(ConfigurationSettings.AppSettings.Get("assemblylo cationmessaging"));

// Get all Types available in the assembly in an array

Type[] typeArray = assembly.GetTypes ();

// Walk through each Type and list their Information

foreach (Type type in typeArray)

{

if (type.FullName.CompareTo(l_MessagingType)==0)

{

lo_MessagingObject = (ISubmit)assembly.CreateInstance(l_MessagingType);

}

}

l_MessagingType is a class that implements the ISubmitI Interface.

Fails saying the type is an invalid cast, however the same object type below
casts without any problem. Is this the correct behaviour??

System.Type oType = System.Type.GetType(l_MessagingType);

//create an instance and type cast it to our interface

lo_MessagingObject = (ISubmit)System.Activator.CreateInstance(oType);

thanks


Nov 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
John Jenkins <jo**********@yahoo.com> wrote:
I have a fairly simeple question. What are the differences between
Assembly.CreateInstance and System.Activator.CreateInstance? I had read that
one maps to the other, however when I use Assembly.CreateInstance to create
an object that implements an interface it cannot cast to the appropriate
interface type before returning.


That usually occurs when the interface occurs in two assemblies, as per
http://www.pobox.com/~skeet/csharp/plugin.html

Might that be the problem in your case?

Type and assembly loading and linking is one of those areas I never
feel I understand well enough, to be honest...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.