Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How do I cast a string into an Object
Message
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
00832051
Message ID:
00834101
Views:
31
>Sorry to get back on this but I just tried this code:
>
Label x = new Label();
>this.GetType().GetProperty("label1").GetValue(this,null);
>MessageBox.Show(x.Text);
>
>but I received the 'System.NullReferenceException' exception. Any ideas?
>

Couple of issues. You need to use GetField() not GetProperty() since it's not a property of the form. Second by default labels are protected and the default binding is only to public members. You can either make the label public (bad idea) or alternately specify the binding flags to make ti work. This works:
Label loLabel = (Label) this.GetType().GetField("lblWelcome",BindingFlags.NonPublic | BindingFlags.Public |BindingFlags.Instance).GetValue(this);
+++ RIck ---

>>>HI Rick.
>>>
>>>Do you know if it is working using Windows forms too?
>>
>>Sure. Reflection works on any type.
+++ Rick ---

West Wind Technologies
Maui, Hawaii

west-wind.com/
West Wind Message Board
Rick's Web Log
Markdown Monster
---
Making waves on the Web

Where do you want to surf today?
Previous
Reply
Map
View

Click here to load this message in the networking platform