Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How do I cast a string into an Object
Message
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
00832051
Message ID:
00834099
Vues:
19
>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 here. First it's not a property but a field. The default for Reflection is to bind only to public members so you need to either make the label public (not a good idea because the ASPX designer will often change it back) or explicitly override the bindingflags parameter. 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?
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform