次の方法で共有


FieldInfo.IsPrivate プロパティ

フィールドがプライベートかどうかを示す値を取得します。

Public ReadOnly Property IsPrivate As Boolean
[C#]
public bool IsPrivate {get;}
[C++]
public: __property bool get_IsPrivate();
[JScript]
public function get IsPrivate() : Boolean;

プロパティ値

フィールドがプライベートの場合は true 。それ以外の場合は false

解説

プライベート フィールドにアクセスできるのは、メンバ関数だけです。

IsPrivate プロパティは、 FieldAttributes.Private 属性が設定されたときに設定されます。

IsPrivate プロパティを取得するには、最初に Type クラスを取得します。そして、その Type から FieldInfo を取得します。最後に、 FieldInfo から IsPrivate プロパティを取得します。非パブリック フィールドにアクセスするには、 BindingFlagsNonPublic に設定し、 GetField メソッドで Static または Instance を設定します。

使用例

[Visual Basic, C#, C++] クラスのフィールドがプライベートかどうかを示す値を返す例を次に示します。

 

Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

Class [MyClass]
    Private myField As String
    Public myArray() As String = {"New York", "New Jersey"}

    Sub New()
        myField = "Microsoft"
    End Sub 'New

    ReadOnly Property GetField() As String
        Get
            Return myField
        End Get
    End Property
End Class '[MyClass]


Class FieldInfo_IsPrivate

    Public Shared Sub Main()
        Try
            ' Gets the type of MyClass.
            Dim myType As Type = GetType([MyClass])

            ' Gets the field information of MyClass.
            Dim myFields As FieldInfo() = myType.GetFields((BindingFlags.NonPublic Or BindingFlags.Public Or BindingFlags.Instance))

            Console.WriteLine(ControlChars.Cr & "Displaying whether the fields of {0} are private or not:" & ControlChars.Cr, myType)
            Console.WriteLine() 
            Dim i As Integer
            For i = 0 To myFields.Length - 1
                ' Check whether the field is private or not. 
                If myFields(i).IsPrivate Then
                    Console.WriteLine("{0} is a private field.", myFields(i).Name)
                Else
                    Console.WriteLine("{0} is not a private field.", myFields(i).Name)
                End If
            Next i
        Catch e As Exception
            Console.WriteLine("Exception : {0} ", e.Message.ToString())
        End Try
    End Sub 'Main
End Class 'FieldInfo_IsPrivate

[C#] 
using System;
using System.Reflection;

class MyClass
{
    private string myField;
    public string[] myArray = new string[] {"New York", "New Jersey"};
    MyClass()
    {
        myField = "Microsoft";
    }
    string GetField
    {
        get
        {
            return myField;
        }
    }
}

class FieldInfo_IsPrivate
{
    public static void Main()
    {
        try
        {
            // Gets the type of MyClass.
            Type myType = typeof(MyClass);

            // Gets the field information of MyClass.
            FieldInfo[] myFields = myType.GetFields(BindingFlags.NonPublic
                |BindingFlags.Public
                |BindingFlags.Instance);
      
            Console.WriteLine("\nDisplaying whether the fields of {0} are private or not:\n", myType);
            for(int i = 0; i < myFields.Length; i++)
            {
                // Check whether the field is private or not. 
                if(myFields[i].IsPrivate)
                    Console.WriteLine("{0} is a private field.", myFields[i].Name);
                else
                    Console.WriteLine("{0} is not a private field.", myFields[i].Name);
            }
        }
        catch(Exception e)
        {
            Console.WriteLine("Exception : {0} " , e.Message);
        }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Reflection;

__gc class MyClass 
{
private:
   String* myField;
public:
   String* myArray[];
   MyClass() {
      myField = S"Microsoft";
      String* s[] = {S"New York", S"New Jersey"};
      myArray = s;
   }
   __property String* get_GetField()
   {
      return myField;
   }
};

int main() 
{
   try {
      // Gets the type of MyClass.
      Type* myType = __typeof(MyClass);

      // Gets the field information of MyClass.
      FieldInfo* myFields[] = myType->GetFields(
         static_cast<BindingFlags>(BindingFlags::NonPublic|BindingFlags::Public|BindingFlags::Instance));

      Console::WriteLine(S"\nDisplaying whether the fields of {0} are private or not:\n", myType);
      for (int i = 0; i < myFields->Length; i++) {
         // Check whether the field is private or not. 
         if (myFields[i]->IsPrivate)
            Console::WriteLine(S" {0} is a private field.", myFields[i]->Name);
         else
            Console::WriteLine(S" {0} is not a private field.", myFields[i]->Name);
      }
   } catch (Exception* e) {
      Console::WriteLine(S"Exception : {0} " , e->Message);
   }
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: 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

参照

FieldInfo クラス | FieldInfo メンバ | System.Reflection 名前空間 | BindingFlags | Type