Problem in Load vs LoadFrom

  • Thread starter Thread starter daveland
  • Start date Start date
D

daveland

The following fragment of code exhibits some anomolous behavior:

public IDbConnection GetConnection(string cnstr)
{
1 IDbConnection cn = null;
2 Assembly a = Assembly.Load("System.Data");
3 Assembly a = Assembly.LoadFrom("System.Data.dll");
4 cn = (IDbConnection) Activator.CreateInstance(
a.GetType("System.Data.OleDb.OleDbConnection"),
new Object[]{cnstr});
return (cn);
}

When using the code in line 2, everything works OK.
When using the code in line 3, the following error occurs at line 4:

Unhandled Exception: System.InvalidCastException: Specified cast is
not valid

If you try to leave out the cast on line 4, you get a compile time
error:

Cannot implicitly convert type 'object' to 'System.Data.IDbConnection'

Why would the use of "Load" result in a different behavior from using
"LoadFrom"?

Thanks for your help!

Dave Eland
(e-mail address removed)
 
Back
Top