Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Converting a string to an object property
Message
 
À
12/06/2008 15:22:08
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
VB 8.0
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01323589
Message ID:
01323606
Vues:
14
This message has been marked as the solution to the initial question of the thread.
>OK, for some reason this is tough for me.
>

It's tough because it's completely non-obvious <g>.

Sinec you're working with an ASP.NET app. I assumed your control references were fields, not properties.

Here's what that might look like in C#
string myProps = "FormView1.PageIndex";
string[] info = myProps.Split('.');

Type formType = this.GetType();
System.Reflection.FieldInfo formInfo = formType.GetField(info[0], System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);            
object control = formInfo.GetValue(this);

Type propertyType = control.GetType();            
System.Reflection.PropertyInfo controlInfo = propertyType.GetProperty(info[1]);
object propertyValue = controlInfo.GetValue(control, null);
This (might) be the VB.NET version - I ran it through a translator so it might be a bit weird. For instance, I think the info(0) and info(1) should probably be info(1) and info(2).
Dim myProps As String = "FormView1.PageIndex"
Dim info As String() = myProps.Split(Microsoft.VisualBasic.ChrW(46))
Dim formType As Type = Me.GetType()
Dim formInfo As System.Reflection.FieldInfo = formType.GetField(info(0), System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.Instance)
Dim control As object = formInfo.GetValue(Me)
Dim propertyType As Type = control.GetType()
Dim controlInfo As System.Reflection.PropertyInfo = propertyType.GetProperty(info(1))
Dim propertyValue As object = controlInfo.GetValue(control, Nothing)
-Paul

RCS Solutions, Inc.
Blog
Twitter
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform