Reflection

  • Thread starter OldButStillLearning
  • Start date
O

OldButStillLearning

I am trying to use reflection to create and instance of a class. I have done
this before on some other classes that I wanted to create an instance of, but
this particular class requires a "System.Data.DataRow" as one of the
parameters. When I attempt to create a variable with the "Type" with the
following
Type myType = Type.GetType("System.Data.DataRow") this is not picking up the
right values for other things which are required.

I located the System.Data.dll and selected properties on this in
windows explorer attempting to locate the appropriate values for Version,
Culture and PublicKeyToken. Not sure what to provided - maybe not all of
these are required for it to be successful, not sure.

Secondly, is there a way to retreive these within the code so that as the
Framework changes, these will still pick up the appropriate version, culture,
etc.


Thanks in advance for your assistance!!!!
 
A

Alberto Poblacion

OldButStillLearning said:
I am trying to use reflection to create and instance of a class
[...]
Type myType = Type.GetType("System.Data.DataRow") this is not picking up
the
right values for other things which are required.

Note that System.Data.DataRow does not have a public constructor, which
means that you should not be creating an instance of this class. Normally
you obtain a new DataRow by means of the method NewRow of the DataTable
class. Maybe this is why it "is not picking up the right values"? What
exactly are you trying to do, and what errors are you getting?
 
J

JimHeavey

Thanks Alberto for your response....

I am attempting to create and instance of a class which requires a parameter
of a System.Data.DataRow. When I use reflection to create an instance of
my class, I have to provide an array of Types which identifies the list of
the Types required by the constructor of the class which I am trying to
create, so I am merely attempting to create the array of Types for my
constructor so that I can get the handle for my class that I want to create...

Does this help?
 
M

miher

Hello,
I think i found this same question and already answered in a different
newsgroup.
I paste my answer here too, hope it helps.
Hi,

I hope i understood Your question well. So as i think You have a class like
this :
class SomeClass
{
public SomeClass(DataRow dr)
{ ... }
}

To create an instance of this class by reflection do the following :
1., get the Type representing Your class
2., get the constructor for Your type
3., invoke it

This can be achieved like this :
string typeName = //full name of the class You wish to create an
instance of
Type t = Type.GetType(typeName); // get the Type representing
Your type
// get the constructor of the type, which takes 1 DataRow as
parameter
// here You can use the typeof operator to get the Type
representing DataRow
// (assuming You referenced its assembly in Your project)
ConstructorInfo ci = t.GetConstructor(new Type[] {
typeof(DataRow) });
DataRow dr = // this will be the parameters value
// create new instance
SomeClass sc = ci.Invoke(new object[] { dr }) as SomeClass;

This is an example only. You might also want to look at other overloads of
GetConstructor.

Hope You find this useful.
-Zsolt


"JimHeavey" <[email protected]> az alábbiakat írta a
következő üzenetben
 
J

JimHeavey

No, that does not answer the question.

What is the full name? I tried "System.Data.DataRow", but that is not
explicit enough. As identified in my issue, I believe I have to supply the
"Version", "Culture", "PublicKeyToken" and AssemblyName.

What are the values which should be passed for these? Or maybe I am
completely off base, but this is what I provide for other home gown classes
that I attempt to get "Types" for. Here is an example of getting a type for
one of my home grown classes....

Type myType = Type.GetType("DALLibrary.Common.NullableDataReader" + ", " +
"DALLibrary, " + "Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");


I am looking to build the same pattern for "System.Data.DataRow"
 
M

miher

Hi,

To load the DataRow type, You can do any of these :
1., set copylocal to true on the reference to the System.Data assembly, in
this case use "System.Data.Datarow, System.Data" as typename
2., use full identity of the assembly You wish to get DataRow from. For
example :
"System.Data.DataRow, System.Data, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"
(its worth mentioning that this is the same as the AssemblyQualifiedName of
the given Type)

Hope You find this useful.
-Zsolt



"JimHeavey" <[email protected]> az alábbiakat írta a
következő üzenetben
news:[email protected]...
 

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