Activator.CreateInstance problem

G

Guest

Hi all, when trying to load and create an instance from an assembly I'm getting InvalidCastException. Here's a chunk of code

Assembly cAssembly = Assembly.LoadFrom("d:\\DAOTest.dll")
object cDAOInstance = null
foreach (Type cType in cAssembly.GetTypes()

if ((cType.IsClass == true) && (cType.FullName == "DAOTest.CustomerRowSet")

cDAOInstance = Activator.CreateInstance(cType)

// this line generates the InvalidCastExceptio
//((DAOTest.CustomerRowSet)cDAOInstance).getTraderList()

CustomerRowSet rs = new CustomerRowSet()
Type t1 = cDAOInstance.GetType()
Type t2 = rs.GetType()
Console.WriteLine(t1.ToString())
Console.WriteLine(t2.ToString())



Strangely, the result in the console for the ToString calls is

DAOTest.CustomerRowSe
DAOTest.CustomerRowSe

Any info/advice on resolving this problem ? Many thanks in advance

Chris
 
I

Ian Frawley

Hi,

Is it possible that when you try to cast to (DAOTest.CustomerRowSet) the
compiler doesn't know what one of these is so it bombs.

Ian

Chris Bamford said:
Hi all, when trying to load and create an instance from an assembly I'm
getting InvalidCastException. Here's a chunk of code:
 
G

Guest

Not sure... possibly. I can create an instance of CustomerRowSet which resides in DAOTest so I would assume, possibly invalidly, that the assembly (DAOTest) and the class (CustomerRowSet) is in scope
 
I

Ian Frawley

Just out of interest does this line:

--> //((DAOTest.CustomerRowSet)cDAOInstance).getTraderList();

Return anything?


Chris Bamford said:
Not sure... possibly. I can create an instance of CustomerRowSet which
resides in DAOTest so I would assume, possibly invalidly, that the assembly
(DAOTest) and the class (CustomerRowSet) is in scope.
 
I

Ian Frawley

I might be misreading this somewhere and if I am the I appologise but if
Console.WriteLine(t1.ToString()); Prints DAOTest.CustomerRowSet
Then can you not just go:

cDAOInstance.getTraderList();

Ian
 
G

Guest

Just checked, to be on the safe side, doesn't compile as cDAOInstance is of type object
 
I

Ian Frawley

It looks like you are having a similar problem to me. Have you debugged your
code to see if cDAOInstanceis actually an instance of something and not
undefined.
 
G

Guest

When debugging the cDAOInstance variable (type object) shows as being of typ
DAOTest.CustomerRowSet. To clarify, CustomerRowSet is an extension o
RowSet (one of my classes) which is an extension of BaseDAO (again, one of mine
which is an extension of DataTable (.NET class). I'm checking if this has an
relevance on the problem by creating a simple class heirarchy which doesn'
chain from DataTable
 
I

Ian Frawley

Try this:

Assembly cAssembly = Assembly.LoadFrom("d:\\DAOTest.dll");
foreach (Type cType in cAssembly.GetTypes())
{
if ((cType.IsClass == true) && (cType.FullName ==
"DAOTest.CustomerRowSet"))
{
DAOTest.CustomerRowSet cDAOInstance =
(DAOTest.CustomerRowSet)Activator.CreateInstance(cType);

cDAOInstance.getTraderList();
}
}

Regards

Ian
 
G

Guest

Ian, that worked a treat. Thank you very much. Just have to figure out how I
cast to the dynamic type in my generic class loader now

Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top