473,412 Members | 2,294 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,412 software developers and data experts.

"Dynamic Properties" - no really...

All samples related to this see to come short of being 'truly' dynamic. For
instance, after creating all the code to load/save a properties value, you
turn around and save it to one you KNOW exists:

foo.Height = (my loaded value)...

I am trying to load and set the property on the fly - without necessarily
knowing what it is. Imagine a 100 name/value pairs in a file (config, res,
txt, whatever). I want to load and set those on the fly, without knowing
about the properties that exist (dynamic) or don't (exception handling). For
instance:

MyForm.Controls["myctrlname"]["mypropname"] = myvalue;

....where all three "unknowns" are loaded on the fly. I know C++ couldn't do
this but scripting languages can. Given that this is an intermediate type
language, is this possible?

--
Brad Eck
<a href="http://www.sitesdynamic.com">SitesDynamic</a>
<a href="http://www.basketsetcetera.com">Baskets Etcetera</a>
Nov 17 '05 #1
5 1728
Brad,

Yes, this is very easy to do using reflection. Basically, you will get
the Type instance for the instance you want to create. Once you create the
instance, you will get the PropertyInfo for the appropriate property to
get/set. With that, you can then get/set the property on a specific
instance.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brad" <br******@sitesdynamic.com> wrote in message
news:8E**********************************@microsof t.com...
All samples related to this see to come short of being 'truly' dynamic.
For
instance, after creating all the code to load/save a properties value, you
turn around and save it to one you KNOW exists:

foo.Height = (my loaded value)...

I am trying to load and set the property on the fly - without necessarily
knowing what it is. Imagine a 100 name/value pairs in a file (config, res,
txt, whatever). I want to load and set those on the fly, without knowing
about the properties that exist (dynamic) or don't (exception handling).
For
instance:

MyForm.Controls["myctrlname"]["mypropname"] = myvalue;

...where all three "unknowns" are loaded on the fly. I know C++ couldn't
do
this but scripting languages can. Given that this is an intermediate type
language, is this possible?

--
Brad Eck
<a href="http://www.sitesdynamic.com">SitesDynamic</a>
<a href="http://www.basketsetcetera.com">Baskets Etcetera</a>

Nov 17 '05 #2
Brad <br******@sitesdynamic.com> wrote:
All samples related to this see to come short of being 'truly' dynamic. For
instance, after creating all the code to load/save a properties value, you
turn around and save it to one you KNOW exists:

foo.Height = (my loaded value)...

I am trying to load and set the property on the fly - without necessarily
knowing what it is. Imagine a 100 name/value pairs in a file (config, res,
txt, whatever). I want to load and set those on the fly, without knowing
about the properties that exist (dynamic) or don't (exception handling). For
instance:

MyForm.Controls["myctrlname"]["mypropname"] = myvalue;

...where all three "unknowns" are loaded on the fly. I know C++ couldn't do
this but scripting languages can. Given that this is an intermediate type
language, is this possible?


Absolutely. You can use reflection to find the property with the
appropriate name, and then set it to the new value. Which bit are you
having trouble with?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3
Hi,

You have two things involved here, one is accesing a property, which would
be "mypropname" , you can access it using reflection like this:

objVar.GetType().GetProperty( "mypropname" ).SetValue( objVar, myvalue );
Another thing is accesing a indexed property , in this case is the Controls
collection, in this case you have to use a similar construction as above but
pass the index.

the important thing is that you need to differentiate between both cases. as
they use different set of parameters.Other than that I see no problem

OTOH I would bet that you can do the same with managed C++ as this is part
of the framework, you can do it with any .net language

"Brad" <br******@sitesdynamic.com> wrote in message
news:8E**********************************@microsof t.com...
All samples related to this see to come short of being 'truly' dynamic.
For
instance, after creating all the code to load/save a properties value, you
turn around and save it to one you KNOW exists:

foo.Height = (my loaded value)...

I am trying to load and set the property on the fly - without necessarily
knowing what it is. Imagine a 100 name/value pairs in a file (config, res,
txt, whatever). I want to load and set those on the fly, without knowing
about the properties that exist (dynamic) or don't (exception handling).
For
instance:

MyForm.Controls["myctrlname"]["mypropname"] = myvalue;

...where all three "unknowns" are loaded on the fly. I know C++ couldn't
do
this but scripting languages can. Given that this is an intermediate type
language, is this possible?

--
Brad Eck
<a href="http://www.sitesdynamic.com">SitesDynamic</a>
<a href="http://www.basketsetcetera.com">Baskets Etcetera</a>

Nov 17 '05 #4
Hi,

The code below has error, there is no SetValue with that set of parameters,
but it needs an extra one, for handle the indexed properties, with an non
indexed property using null as the third parameter:

objVar.GetType().GetProperty( "mypropname" ).SetValue( objVar, myvalue ,
null );

so you use the same method call you just have to pass either null or a
object[] depending if it;s an indexed property or not.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:uM*************@TK2MSFTNGP10.phx.gbl...
Hi,

You have two things involved here, one is accesing a property, which
would be "mypropname" , you can access it using reflection like this:

objVar.GetType().GetProperty( "mypropname" ).SetValue( objVar,
myvalue );
Another thing is accesing a indexed property , in this case is the
Controls collection, in this case you have to use a similar construction
as above but pass the index.

the important thing is that you need to differentiate between both cases.
as they use different set of parameters.Other than that I see no problem

OTOH I would bet that you can do the same with managed C++ as this is part
of the framework, you can do it with any .net language

"Brad" <br******@sitesdynamic.com> wrote in message
news:8E**********************************@microsof t.com...
All samples related to this see to come short of being 'truly' dynamic.
For
instance, after creating all the code to load/save a properties value,
you
turn around and save it to one you KNOW exists:

foo.Height = (my loaded value)...

I am trying to load and set the property on the fly - without necessarily
knowing what it is. Imagine a 100 name/value pairs in a file (config,
res,
txt, whatever). I want to load and set those on the fly, without knowing
about the properties that exist (dynamic) or don't (exception handling).
For
instance:

MyForm.Controls["myctrlname"]["mypropname"] = myvalue;

...where all three "unknowns" are loaded on the fly. I know C++ couldn't
do
this but scripting languages can. Given that this is an intermediate type
language, is this possible?

--
Brad Eck
<a href="http://www.sitesdynamic.com">SitesDynamic</a>
<a href="http://www.basketsetcetera.com">Baskets Etcetera</a>


Nov 17 '05 #5
OK, I am really close but the examples for this that I found are still
showing a couple probs. Most importantly, I do a GetProperty() with the
name and get valid info but when I call InvokeMember() with the GetField
or GetProperty (not sure which to use) it fails as not finding it ???

Type tmpType = currentCtrl.GetType();
PropertyInfo pi = tmpType.GetProperty(resource.propName);
if (!pi.CanWrite) // writable?
continue;
tmpType.InvokeMember(resource.propName,
BindingFlags.SetField, null, currentCtrl,
new object [] {resource.propValue});

HELP!!!

Brad Eck
http://www.sitesdynamic.com
http://www.basketsetcetera.com

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jordi Vilar | last post by:
Hi All, Is there a way to dynamic_cast a pointer to a type defined by a type_info*? something like: type_info *info_of_type_to_cast = typeid(type_to_cast); type_to_cast *casted =...
1
by: Razzbar | last post by:
I'm creating a site that is all on one page. The navigation of the site is enabled by a hidden iframe that fetches content from the server and re/places it in a div on the page. The problem is...
7
by: GfxGuy | last post by:
I've seen this problem posted a million times, but I've read through all of them and can't figure out what I'm doing wrong. Simple example (this is the whole file, no editing): ---------- ...
0
by: raca | last post by:
I am trying to create a generic SOA ServiceInvoker that will accept an XML string that will be used to deserialize an object generated by XSDObjectGen. The hierarchy goes like this:...
1
by: Phill. W | last post by:
I have a number of properties on a UserControl that are logically related to one another and do a specific job. What I'd /like/ to do is to have these properties available in the Forms Designer,...
1
by: DiegoMx | last post by:
I'm having a problem with "a:visited" properties overriding "a:hover" ones - here's the relevant part of my CSS: a.barra { position:absolute; top:30px; font-family:century gothic;...
3
by: EYIII | last post by:
Is it possible to retrieve the "created by" identity and "modified by" identity for a file (e.g. word doc, .pdf, report, etc) using .NET?
0
by: walterbyrd | last post by:
This according to SDTimes: http://www.sdtimes.com/article/story-20071215-13.html They don't specifically mention Python. But, I think Python qualifies as a dynamic language. "1. Dynamic...
17
by: David C. Ullrich | last post by:
Having a hard time phrasing this in the form of a question... The other day I saw a thread where someone asked about overrideable properties and nobody offered the advice that properties are...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.