Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
String to reference static object
Message
De
15/12/2009 10:19:20
 
 
À
15/12/2009 10:00:14
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 3.0
Divers
Thread ID:
01439141
Message ID:
01439157
Vues:
31
>>>I think I'm having a pre-lunch mental block. Simple case: Class:
>>>public class LLCommands
>>>{
>>>      private static RoutedUICommand lockDown;
>>>
>>>      static LLCommands()
>>>      { lockDown = new RoutedUICommand("LockDown","LockDown",typeof(LLCommands));   }
>>>
>>>      public static RoutedUICommand LockDown
>>>      {  get {return lockDown; } }
>>>}
elsewhere:
List<RoutedUICommands>() SupportedCommands;
>>>SupportedCommands.Add(LLCommands.LockDown);
So far, so good. But how can I achieve the add given only a string (e.g "LLCommands.LockDown") ?
>>
>>Sorry - can't test - much to my regret
>>
>>That would be reflection I think
>
>This works (but maybe there's a shorter way?):
Type t = typeof(LLCommands);
>            PropertyInfo p =  t.GetProperty("LockDown", BindingFlags.Public | BindingFlags.Static);
>            RoutedUICommand r = (RoutedUICommand) p.GetValue(LLCommands.LockDown,null);
>            SupportedCommands.Add(r);
>obviously needs a bit of exception handling tho.....

That was basically what I was thinking

Exception Handling : Yes
You could write a 'TryEval' method - cons (1) a lot slower and (2) compiler cannot catch (your) typing errors
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform