First Time Using the ConstructorInfo

G

Guest

I'm trying to figure out how to use the ConstructorInfo class to get the
appropriate constructor and then use the "Invoke" method to create and
instance of the class. Seems easy enough, but it is not working for me, so
it must be something simple.

Here is the code that I am using, and the last instruction fails.

NullableDataReader dr = cmFrom.Data_Reader;
object[] argumentItems = { dr };
string loc = "Common." + "NullableDataReader" + ", " + _AssemblyName + ",
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
Type drType = Type.GetType(loc);
Type[] parms = { drType };
Type theTblType = Type.GetType(_TableClassName);
ConstructorInfo methodCreate = theTblType.GetConstructor(parms);
object rec = methodCreate.Invoke(theTblType, argumentItems);

When the last instruction executes, I get a "TargetException" - Object does
not meet the target type.

When I look at the "theTblType" and the "drType" in the debugger, it shows
me an appropriate value. The "dr" has an appropriate reference to the
"NullableDataReader".

Below is the constructor for that class, which takes as an arguement
NullableDataReader, which is what I believe I am passing
into the Invoke method.

public PS_GL_ACCOUNT_TBL(NullableDataReader row)

What Am I doing wrong?

Second question, the Invoke method returns an "object", but how can I "cast"
this to a "PS_GL_ACCOUNT_TBL" type when all I have is the text name of that
class?

Thanks in advance for your assistance!!
 
B

bruce barker

try:

object rec = methodCreate.Invoke(argumentItems);


as for the cast, if your code knows the type, why use the string name?
if it doesn't, how does it know the methods or what to do with it? a
common solution with is to define an known interface and cast the object
to the interface.

-- bruce (sqlwork.com)
 

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

Top