473,386 Members | 1,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 1429
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Raveendra M | last post by:
Hi! I am working with ASP.NET application. In my page I am creating one Application Domain and in that domain I am calling my DLL. Using the methods of the dll. And unloading the Application...
3
by: Sky Sigal | last post by:
I coming unglued... really need some help. 3 days chasing my tail all over MSDN's documentation ...and I'm getting nowhere. I have a problem with TypeConverters and storage of expandableobjects...
4
by: Bill Borg | last post by:
Hello, I've got a simple shared property, e.g. Public Class dbObject Private Const m_ID As String = "ID" Public Shared ReadOnly Property ID() As String Get Return m_ID End Get End Property
5
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public...
1
by: John Bode | last post by:
I need a way to fake reflection in C++ code that makes as few assumptions about the data types involved as possible. I suspect there is no good answer for what I need to do, but I'll present the...
9
by: Kuberan Naganathan | last post by:
Hello all Does anyone know of a good way to use reflection in c++? I don't mean simply using rtti or dynamic casting. I'm talking about java/c# style reflection where an actual instance of...
0
by: avukovi4 | last post by:
I've created rectangular coordinate system and a triangle movie clip and two buttons by which would like to be able to create a reflected image of the triangle movie clip according to x or y axis....
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
0
by: Gustavo Arriola | last post by:
Hola a todos! Estoy intentando ejecutar un método usando Reflection. El código es el siguiente: public static void SoapHandler(Exception Error) { try { Type assemblyType;
6
by: Cralis | last post by:
Hi guys, Someone once said, 'You can do that with reflection'. I can't recall what it was I was trying to do at the time, but then he said, 'Any developer knows what reflection is...'. I kept...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.