FieldInfo.IsInitOnly 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取一个值,通过该值指示此字段是否只能在构造函数的主体中设置。
public:
property bool IsInitOnly { bool get(); };
public bool IsInitOnly { get; }
member this.IsInitOnly : bool
Public ReadOnly Property IsInitOnly As Boolean
属性值
如果字段设置了 true
属性,则为 InitOnly
;否则为 false
。
实现
示例
在以下示例中,创建了两个字段。 第二个字段是只读的,没有 set 访问器,并且 IsInitOnly
设置为 true
。
using namespace System;
using namespace System::Reflection;
//Make two fields, one public and one read-only.
public ref class Myfielda
{
public:
String^ field;
Myfielda()
: field( "A - public field" )
{}
property String^ Field
{
String^ get()
{
return field;
}
void set( String^ value )
{
if ( field != value )
{
field = value;
}
}
}
};
public ref class Myfieldb
{
private:
String^ const field;
public:
Myfieldb()
: field( "B - readonly field" )
{}
property String^ Field
{
String^ get()
{
return field;
}
}
};
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( "field", static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance) );
Type^ MyTypeb = Type::GetType( "Myfieldb" );
FieldInfo^ Myfieldinfob = MyTypeb->GetField( "field", static_cast<BindingFlags>(BindingFlags::NonPublic | BindingFlags::Instance) );
//Modify the fields.
//Note that Myfieldb is not modified, as it is
//read-only (IsInitOnly is True).
myfielda->field = "A- modified";
//Myfieldb.field = "B- modified";
//For the first field, get and display the name, field, and IsInitOnly state.
Console::Write( "\n{0} - {1}, IsInitOnly = {2} ", MyTypea->FullName, Myfieldinfoa->GetValue( myfielda ), Myfieldinfoa->IsInitOnly );
//For the second field get and display the name, field, and IsInitOnly state.
Console::Write( "\n{0} - {1}, IsInitOnly = {2} ", MyTypeb->FullName, Myfieldinfob->GetValue( myfieldb ), Myfieldinfob->IsInitOnly );
return 0;
}
using System;
using System.Reflection;
//Make two fields, one public and one read-only.
public class Myfielda
{
public string field = "A - public modifiable field";
}
public class Myfieldb
{
public readonly string field = "B - readonly 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("field",
BindingFlags.Public | BindingFlags.Instance);
Type MyTypeb = typeof(Myfieldb);
FieldInfo Myfieldinfob = MyTypeb.GetField("field",
BindingFlags.Public | BindingFlags.Instance);
//Modify the fields.
//Note that Myfieldb is not modified, as it is
//read-only (IsInitOnly is True).
Myfielda.field = "A - modified";
//Myfieldb.field = "B - modified";
//For the first field, get and display the name, field, and IsInitOnly state.
Console.Write("\n{0} - {1}, IsInitOnly = {2} ",
MyTypea.FullName,
Myfieldinfoa.GetValue(Myfielda),
Myfieldinfoa.IsInitOnly);
//For the second field get and display the name, field, and IsInitOnly state.
Console.Write("\n{0} - {1}, IsInitOnly = {2} ",
MyTypeb.FullName,
Myfieldinfob.GetValue(Myfieldb),
Myfieldinfob.IsInitOnly);
return 0;
}
}
Imports System.Reflection
'Make two fields, one public and one read-only.
Public Class Myfielda
Public field As String = "A - public modifiable field"
End Class
Public Class Myfieldb
Public ReadOnly field As String = "B - readonly 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("field", _
BindingFlags.Public Or BindingFlags.Instance)
Dim MyTypeb As Type = GetType(Myfieldb)
Dim Myfieldinfob As FieldInfo = MyTypeb.GetField("field", _
BindingFlags.Public Or BindingFlags.Instance)
'Modify the fields.
'Note that Myfieldb is not modified, as it is
'read-only (IsInitOnly is True).
Myfielda.field = "A - modified"
'For the first field, get and display the name, field, and IsInitOnly state.
Console.WriteLine("{0} - {1}, IsInitOnly = {2} ", MyTypea.FullName, _
Myfieldinfoa.GetValue(Myfielda), Myfieldinfoa.IsInitOnly)
'For the second field get and display the name, field, and IsInitOnly state.
Console.WriteLine("{0} - {1}, IsInitOnly = {2} ", MyTypeb.FullName, _
Myfieldinfob.GetValue(Myfieldb), Myfieldinfob.IsInitOnly)
Return 0
End Function 'Main
End Class
此代码生成以下输出:
Reflection.FieldInfo
Myfielda - A- modified, IsInitOnly = False
Myfieldb - B readonly field, IsInitOnly = True
注解
如果返回的值为 true
,则字段只能初始化,并且此后为只读。
若要获取 IsInitOnly
属性,请首先获取类 Type
。
Type
从 中获取 FieldInfo
。 从 中 FieldInfo
获取 IsInitOnly
属性。 若要访问非公共字段,请在 方法中GetField
与 和 BindingFlags.Instance 的BindingFlags.Static或两者结合使用BindingFlags.NonPublic。
属性 IsInitOnly
是在设置 属性时设置的 FieldAttributes.InitOnly 。