c# - IDataParameter InvalidCastException -
the microsoft enterprise library data access application block contains following method:
private static idataparameter[] createparametercopy(dbcommand command) { idataparametercollection parameters = (idataparametercollection)command.parameters; idataparameter[] parameterarray = new idataparameter[parameters.count]; parameters.copyto(parameterarray, 0); return cachingmechanism.cloneparameters(parameterarray); }
if command.parameters
tdparametercollection (a teradata .net provider class implements idataparametercollection
), parameters.copyto(parameterarray, 0)
throws invalidcastexception: "unable cast object of type system.data.idataparameter[] type teradata.client.provider.tdparameter[]".
my first question:
how , why happening?
the exception message suggests parameters have been copied parameterarray
, there attempt afterwards convert parameterarray
idataparameter[] tdparameter[]. (in case, tdparameter implements idataparameter.)
my second question:
any ideas how fix without using concrete types?
the problem in internal code of copyto
method.
msdn says:
the type of source icollection cannot cast automatically type of destination array.
that means array must able convert without explicit conversion. otherwise method call fail.
instead of using copyto
, should use foreach
, copy them hand 1 one.
Comments
Post a Comment