FieldInfo.GetValue 方法
在派生类中被重写时,返回给定对象支持的字段的值。
**命名空间:**System.Reflection
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
Public MustOverride Function GetValue ( _
obj As Object _
) As Object
用法
Dim instance As FieldInfo
Dim obj As Object
Dim returnValue As Object
returnValue = instance.GetValue(obj)
public abstract Object GetValue (
Object obj
)
public:
virtual Object^ GetValue (
Object^ obj
) abstract
public abstract Object GetValue (
Object obj
)
public abstract function GetValue (
obj : Object
) : Object
参数
- obj
其字段值将返回的对象。
返回值
包含此实例反映的字段值的对象。
异常
异常类型 | 条件 |
---|---|
此字段是非静态的且 obj 为 空引用(在 Visual Basic 中为 Nothing)。 |
|
字段被标记为文本,但是该字段没有一个可接受的文本类型。 |
|
调用方没有访问此字段的权限。 |
|
obj 类既不声明该方法也不继承该方法。 |
备注
如果该字段是静态的,则忽略 obj。对于非静态字段,obj 应是继承或声明该字段的类的实例。请注意,GetValue 的返回类型为 Object。例如,如果此字段包含一个 Boolean 基元值,则返回带有相应 Boolean 值的 Object 的实例。在返回该值以前,GetValue 将检查用户是否有访问权限。
提示
对于完全受信任的代码忽略访问限制。即,只要代码完全受信任,就可以通过反射来访问和调用私有构造函数、方法、字段和属性 (Property)。
示例
' The following example demonstrates getting
' a field value directly, without having an object,
' by defining it as a static field.
Imports System
Imports System.Reflection
Class [MyClass]
Public Shared val As [String] = "test"
Public Shared Sub Main()
Dim myf As FieldInfo = GetType([MyClass]).GetField("val")
Console.WriteLine(myf.GetValue(Nothing))
val = "hi"
Console.WriteLine(myf.GetValue(Nothing))
End Sub 'Main
End Class '[MyClass]
// The following example demonstrates getting
// a field value directly, without having an object,
// by defining it as a static field.
using System;
using System.Reflection;
class MyClass
{
public static String val = "test";
public static void Main()
{
FieldInfo myf = typeof(MyClass).GetField("val");
Console.WriteLine(myf.GetValue(null));
val = "hi";
Console.WriteLine(myf.GetValue(null));
}
}
// The following example demonstrates getting
// a field value directly, without having an object,
// by defining it as a static field.
using namespace System;
using namespace System::Reflection;
ref class MyClass
{
public:
static String^ val = "test";
};
int main()
{
FieldInfo^ myf = MyClass::typeid->GetField( "val" );
Console::WriteLine( myf->GetValue( nullptr ) );
MyClass::val = "hi";
Console::WriteLine( myf->GetValue( nullptr ) );
}
// The following example demonstrates getting
// a field value directly, without having an object,
// by defining it as a static field.
import System.*;
import System.Reflection.*;
class MyClass
{
public static String val = "test";
public static void main(String[] args)
{
FieldInfo myf = MyClass.class.ToType().GetField("val");
Console.WriteLine(myf.GetValue(null));
val = "hi";
Console.WriteLine(myf.GetValue(null));
} //main
} //MyClass
下面的示例检索 MyClass 的字段并显示字段值。
Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic
Public Class MyClass1
Public myFieldA As String
Public myFieldB As String
Public Sub New()
myFieldA = "A public field"
myFieldB = "Another public field"
End Sub 'New
End Class 'MyClass1
Public Class FieldInfo_GetValue
Public Shared Sub Main()
Dim myInstance As New MyClass1()
' Get the type of MyClass1.
Dim myType As Type = GetType(MyClass1)
Try
' Get the FieldInfo of MyClass1.
Dim myFields As FieldInfo() = myType.GetFields((BindingFlags.Public Or BindingFlags.Instance))
' Display the values of the fields.
Console.WriteLine(ControlChars.NewLine & "Displaying the values of the fields of {0}." & ControlChars.NewLine, myType.ToString())
Dim i As Integer
For i = 0 To myFields.Length - 1
Console.WriteLine("The value of the field {0} is: {1}", myFields(i).Name, myFields(i).GetValue(myInstance))
Next i
Catch e As FieldAccessException
Console.WriteLine("FieldAccessException : {0}", e.Message.ToString())
Catch e As TargetException
Console.WriteLine("TargetException : {0}", e.Message.ToString())
Catch e As ExecutionEngineException
Console.WriteLine("ExecutionEngineException : {0}", e.Message.ToString())
Catch e As MemberAccessException
Console.WriteLine("MemberAccessException : {0}", e.Message.ToString())
Catch e As Exception
Console.WriteLine("Exception : {0}", e.Message.ToString())
End Try
End Sub 'Main
End Class 'FieldInfo_GetValue
using System;
using System.Reflection;
public class MyClass
{
public string myFieldA;
public string myFieldB;
public MyClass()
{
myFieldA = "A public field";
myFieldB = "Another public field";
}
}
public class FieldInfo_GetValue
{
public static void Main()
{
MyClass myInstance = new MyClass();
// Get the type of MyClass.
Type myType = typeof(MyClass);
try
{
// Get the FieldInfo of MyClass.
FieldInfo[] myFields = myType.GetFields(BindingFlags.Public
| BindingFlags.Instance);
// Display the values of the fields.
Console.WriteLine("\nDisplaying the values of the fields of {0}.\n",
myType);
for(int i = 0; i < myFields.Length; i++)
{
Console.WriteLine("The value of {0} is: {1}",
myFields[i].Name, myFields[i].GetValue(myInstance));
}
}
catch(FieldAccessException e)
{
Console.WriteLine("FieldAccessException : {0}", e.Message);
}
catch(TargetException e)
{
Console.WriteLine("TargetException : {0}", e.Message);
}
catch(ExecutionEngineException e)
{
Console.WriteLine("ExecutionEngineException : {0}", e.Message);
}
catch(MemberAccessException e)
{
Console.WriteLine("MemberAccessException : {0}", e.Message);
}
catch(Exception e)
{
Console.WriteLine("Exception : {0}", e.Message);
}
}
}
using namespace System;
using namespace System::Reflection;
public ref class MyClass
{
public:
String^ myFieldA;
String^ myFieldB;
MyClass()
{
myFieldA = "A public field";
myFieldB = "Another public field";
}
};
int main()
{
MyClass^ myInstance = gcnew MyClass;
// Get the type of MyClass.
Type^ myType = MyClass::typeid;
try
{
// Get the FieldInfo of MyClass.
array<FieldInfo^>^myFields = myType->GetFields( static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance) );
// Display the values of the fields.
Console::WriteLine( "\nDisplaying the values of the fields of {0}.\n", myType );
for ( int i = 0; i < myFields->Length; i++ )
{
Console::WriteLine( "The value of {0} is: {1}", myFields[ i ]->Name, myFields[ i ]->GetValue( myInstance ) );
}
}
catch ( FieldAccessException^ e )
{
Console::WriteLine( "FieldAccessException : {0}", e->Message );
}
catch ( TargetException^ e )
{
Console::WriteLine( "TargetException : {0}", e->Message );
}
catch ( ExecutionEngineException^ e )
{
Console::WriteLine( "ExecutionEngineException : {0}", e->Message );
}
catch ( MemberAccessException^ e )
{
Console::WriteLine( "MemberAccessException : {0}", e->Message );
}
catch ( Exception^ e )
{
Console::WriteLine( "Exception : {0}", e->Message );
}
}
.NET Framework 安全性
- ReflectionPermission 当通过类似 Type.InvokeMember 的机制以后期绑定的形式进行调用时。关联的枚举:ReflectionPermissionFlag.MemberAccess。
平台
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