Activator.CreateInstance problem

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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:
 
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
 
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 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
 
Just checked, to be on the safe side, doesn't compile as cDAOInstance is of type object
 
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.
 
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
 
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
 
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.
 
Back
Top