"Dynamic Properties" - no really...

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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,

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.
 
Brad said:
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?
 
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
 
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 ) said:
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 said:
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?
 
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
 
Back
Top