471,317 Members | 2,489 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

I need help with Reflection

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

--
Layos

Dec 22 '05 #1
5 1354
Type type = fieldInfo.ReflectedType;
ConstructorInfo ctor = type.GetConstructor(...);
object obj = ctor.Invoke(new object[n] {p1, p2, ... pn};

--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

<gl****@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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

--
Layos

Dec 22 '05 #2
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[i].Name == "mysqbprovidertest")
{
Type[] constructorArgs = { typeof(SQB.SQBDataprovider) };
Type t = f[i].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 ;)

--
Layos

Dec 22 '05 #3
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.

--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

"layos" <gl****@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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[i].Name == "mysqbprovidertest")
{
Type[] constructorArgs = { typeof(SQB.SQBDataprovider) };
Type t = f[i].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 ;)

--
Layos

Dec 22 '05 #4
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. :)

--
Layos

Dec 22 '05 #5
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.

--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

"layos" <gl****@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
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. :)

--
Layos

Dec 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Bill Borg | last post: by
1 post views Thread by John Bode | last post: by
9 posts views Thread by Kuberan Naganathan | last post: by
1 post views Thread by =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post: by
reply views Thread by Gustavo Arriola | last post: by
6 posts views Thread by Cralis | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.