2009-09-08 5 views
1

J'ai une classe qui reçoit un objet d'autres modules inconnus et donc je dois faire une réflexion sur l'objet pour obtenir ses données, appeler ses méthodes, etc.Recherche d'un objet .NET ObjectReflector qui me permettra de simplifier la réflexion d'un objet

pour simplifier le code pour réfléchir sur l'objet, je construis cette classe:

using System; 

namespace ApplicationCore.Helpers 
{ 
    class ObjectReflector 
    { 
     public object TheObject { get; set; } 
     public Type TheType { get; set; } 

     public ObjectReflector(object theObject) 
     { 
      TheObject = theObject; 
      TheType = theObject.GetType(); 
     } 

     public string GetObjectShortName() 
     { 
      return TheObject.GetType().Name; 
     } 

     public string GetObjectLongName() 
     { 
      return TheObject.GetType().ToString(); 
     } 

     public T GetPropertyValue<T>(string propertyName) 
     { 
      return (T)TheType.GetProperty(propertyName).GetValue(TheObject, null); 
     } 

     public T GetMethodValue<T>(string methodName, object[] parameters) 
     { 
      return (T)TheType.GetMethod(methodName).Invoke(TheObject, parameters); 
     } 

    } 
} 

Alors que j'ai beau, code propre qui ressemble à ceci:

namespace ApplicationCore.Presenters 
{ 
    public class SmartFormPresenter 
    { 
     public UserControl View { get; set; } 

     public string ShortName { get; set; } 
     public string LongName { get; set; } 
     public string FirstName { get; set; } 
     public int Age { get; set; } 
     public int AgePlusTwo { get; set; } 

     public SmartFormPresenter(object o) 
     { 
      SmartFormView smartFormView = new SmartFormView(); 
      View = smartFormView; 
      smartFormView.DataContext = this; 

      ObjectReflector or = new ObjectReflector(o); 
      ShortName = or.GetObjectShortName(); 
      LongName = or.GetObjectLongName(); 
      FirstName = or.GetPropertyValue<string>("FirstName"); 
      Age = or.GetPropertyValue<int>("Age"); 
      AgePlusTwo = or.GetMethodValue<int>("GetAgeInTwoYears", null); 
     } 
    } 
} 

Mais maintenant je dois faire des méthodes, par exemple à lire pas un int mais un pour que je vais devoir obtenir un List<object> puis réfléchir sur la etc. « objet »

Je pense que cela a été fait avant . Existe-t-il un type d'outil ou de classe dans .NET appelé ObjectReflector qui va m'aider à simplifier la réflexion d'un objet comme je le fais ci-dessus?

+0

Je n'utiliserais pas les noms de propriété et de méthode directement. Utilisez plutôt les arbres d'expression linq pour les éviter. Cela rend le refactoring plus sûr. –

+0

J'ai les noms de méthodes et de propriétés dans un fichier XML sous forme de chaînes que j'ai besoin d'appeler sur mon objet inconnu. Comment cela fonctionnerait-il dans ce cas avec les arbres d'expression ling? –

+0

Si j'ai bien compris, vous voulez extraire une collection de votre objet inconnu et ensuite réfléchir sur les objets de la collection? –

Répondre

0

Impossible de poster le code dans mon commentaire. Cela peut vous aider à démarrer:

foreach (PropertyInfo pi in oProps) 
      { 
       Type colType = pi.PropertyType; 

       if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()  
       ==typeof(Nullable<>))) 
       { 
        colType = colType.GetGenericArguments()[0]; 
       } 
Questions connexes