次の方法で共有


PropertyInfo.CanRead プロパティ

プロパティを読み取ることができるかどうかを示す値を取得します。

Public MustOverride ReadOnly Property CanRead As Boolean
[C#]
public abstract bool CanRead {get;}
[C++]
public: __property virtual bool get_CanRead() = 0;
[JScript]
public abstract function get CanRead() : Boolean;

プロパティ値

このプロパティを読み取ることができる場合は true 。それ以外の場合は false

解説

プロパティに get アクセサがない場合は、読み取ることができません。

CanRead プロパティを取得するには、最初に Type クラスを取得します。そして、その Type から PropertyInfo を取得します。 PropertyInfo から CanRead 値を取得します。

使用例

2 つのプロパティを定義する例を次に示します。最初のプロパティは読み取り可能で、 CanRead プロパティは true です。2 番目のプロパティは読み取り不可で (get アクセサなし)、 CanRead プロパティは false です。

 
Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

' 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

[C#] 
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;
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Reflection;

// Define one readable property and one not readable.
public __gc class Mypropertya
{
private:
    String* caption;
public:
    Mypropertya() : caption(S"A Default caption") {}
    __property String* get_Caption() {return caption;}
    __property void set_Caption(String* value) {
        if(caption!=value) {
            caption = value;
        }
    }
};
public __gc class Mypropertyb
{
private:
    String* caption;
public:
    Mypropertyb() : caption(S"B Default caption") {}
    __property void set_Caption( String* value ) {
        if(caption!=value) {
            caption = value;
        }
    }
};

int main()
{
    Console::WriteLine(S"\nReflection.PropertyInfo");

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

    Console::Write(S"\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(S"Mypropertya");
    PropertyInfo* Mypropertyinfoa = MyTypea->GetProperty(S"Caption");
    Type* MyTypeb = Type::GetType(S"Mypropertyb");
    PropertyInfo* Mypropertyinfob = MyTypeb->GetProperty(S"Caption");

    // Get and display the CanRead property.
    Console::Write(S"\nCanRead a - {0}", __box(Mypropertyinfoa->CanRead));
    Console::Write(S"\nCanRead b - {0}", __box(Mypropertyinfob->CanRead));

    return 0;
}

[JScript] 
import System;
import System.Reflection;
 
//Make two properties, one readable and on not readable
 public class Mypropertya
 {
    private var caption : String = "A Default caption";
    public function get Caption() : String {
        return caption;
    }
    public function set Caption(value:String)
    {
        if(caption!=value) {caption = value;}
    }
 }
 public class Mypropertyb
 {
    private var caption : String = "B Default caption";
    public function set Caption(value:String) {
        if(caption!=value) {caption = value;}
    }
 }
  
 class Mypropertyinfo
 {
    public static function Main() : void
    {
       Console.WriteLine("\nReflection.PropertyInfo");
  
       //Build two properties
       var mypropertya : Mypropertya = new Mypropertya();
       var mypropertyb : Mypropertyb = new Mypropertyb();
  
       Console.Write("\nmypropertya.Caption = " + mypropertya.Caption);
       //Note: Mypropertyb.Caption cannot be read as
       // there is no get accessor
  
       //Get the type and PropertyInfo
       var MyTypea : Type = Type.GetType("Mypropertya");
       var Mypropertyinfoa : PropertyInfo = MyTypea.GetProperty("Caption");
       var MyTypeb : Type = Type.GetType("Mypropertyb");
       var Mypropertyinfob : PropertyInfo = MyTypeb.GetProperty("Caption");
  
       //Get and display the CanRead property
      
       Console.Write("\nCanRead a - " + Mypropertyinfoa.CanRead);
      
       Console.Write("\nCanRead b - " + Mypropertyinfob.CanRead);
  
    }
 }
 Mypropertyinfo.Main();
 /*
 Produces the following output
 
 Reflection.PropertyInfo
 Mypropertya.Caption = A Default caption
 CanRead a - True
 CanRead b - False
 */

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

PropertyInfo クラス | PropertyInfo メンバ | System.Reflection 名前空間