Condividi tramite


PropertyInfo.CanRead Proprietà

Definizione

Ottiene un valore che indica se è possibile leggere la proprietà.

public:
 abstract property bool CanRead { bool get(); };
public abstract bool CanRead { get; }
member this.CanRead : bool
Public MustOverride ReadOnly Property CanRead As Boolean

Valore della proprietà

true se è possibile leggere la proprietà; in caso contrario, false.

Implementazioni

Esempio

Nell'esempio seguente vengono definite due proprietà. La prima proprietà è leggibile e la CanRead proprietà è true. La seconda proprietà non è leggibile (non è disponibile una funzione di accesso) e la CanRead proprietà è false.

using namespace System;
using namespace System::Reflection;

// Define one readable property and one not readable.
public ref class Mypropertya
{
private:
   String^ caption;

public:
   Mypropertya()
      : caption( "A Default caption" )
   {}


   property String^ Caption 
   {
      String^ get()
      {
         return caption;
      }

      void set( String^ value )
      {
         if ( caption != value )
         {
            caption = value;
         }
      }

   }

};

public ref class Mypropertyb
{
private:
   String^ caption;

public:
   Mypropertyb()
      : caption( "B Default caption" )
   {}


   property String^ Caption 
   {
      void set( String^ value )
      {
         if ( caption != value )
         {
            caption = value;
         }
      }

   }

};

int main()
{
   Console::WriteLine( "\nReflection.PropertyInfo" );
   
   // Define two properties.
   Mypropertya^ mypropertya = gcnew Mypropertya;
   Mypropertyb^ mypropertyb = gcnew Mypropertyb;
   Console::Write( "\nMypropertya->Caption = {0}", mypropertya->Caption );
   
   // Mypropertyb.Caption cannot be read, because
   // there is no get accessor.
   // Get the type and PropertyInfo.
   Type^ MyTypea = Type::GetType( "Mypropertya" );
   PropertyInfo^ Mypropertyinfoa = MyTypea->GetProperty( "Caption" );
   Type^ MyTypeb = Type::GetType( "Mypropertyb" );
   PropertyInfo^ Mypropertyinfob = MyTypeb->GetProperty( "Caption" );
   
   // Get and display the CanRead property.
   Console::Write( "\nCanRead a - {0}", Mypropertyinfoa->CanRead );
   Console::Write( "\nCanRead b - {0}", Mypropertyinfob->CanRead );
   return 0;
}
using System;
using System.Reflection;

// Define one readable property and one not readable.
public class Mypropertya
{
    private string caption = "A Default caption";
    public string Caption
    {
        get{return caption;}
        set {if(caption!=value) {caption = value;}
        }
    }
}
public class Mypropertyb
{
    private string caption = "B Default caption";
    public string Caption
    {
        set{if(caption!=value) {caption = value;}
        }
    }
}

class Mypropertyinfo
{
    public static int Main()
    {
        Console.WriteLine("\nReflection.PropertyInfo");

        // Define two properties.
        Mypropertya Mypropertya = new Mypropertya();
        Mypropertyb Mypropertyb = new Mypropertyb();

        Console.Write("\nMypropertya.Caption = " + Mypropertya.Caption);
        // Mypropertyb.Caption cannot be read, because
        // there is no get accessor.

        // Get the type and PropertyInfo.
        Type MyTypea = Type.GetType("Mypropertya");
        PropertyInfo Mypropertyinfoa = MyTypea.GetProperty("Caption");
        Type MyTypeb = Type.GetType("Mypropertyb");
        PropertyInfo Mypropertyinfob = MyTypeb.GetProperty("Caption");

        // Get and display the CanRead property.
        Console.Write("\nCanRead a - " + Mypropertyinfoa.CanRead);
        Console.Write("\nCanRead b - " + Mypropertyinfob.CanRead);

        return 0;
    }
}
Imports System.Reflection

' Define one readable property and one not readable.
Public Class Mypropertya
    Private myCaption As String = "A Default caption"

    Public Property Caption() As String
        Get
            Return myCaption
        End Get
        Set(ByVal Value As String)
            If myCaption <> value Then
                myCaption = value
            End If
        End Set
    End Property
End Class

Public Class Mypropertyb
    Private myCaption As String = "B Default caption"

    Public WriteOnly Property Caption() As String
        Set(ByVal Value As String)
            If myCaption <> value Then
                myCaption = value
            End If
        End Set
    End Property
End Class

Class Mypropertyinfo

    Public Shared Function Main() As Integer
        Console.WriteLine(ControlChars.CrLf & "Reflection.PropertyInfo")

        ' Define two properties.
        Dim Mypropertya As New Mypropertya()
        Dim Mypropertyb As New Mypropertyb()

        Console.Write(ControlChars.Cr & "Mypropertya.Caption = " & _
           Mypropertya.Caption)
        ' Mypropertyb.Caption cannot be read because
        ' there is no get accessor.
        ' Get the type and PropertyInfo.
        Dim MyTypea As Type = Type.GetType("Mypropertya")
        Dim Mypropertyinfoa As PropertyInfo = MyTypea.GetProperty("Caption")
        Dim MyTypeb As Type = Type.GetType("Mypropertyb")
        Dim Mypropertyinfob As PropertyInfo = MyTypeb.GetProperty("Caption")

        ' Get and display the CanRead property.
        Console.Write(ControlChars.CrLf & "CanRead a - " & _
           Mypropertyinfoa.CanRead)

        Console.Write(ControlChars.CrLf & "CanRead b - " & _
           Mypropertyinfob.CanRead)

        Return 0
    End Function
End Class

Commenti

Se la proprietà non dispone di una get funzione di accesso, non può essere letto.

Per ottenere la proprietà, ottenere prima di CanRead tutto la classe Type. Dall'oggetto , ottenere l'oggetto TypePropertyInfo. Dall'oggetto PropertyInfo, ottenere il CanRead valore.

Si applica a