Reflection - Dynamically invoking property
-
My class exposes some controls via properties. I need to invoke certain properties of these controls that are decorated with my custom attributes. So let’s take example. Here is how my class look like public partial class Form1 : Form { public Control Button1 { get { return this.btn1; } } [MyCustomAttributes] public Control Button2 { get { return this.btn2; } } } Now I want to dyanmically call Visible property to false on the Control properties that are decorated with MyCustomAttributes attribute. PropertyInfo[] PI = typeof(Form1).GetProperties(); foreach (PropertyInfo property in PI) { MyCustomAttributes myAtt = (MyCustomAttributes)Attribute.GetCustomAttribute(property, typeof(MyCustomAttributes)); if (myAtt == null) continue; property.SetValue("Visible", false, null); } But I get exception that "Property set method not found". Any ideas what I am doing wrong here?
-
My class exposes some controls via properties. I need to invoke certain properties of these controls that are decorated with my custom attributes. So let’s take example. Here is how my class look like public partial class Form1 : Form { public Control Button1 { get { return this.btn1; } } [MyCustomAttributes] public Control Button2 { get { return this.btn2; } } } Now I want to dyanmically call Visible property to false on the Control properties that are decorated with MyCustomAttributes attribute. PropertyInfo[] PI = typeof(Form1).GetProperties(); foreach (PropertyInfo property in PI) { MyCustomAttributes myAtt = (MyCustomAttributes)Attribute.GetCustomAttribute(property, typeof(MyCustomAttributes)); if (myAtt == null) continue; property.SetValue("Visible", false, null); } But I get exception that "Property set method not found". Any ideas what I am doing wrong here?
What you want to do is:
Control control = (Control)property.GetValue(obj, null);
control.Visible = false;Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
-
My class exposes some controls via properties. I need to invoke certain properties of these controls that are decorated with my custom attributes. So let’s take example. Here is how my class look like public partial class Form1 : Form { public Control Button1 { get { return this.btn1; } } [MyCustomAttributes] public Control Button2 { get { return this.btn2; } } } Now I want to dyanmically call Visible property to false on the Control properties that are decorated with MyCustomAttributes attribute. PropertyInfo[] PI = typeof(Form1).GetProperties(); foreach (PropertyInfo property in PI) { MyCustomAttributes myAtt = (MyCustomAttributes)Attribute.GetCustomAttribute(property, typeof(MyCustomAttributes)); if (myAtt == null) continue; property.SetValue("Visible", false, null); } But I get exception that "Property set method not found". Any ideas what I am doing wrong here?
imak wrote:
property.SetValue("Visible", false, null);
The property in this case is not the same as the control. "property" is actually your "get" method on Form1 to RETRIEVE the control. Try this:
Control ctl = (Control)property.GetValue(Form1, null);
ctl.Visible = false;Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
imak wrote:
property.SetValue("Visible", false, null);
The property in this case is not the same as the control. "property" is actually your "get" method on Form1 to RETRIEVE the control. Try this:
Control ctl = (Control)property.GetValue(Form1, null);
ctl.Visible = false;Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels)How dare you beat me by seconds with the same answer! :)
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
-
What you want to do is:
Control control = (Control)property.GetValue(obj, null);
control.Visible = false;Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
-
I rewrite my variable names when I post answers. Ie, I was almost going to use propertyInfo instead of using property but I figured it would be too confusing.
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
-
imak wrote:
property.SetValue("Visible", false, null);
The property in this case is not the same as the control. "property" is actually your "get" method on Form1 to RETRIEVE the control. Try this:
Control ctl = (Control)property.GetValue(Form1, null);
ctl.Visible = false;Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
What you want to do is:
Control control = (Control)property.GetValue(obj, null);
control.Visible = false;Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
-
obj is the instance of the object you are using. The one with your attributes and properties. Really, if you check the MSDN on the topic it is relatively thorough.
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
-
obj is the instance of the object you are using. The one with your attributes and properties. Really, if you check the MSDN on the topic it is relatively thorough.
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
-
I rewrite my variable names when I post answers. Ie, I was almost going to use propertyInfo instead of using property but I figured it would be too confusing.
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
-
Oh, I just looked at my post. I had no idea what you are talking about. Ask the site admins if you want to know what crazy stuff happens not me :)
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
-
Ok, well plug in the current instance of Form1... If you're running this code inside the form, it'd just be "this"... If you're running it elsewhere, then, well, I assume you have a reference to the form, so pop that in there.
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
How dare you beat me by seconds with the same answer! :)
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
And I even added a (very) brief explanation... Ha! I win the prize! :-D Wait... There's no prize? :((
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
-
That's what you get to see in a pre-tag without specifying the language;
Control control = (Control)property.GetValue(obj, null);
This is with the language set to C#;
Control control = (Control)property.GetValue(obj, null);
I are Troll :suss:
-
That's what you get to see in a pre-tag without specifying the language;
Control control = (Control)property.GetValue(obj, null);
This is with the language set to C#;
Control control = (Control)property.GetValue(obj, null);
I are Troll :suss:
It must be guessing VB instead of C... If I remember right (Been a while), in VB, you define a property as:
Public Property Something
Get
Return _localVariable
End Get
Set
_localVariable = Value
End Set
End PropertySo "Property" is blue... Dunno why it would guess that for your code line though... You figure the "null" would be a dead giveaway that it's C/C++/C#
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
Oh, I just looked at my post. I had no idea what you are talking about. Ask the site admins if you want to know what crazy stuff happens not me :)
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
You need to take some responsibility for the craziness too; a PRE or CODE tag accepts an optional LANG attrribute where you specify the language, and hence the applicable list of keywords. More information can be found here[^]. By default the language stuff is handled automatically when pasting in Q&A (based on code I provided here: PRE tags galore: LPCodeRecognizer[^]). So far Chris hasn't been willing or able to implement it also in the forums. There is a default, unfortunately it is C++ everywhere (and not C# as was documented somewhere), and independent of the actual forum. And then there are some bugs in the syntax colorizer, IIRC it is easily confused by quoted stuff. Conclusion: the best you can do is change
<PRE>
into<PRE lang="CS">
and hope for the best. :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
It must be guessing VB instead of C... If I remember right (Been a while), in VB, you define a property as:
Public Property Something
Get
Return _localVariable
End Get
Set
_localVariable = Value
End Set
End PropertySo "Property" is blue... Dunno why it would guess that for your code line though... You figure the "null" would be a dead giveaway that it's C/C++/C#
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels)There is no guessing in the forums, it is just a bad default IMO. See my reply here[^]. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.