다음을 통해 공유


FieldInfo.IsPublic 속성

public 필드인지 여부를 나타내는 값을 가져옵니다.

네임스페이스: System.Reflection
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
Public ReadOnly Property IsPublic As Boolean
‘사용 방법
Dim instance As FieldInfo
Dim value As Boolean

value = instance.IsPublic
public bool IsPublic { get; }
public:
virtual property bool IsPublic {
    bool get () sealed;
}
/** @property */
public final boolean get_IsPublic ()
public final function get IsPublic () : boolean

속성 값

public 필드이면 true이고, 그렇지 않으면 false입니다.

설명

public 필드는 해당 클래스가 표시된 모든 위치에서 액세스할 수 있습니다.

FieldAttributes.Public 특성이 설정될 때 IsPublic 속성이 설정됩니다.

IsPublic 속성을 가져오려면 먼저 Type 클래스를 가져옵니다. Type에서 FieldInfo를 가져옵니다. FieldInfo에서 IsPublic 속성을 가져옵니다. public 필드 이외의 필드는 protected 필드이므로 액세스할 수 없습니다. public이 아닌 필드에 액세스하려면 BindingFlagsNonPublic으로 설정하고 BindingFlags.Instance 또는 BindingFlags.Static을 지정하여 GetField 메서드에 대해 이것을 사용합니다.

예제

다음 예제에서는 클래스의 필드가 public 또는 private 필드인지 여부를 나타내는 값을 반환합니다.

Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic


' Make two fields.
Public Class Myfielda
    ' Make a private field.
    Private SomeField As String = "private field"

    Public ReadOnly Property Field() As String
        Get
            Return SomeField
        End Get
    End Property
End Class 'Myfielda

Public Class Myfieldb
    ' Make a public field.
    Public SomeField As String = "public field"
End Class 'Myfieldb

Public Class Myfieldinfo

    Public Shared Function Main() As Integer
        Console.WriteLine("Reflection.FieldInfo")
        Console.WriteLine()
        Dim Myfielda As New Myfielda()
        Dim Myfieldb As New Myfieldb()

        ' Get the Type and FieldInfo.
        Dim MyTypea As Type = GetType(Myfielda)
        Dim Myfieldinfoa As FieldInfo = MyTypea.GetField("SomeField", _
            BindingFlags.NonPublic Or BindingFlags.Instance)
        Dim MyTypeb As Type = GetType(Myfieldb)
        Dim Myfieldinfob As FieldInfo = MyTypeb.GetField("SomeField")

        ' Get and display the IsPublic and IsPrivate property values.
        Console.WriteLine("{0}.{1} - {2}", MyTypea.FullName, Myfieldinfoa.Name, _
            Myfielda.Field)
        Console.WriteLine("   IsPublic = {0}", Myfieldinfoa.IsPublic)
        Console.WriteLine("   IsPrivate = {0}", Myfieldinfoa.IsPrivate)
        Console.WriteLine()
        Console.WriteLine("{0}.{1} - {2}", MyTypeb.FullName, Myfieldinfob.Name, _
            Myfieldb.SomeField)
        Console.WriteLine("   IsPublic = {0}", Myfieldinfob.IsPublic)
        Console.WriteLine("   IsPrivate = {0}", Myfieldinfob.IsPrivate)

        Return 0
    End Function 'Main
End Class 'Myfieldinfo
using System;
using System.Reflection;


 // Make two fields.
public
    class Myfielda // private
{
    private string SomeField = "private field";
    public string Field
    {
        get{return SomeField;}
    }
}

public
    class Myfieldb // public
{
    public string SomeField = "public field";
}
 
public
    class Myfieldinfo
{
    public static int Main()
    {
        Console.WriteLine("\nReflection.FieldInfo");
        Myfielda Myfielda = new Myfielda();
        Myfieldb Myfieldb = new Myfieldb();
  
        // Get the Type and FieldInfo.
        Type MyTypea = typeof(Myfielda);
        FieldInfo Myfieldinfoa = MyTypea.GetField("SomeField", 
            BindingFlags.NonPublic|BindingFlags.Instance);
        Type MyTypeb = typeof(Myfieldb);
        FieldInfo Myfieldinfob = MyTypeb.GetField("SomeField");
  
        // Get and display the IsPublic and IsPrivate property values.
        Console.Write("\n{0}.", MyTypea.FullName);
        Console.Write("{0} - ", Myfieldinfoa.Name);
        Console.Write("{0}", Myfielda.Field);
        Console.Write("\n   IsPublic = {0}", Myfieldinfoa.IsPublic);
        Console.Write("\n   IsPrivate = {0}", Myfieldinfoa.IsPrivate);
  
        Console.Write("\n{0}.", MyTypeb.FullName);
        Console.Write("{0} - ", Myfieldinfob.Name);
        Console.Write("{0};", Myfieldb.SomeField);
        Console.Write("\n   IsPublic = {0}", Myfieldinfob.IsPublic);
        Console.Write("\n   IsPrivate = {0}", Myfieldinfob.IsPrivate);
  
        return 0;
    }
}
using namespace System;
using namespace System::Reflection;

// Make two fields.
// private
public ref class Myfielda
{
private:
   String^ SomeField;

public:
   Myfielda()
      : SomeField( "private field" )
   {}


   property String^ Field 
   {
      String^ get()
      {
         return SomeField;
      }

   }

};


// public
public ref class Myfieldb
{
public:
   String^ SomeField;
   Myfieldb()
      : SomeField( "public field" )
   {}


   property String^ Field 
   {
      String^ get()
      {
         return SomeField;
      }

   }

};

int main()
{
   Console::WriteLine( "\nReflection.FieldInfo" );
   Myfielda^ myfielda = gcnew Myfielda;
   Myfieldb^ myfieldb = gcnew Myfieldb;
   
   // Get the Type and FieldInfo.
   Type^ MyTypea = Type::GetType( "Myfielda" );
   FieldInfo^ Myfieldinfoa = MyTypea->GetField( "SomeField", static_cast<BindingFlags>(BindingFlags::NonPublic | BindingFlags::Instance) );
   Type^ MyTypeb = Type::GetType( "Myfieldb" );
   FieldInfo^ Myfieldinfob = MyTypeb->GetField( "SomeField" );
   
   // Get and display the IsPublic and IsPrivate property values.
   Console::Write( "\n{0}.", MyTypea->FullName );
   Console::Write( "{0} - ", Myfieldinfoa->Name );
   Console::Write( "{0}", myfielda->Field );
   Console::Write( "\n   IsPublic = {0}", Myfieldinfoa->IsPublic );
   Console::Write( "\n   IsPrivate = {0}", Myfieldinfoa->IsPrivate );
   Console::Write( "\n{0}.", MyTypeb->FullName );
   Console::Write( "{0} - ", Myfieldinfob->Name );
   Console::Write( "{0};", myfieldb->Field );
   Console::Write( "\n   IsPublic = {0}", Myfieldinfob->IsPublic );
   Console::Write( "\n   IsPrivate = {0}", Myfieldinfob->IsPrivate );
   return 0;
}
import System.*;
import System.Reflection.*;

// Make two fields.
public class MyFielda
{
    // private
    private String someField = "private field";

    /** @property 
     */
    public String get_Field()
    {
        return someField;
    } //get_Field
} //MyFielda

public class MyFieldb
{
    // public
    public String someField = "public field";

    /** @property 
     */
    public String get_Field()
    {
        return someField;
    } //get_Field
} //MyFieldb

public class MyFieldInfo
{
    public static void main(String[] args)
    {
        Console.WriteLine("\nReflection.FieldInfo");
        MyFielda myFielda = new MyFielda();
        MyFieldb myFieldb = new MyFieldb();

        // Get the Type and FieldInfo.
        Type myTypea = Type.GetType("MyFielda");
        FieldInfo myFieldInfoa = myTypea.GetField("someField", 
            BindingFlags.NonPublic | BindingFlags.Instance);
        Type myTypeb = Type.GetType("MyFieldb");
        FieldInfo myFieldInfob = myTypeb.GetField("someField");

        // Get and display the IsPublic and IsPrivate property values.
        Console.Write("\n{0}.", myTypea.get_FullName());
        Console.Write("{0} - ", myFieldInfoa.get_Name());
        Console.Write("{0}", myFielda.get_Field());
        Console.Write("\n   IsPublic = {0}", 
            System.Convert.ToString(myFieldInfoa.get_IsPublic()));
        Console.Write("\n   IsPrivate = {0}", 
            System.Convert.ToString(myFieldInfoa.get_IsPrivate()));
        Console.Write("\n{0}.", myTypeb.get_FullName());
        Console.Write("{0} - ", myFieldInfob.get_Name());
        Console.Write("{0};", myFieldb.get_Field());
        Console.Write("\n   IsPublic = {0}", 
            System.Convert.ToString(myFieldInfob.get_IsPublic()));
        Console.Write("\n   IsPrivate = {0}", 
            System.Convert.ToString(myFieldInfob.get_IsPrivate()));
    } //main
} //MyFieldInfo
import System;
import System.Reflection;

// Make two fields.

// Make a private field.
public class Myfielda
{
    private var field : String = "private field";
    public function get Field() : String {
        return field;
    }
 }
// Make a public field.
 public class Myfieldb
 {
    public var field : String = "public field";
    public function get Field() : String {
        return field;
    }
 }
 
 public class Myfieldinfo
 {
    public static function Main(): void
    {
       Console.WriteLine("\nReflection.FieldInfo");
       var myfielda : Myfielda = new Myfielda();
       var myfieldb : Myfieldb = new Myfieldb();
  
       //Get the Type and FieldInfo.
       var MyTypea : Type = Type.GetType("Myfielda");
       var Myfieldinfoa : FieldInfo = MyTypea.GetField("field", BindingFlags.Instance|BindingFlags.NonPublic);
       var MyTypeb : Type = Type.GetType("Myfieldb");
       var Myfieldinfob : FieldInfo = MyTypeb.GetField("field");
  
       //Get and Display the IsPublic and IsPrivate.
       Console.Write("\n{0}.", MyTypea.FullName);
       Console.Write("{0} - ", Myfieldinfoa.Name);
       Console.Write("{0};", myfielda.Field);
       Console.Write("\n   IsPublic = {0}; ",Myfieldinfoa.IsPublic);
       Console.Write("\n   IsPrivate = {0}; ",Myfieldinfoa.IsPrivate);
  
       Console.Write("\n{0}.", MyTypeb.FullName);
       Console.Write("{0} - ", Myfieldinfob.Name);
       Console.Write("{0};", myfieldb.Field);
       Console.Write("\n   IsPublic = {0}; ", Myfieldinfob.IsPublic);
       Console.Write("\n   IsPrivate = {0}; ",Myfieldinfob.IsPrivate);
    }
 }
 Myfieldinfo.Main();

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

FieldInfo 클래스
FieldInfo 멤버
System.Reflection 네임스페이스
FieldAttributes 열거형
BindingFlags 열거형