Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / December 2005

Tip: Looking for answers? Try searching our database.

I need help with Reflection

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
glayos@gmail.com - 22 Dec 2005 08:26 GMT
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

Signature

Layos

Vadym Stetsyak - 22 Dec 2005 09:04 GMT
Type  type = fieldInfo.ReflectedType;
ConstructorInfo ctor = type.GetConstructor(...);
object obj = ctor.Invoke(new object[n] {p1, p2, ... pn};

Signature

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

> Hi all, i have a question about the framework reflection.
> I have a project with some pages and a component class file. Inside
[quoted text clipped - 8 lines]
> Please forgive my english.
> TIA
layos - 22 Dec 2005 10:17 GMT
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 ;)

Signature

Layos

Vadym Stetsyak - 22 Dec 2005 11:47 GMT
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.

Signature

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

> Hi Vadym, thank you very much, I'm near the solution, but i have two
> questions.
[quoted text clipped - 34 lines]
>
> Thank you very much, I owe your beer ;)
layos - 22 Dec 2005 13:32 GMT
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. :)

Signature

Layos

Vadym Stetsyak - 22 Dec 2005 14:07 GMT
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.

Signature

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

> Thank you again :)
>
[quoted text clipped - 11 lines]
>
> Many thanks again. :)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.