473,385 Members | 1,309 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Difference Between assembly.CreateInstance and System.Activator.CreateInstance

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
1 15261
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mrudul Sadanandan | last post by:
H I am trying to create object in the current app domain using CreateInstanceAndUnwrap. It is throwing the exception. Any ide ...
6
by: Pablo | last post by:
Hi, I'm creating an AppDomain and creating an instance of a class. This works fine unless I use caching, which I need to use. The exception that I get is of type "System.IO.FileLoadException"...
1
by: Daniel | last post by:
how do i access methods when the .net assembly was loaded with late biding? class Class1 { public static void Main() { System.Reflection.Assembly SampleAssembly; SampleAssembly =...
7
by: Osvaldo Bisignano | last post by:
I'm using Assembly.CreateInstance (3rd overload) to create an instance of one of the types included on that dll. But what the hell are the bindingflags? I get this exception: ...
2
by: aallee83 | last post by:
i'm new in asp.net after develop my solution i copied it on the server where I want it to run but something cares... WHAT?!?! thank you in advance File or assembly name System, or one of its...
0
by: Rui Macdonald | last post by:
I working with some samples from angGoGo PhotoControl and when I start it I receive always the following message, can you help me please? :-( -------------------- The located assembly's manifest...
8
by: Dan Holmes | last post by:
Isn't CreateInstance(typeof(int)) the same as CreateInstance<int>()? i don't understand how this method helps anything. dan
2
by: rjjaw | last post by:
Here is what I have but it isn't working if someone could point me in the right direction I would greatly appreciate it. Try Dim objAssembly As Reflection.Assembly...
2
by: christopher.watford | last post by:
I'm loading a plugin assembly using Activator.CreateInstanceFrom, and inside this assembly is a settings class which gets serialized to XML. The general code flow is as follows: ObjectHandle...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.