PropertyInfo.PropertyType Propiedad

Definición

Obtiene el tipo de esta propiedad.

public:
 abstract property Type ^ PropertyType { Type ^ get(); };
public abstract Type PropertyType { get; }
member this.PropertyType : Type
Public MustOverride ReadOnly Property PropertyType As Type

Valor de propiedad

Type

Tipo de esta propiedad.

Implementaciones

Ejemplos

En el ejemplo siguiente se define una Employee clase que tiene cinco propiedades. A continuación, usa recupera una matriz de PropertyInfo objetos que representan esas propiedades y muestra el nombre y el tipo de cada uno.

using System;
using System.Reflection;

public class Employee
{
   private string _id;

   public String FirstName { get; set; }
   public String MiddleName { get; set; }
   public String LastName  { get; set; }
   public DateTime HireDate  { get; set; }

   public String ID
   {
      get { return _id; }
      set {
         if (ID.Trim().Length != 9)
            throw new ArgumentException("The ID is invalid");
         _id = value;
      }
   }
}

public class Example
{
   public static void Main()
   {
      Type t = typeof(Employee);
      Console.WriteLine("The {0} type has the following properties: ",
                        t.Name);
      foreach (var prop in t.GetProperties())
         Console.WriteLine("   {0} ({1})", prop.Name,
                           prop.PropertyType.Name);
   }
}
// The example displays the following output:
//       The Employee type has the following properties:
//          FirstName (String)
//          MiddleName (String)
//          LastName (String)
//          HireDate (DateTime)
//          ID (String)
Imports System.Reflection

Public Class Employee
   Private _id As String

   Public Property FirstName As String = String.Empty
   Public Property MiddleName As String = String.Empty
   Public Property LastName As String = String.Empty
   Public Property HireDate As Date = Date.Today

   Public Property ID As String
      Get
         Return _id
      End Get
      Set
         If ID.Trim().Length <> 9 Then _
            Throw New ArgumentException("The ID is invalid")
         _id = value
      End Set
   End Property
End Class

Module Example
   Public Sub Main()
      Dim t As Type = GetType(Employee)
      Console.WriteLine("The {0} type has the following properties: ",
                        t.Name)
      For Each prop In t.GetProperties()
         Console.WriteLine("   {0} ({1})", prop.Name,
                           prop.PropertyType.Name)
      Next
   End Sub
End Module
' The example displays the following output:
'    The Employee type has the following properties:
'       FirstName (String)
'       MiddleName (String)
'       LastName (String)
'       HireDate (DateTime)
'       ID (String)

Comentarios

Para determinar el tipo de una propiedad determinada, haga lo siguiente:

  1. Obtiene un Type objeto que representa el tipo (la clase o estructura) que contiene la propiedad . Si está trabajando con un objeto (una instancia de un tipo), puede llamar a su GetType método . De lo contrario, puede usar el operador de C# o el operador Visual Basic GetType, como se muestra en el ejemplo.

  2. Obtenga un PropertyInfo objeto que represente la propiedad en la que está interesado. Para ello, puede obtener una matriz de todas las propiedades del Type.GetProperties método y, a continuación, iterar los elementos de la matriz, o puede recuperar el PropertyInfo objeto que representa la propiedad directamente llamando al Type.GetProperty método y especificando el nombre de la propiedad.

  3. Recupere el valor de la PropertyType propiedad del PropertyInfo objeto .

Se aplica a