By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,971 Members | 900 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,971 IT Pros & Developers. It's quick & easy.

"Dynamic Properties" - no really...

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.