Pagina personale di:
Carlo Vecchio
appunti di C#, R, SQL Server, ASP.NET, algoritmi, numeri
Vai ai contenuti

C# - Reflection

C#

Cos'è la Reflection

  • La Reflection è la possibilità di ispezionare i metadati di un assembly e di invocarne i metodi.
  • In pratica con la Reflection, a partire da un oggetto, si è in grado di estrarne i Costruttori, le Proprietà e i Metodi.
  • La Reflection è utilizzata dall'ambiente di sviluppo, per esempio quando si trascina un oggetto in un form e la finestra delle proprietà viene aggiornata.

Reflection per ispezionare un oggetto
  • Si consideri la seguente classe, implementata in parte.
  • Essa contiene tre proprietà, due metodi e due costruttori.

   class Persona
   {
       // Proprietà
       public string Nome { get; set; }
       public string Cognome { get; set; }
       public int Stipendio { get; set; }

       // Metodi
       public string NomeCompleto()
       {
           return Cognome + ", " + Nome;
       }
       public void AggiornaStipendio()
       {
           // TODO
       }

       // Costruttori
       public Persona()
       {
           // TODO
       }
       public Persona(string nome, string cognome)
       {
           // TODO
       }
   }

  • Inserire il namespace System.Reflection.
  • Per elencare le proprietà, i metodi e i costruttori, si può utilizzare il codice seguente.

   // Ritorna il tipo (da una classe).
   Type t1 = typeof(Persona);

   // Ritorna il tipo (da un oggetto).
   Persona p = new Persona();
   Type t2 = p.GetType();

   // Il codice seguente può essere utilizzato sia sul Type t1 che sul Type t2.

   // Proprietà
   Console.WriteLine("Elenco proprietà:");
   PropertyInfo[] properties = t1.GetProperties();
   foreach (PropertyInfo property in properties)
   {
       Console.WriteLine(property.PropertyType + " - " + property.Name);
   }

   // Metodi
   Console.WriteLine("Elenco metodi:");
   MethodInfo[] methods = t1.GetMethods();
   foreach (MethodInfo method in methods)
   {
       Console.WriteLine(method.ReturnType.Name + " - " + method.Name);
   }

   // Costruttori
   Console.WriteLine("Elenco costruttori:");
   ConstructorInfo[] constructors = t1.GetConstructors();
   foreach (ConstructorInfo constructor in constructors)
   {
       Console.WriteLine(constructor.ToString());
   }

  • L'output è il seguente:

   Elenco proprietà:
   System.String - Nome
   System.String - Cognome
   System.Int32 - Stipendio

   Elenco metodi:
   String - get_Nome
   Void - set_Nome
   String - get_Cognome
   Void - set_Cognome
   Int32 - get_Stipendio
   Void - set_Stipendio
   String - NomeCompleto
   Void - AggiornaStipendio
   String - ToString
   Boolean - Equals
   Int32 - GetHashCode
   Type - GetType

   Elenco costruttori:
   Void .ctor()
   Void .ctor(System.String, System.String)

  • Osservare che nell'elenco metodi, compaiono i get e i set, ma anche i quattro metodi ereditati dalla classe System.Object (ToString, Equals, GetHashCode, GetType).
  • Inoltre l'elenco costruttori viene ricavato con il ToString() in modo da ottenere i tipi dei parametri di input.

Reflection per eseguire un metodo
  • La Reflection può essere utilizzata anche per eseguire un metodo a partire dal suo nome.
  • Per un esempio completo si vada in: Come si fa: esempi vari, parte 2, paragrafo "Eseguire un metodo dato il nome".

© 2022 Carlo Vecchio
Torna ai contenuti