Sign in to follow this  
gellin

[.net] Set a string property to a constant at design time by using the property grid

Recommended Posts

Say I have this const string declared somewhere: public const string SomeText = "blah blah blah"; In designer mode, I would like to assign the value of this string to a field in the property grid by using the variable SomeText. Is there any way to do this?

Share this post


Link to post
Share on other sites
I'm not trying to change the value of a const. I want to assign the "blah blah blah" to a field in the property grid just by typing in "SomeText" as the value (by using reflection or whatever means necessary).

Share this post


Link to post
Share on other sites
I have an xml file that is used to codegen some constants. I want to be able to assign a property on a control to this constant so I don't ever have to go into code and manually assign it myself.

Share this post


Link to post
Share on other sites
Using reflection it's perfectly possible. It will probably look something like this (assuming 'foo' is the instance of the containing class):


Type fooType = foo.GetType();
PropertyInfo desiredProperty = fooType.GetProperty("TheDesiredProperty");
string constName = desiredProperty.GetValue(foo, BindingFlags.Public | BindingFlags.Instance).ToString();
object desiredValue = fooType.GetField(constName, BindingFlags.Public | BindingFlags.Constant).GetValue(foo);
desiredProperty.SetValue(foo, desiredValue);


Didn't test it though

Share this post


Link to post
Share on other sites
I suppose that you know that while in the properties window, your app is not running so you can't run any code, so I suppose reflexion won't help you there.
I think your best chance to "export" your string to the properties window would be to use an Attribute, but I've got no clues witch one would work.
Anyway, you'll have to recompile your code every time you change that value.

Where do you want to put that property? In the properties window of the code file that contains the "public const string SomeText = "blah blah blah";" or in the properties window of the xml file? Maybe it's some kind of content processor (custom tool or pre/post build event) that you want... you might want to look into those. I know XNA use content processors for it's assets, you can define you own processors and use the property grid to configure how your asset will be exported.

Good luck.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this