I need help with Reflection

G

glayos

Hi all, i have a question about the framework reflection.
I have a project with some pages and a component class file. Inside
this file I've placed some custom objects.
Using the reflection and the Fieldinfo[] class i can look inside my
component and know names and properties of this objects.
My require is to recreate this objects using the informations contained
in the Fieldinfo[] depending by a selection (in my pages i use the
Fieldinfo[].Name to populate a dropdown list).
Can someone help me to do this operation?

Please forgive my english.
TIA
 
V

Vadym Stetsyak

Type type = fieldInfo.ReflectedType;
ConstructorInfo ctor = type.GetConstructor(...);
object obj = ctor.Invoke(new object[n] {p1, p2, ... pn};
 
L

layos

Hi Vadym, thank you very much, I'm near the solution, but i have two
questions.
First, fieldinfo[n].ReflectedType match the Type of the Component Class
and not of the Object inside. I try to use fieldinfo[n].FieldType and
it appears to be correct. Is it right?
The second question is: my objects inside the component class have some
properties like "connection string", "query", "parameters" but the
object created at runtime does not appear to mantain them.

Where is my mistake?

FieldInfo[] f;
Type SqbqueriesType = typeof(sqbfilters);
f = SqbqueriesType.GetFields(BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.Public);

DataSet ds = new DataSet();
SQB.SQBDataprovider sqbdp;

for(int i = 0; i < f.Length; i++)
{
if(f.Name == "mysqbprovidertest")
{
Type[] constructorArgs = { typeof(SQB.SQBDataprovider) };
Type t = f.FieldType;
ConstructorInfo c =
t.GetConstructor(System.Type.EmptyTypes);
Object[] parameter = {};
Object o = c.Invoke(parameter);
sqbdp = o as SQB.SQBDataprovider;
sqbdp.Fill(ds);
}
}

"sqbfilter" is the name of Component Class where i instanciate the
SQB.SQBDataprovider objects.

Thank you very much, I owe your beer ;)
 
V

Vadym Stetsyak

Yes, you're right FieldType is what you need...

There are no values in the runtime object, because you didn't set them. This
can be done via constructor ( you specify parameters for the constructor,
also this assumes that you have appropriate constructor with the same
parameter list).

calling with Object[] parameter = {}; is equivalent to
SQB.SQBDataprovider obj = new SQB.SQBDataprovider();

and Object[] par = new object[2] {"connstr", "other"};
is equivalent to
SQB.SQBDataprovider obj = new SQB.SQBDataprovider("connstr", "other");

Another way is to set them manually
sqbdp = o as SQB.SQBDataprovider;
sqbdp.ConnectionString = "somestring";
etc.

Also after sqbdp = o as SQB.SQBDataprovider;
you do not check for null, may result in NullRef Exception.
 
L

layos

Thank you again :)

My requirement is exactly at this node. I'will create a Component Class
in wich my customers drag and drop only a SQBDataprovider object,
configure on it all parameters, and the pages automatically recreate
colones of this object at run time without other configuration and this
clones take them back well formed DataSets with data and structure.
Without any code written by customers.
The problem is, how can i select the right properties in the right
instance of dataprovider knowing the selection on the dropdown list?
Can i put the properties in the Fieldinfo[] array?

Is the right way in your opinion?

Many thanks again. :)
 
V

Vadym Stetsyak

If the code is executed in the runtime, that you will have to introduce some
persistance model ( save properties somewhere e.g. databse ) and when
instantiating you will retrieve them from database.

If it will be a component then it can generate code that will intialize it
with the settings input by users, no reflection.
 

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