Reflection and Arrays

B

bryan.kardisco

Perhaps someone can help me with a problem I'm having in respect to
reflection.

I have a custom method that converts a DataSet into an instance of a
class that I've created called row[]

So it looks something like

Code Snippet

private row[] convertDataSets(DataSet dataSet , String tableName)

{

row[] row = null;

row = new row[10];

row[0] = new row();

row[0].ClassA = new ClassA();

}



Now, I've been able to dynamically create ClassA by doing this

Code Snippet

Type tableType =

Type.GetType("MyParentLibrary." + tableName , true);


object tableObject =

Activator.CreateInstance(tableType);


PropertyInfo pI = tableType.GetProperty(columnName);

pI.setValue(object , value , null);





But the question this this: How can I dynamically link this to row[0]

I'd like to just have something like row[0].DynamicClass =
DynamicObject.

Can this be done??
 
N

Nicholas Paldino [.NET/C# MVP]

You don't have to associate it with the row, just have a method which
will return the new instance, and then assign the value to the array at the
appropriate index.

All arrays derive from System.Array, so you can cast to that and call
the SetValue method to set the value of the element at a particular index.
 
B

Ben Voigt [C++ MVP]

Nicholas said:
You don't have to associate it with the row, just have a method
which will return the new instance, and then assign the value to the
array at the appropriate index.

All arrays derive from System.Array, so you can cast to that and
call the SetValue method to set the value of the element at a
particular index.


I think you need to know if it is a ref or value type. For value type, do
what Nicholas said.

For reference types, call System.Array.GetValue to get the reference, then
reflect against the property setter passing that instance.
Perhaps someone can help me with a problem I'm having in respect to
reflection.

I have a custom method that converts a DataSet into an instance of a
class that I've created called row[]

So it looks something like

Code Snippet

private row[] convertDataSets(DataSet dataSet , String tableName)

{

row[] row = null;

row = new row[10];

row[0] = new row();

row[0].ClassA = new ClassA();

}



Now, I've been able to dynamically create ClassA by doing this

Code Snippet

Type tableType =

Type.GetType("MyParentLibrary." + tableName , true);


object tableObject =

Activator.CreateInstance(tableType);


PropertyInfo pI = tableType.GetProperty(columnName);

pI.setValue(object , value , null);





But the question this this: How can I dynamically link this to
row[0] I'd like to just have something like row[0].DynamicClass =
DynamicObject.

Can this be done??
 
N

Nicholas Paldino [.NET/C# MVP]

I was thinking of it the other way around. Since the OP is creating a
new instance of the class for each row, it makes sense to just populate the
entire class from reflection, and ^then^ insert it into the array.

It would be easier, unless the array is pre-populated in code that is
not in the context of this method (the one that makes reflection).

But yes, if the array is pre-populated with instances, then the instance
will have to be retrieved with GetValue, and then the reflection can work on
that.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ben Voigt said:
Nicholas said:
You don't have to associate it with the row, just have a method
which will return the new instance, and then assign the value to the
array at the appropriate index.

All arrays derive from System.Array, so you can cast to that and
call the SetValue method to set the value of the element at a
particular index.


I think you need to know if it is a ref or value type. For value type, do
what Nicholas said.

For reference types, call System.Array.GetValue to get the reference, then
reflect against the property setter passing that instance.
Perhaps someone can help me with a problem I'm having in respect to
reflection.

I have a custom method that converts a DataSet into an instance of a
class that I've created called row[]

So it looks something like

Code Snippet

private row[] convertDataSets(DataSet dataSet , String tableName)

{

row[] row = null;

row = new row[10];

row[0] = new row();

row[0].ClassA = new ClassA();

}



Now, I've been able to dynamically create ClassA by doing this

Code Snippet

Type tableType =

Type.GetType("MyParentLibrary." + tableName , true);


object tableObject =

Activator.CreateInstance(tableType);


PropertyInfo pI = tableType.GetProperty(columnName);

pI.setValue(object , value , null);





But the question this this: How can I dynamically link this to
row[0] I'd like to just have something like row[0].DynamicClass =
DynamicObject.

Can this be done??
 

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