FieldInfo.IsPublic 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得值,指出欄位是否為公用的。
public:
property bool IsPublic { bool get(); };
public bool IsPublic { get; }
member this.IsPublic : bool
Public ReadOnly Property IsPublic As Boolean
屬性值
如果欄位是公用的,則為 true
,否則為 false
。
實作
範例
下列範例會傳回值,指出 類別的欄位是否為公用或私用。
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;
}
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;
}
}
Imports System.Reflection
' 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
Public Class Myfieldb
' Make a public field.
Public SomeField As String = "public field"
End Class
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
備註
公用欄位可在可見其對應類別的地方存取。
屬性 IsPublic
設定時 FieldAttributes.Public
會設定屬性。
若要取得 IsPublic
屬性,請先取得 類別 Type
。 從取得 Type
FieldInfo
。 FieldInfo
從取得 IsPublic
屬性。 如果欄位不是公用欄位,則會受到保護且無法立即存取。 若要存取非公用字段,請將 設定 BindingFlags
為 NonPublic
、指定 BindingFlags.Instance
或 BindingFlags.Static
,並將這個 用於 GetField
方法。