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
方法。