DynamicObject.TryGetMember(GetMemberBinder, Object) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为获取成员值的操作提供实现。 从 DynamicObject 类派生的类可以重写此方法,以便为诸如获取属性值这样的操作指定动态行为。
public:
virtual bool TryGetMember(System::Dynamic::GetMemberBinder ^ binder, [Runtime::InteropServices::Out] System::Object ^ % result);
public virtual bool TryGetMember (System.Dynamic.GetMemberBinder binder, out object result);
public virtual bool TryGetMember (System.Dynamic.GetMemberBinder binder, out object? result);
abstract member TryGetMember : System.Dynamic.GetMemberBinder * obj -> bool
override this.TryGetMember : System.Dynamic.GetMemberBinder * obj -> bool
Public Overridable Function TryGetMember (binder As GetMemberBinder, ByRef result As Object) As Boolean
参数
- binder
- GetMemberBinder
提供有关调用了动态操作的对象的信息。 属性 binder.Name
提供对其执行动态操作的成员的名称。 例如,对于 Console.WriteLine(sampleObject.SampleProperty)
语句,其中 sampleObject
是派生自 DynamicObject 类的类的实例, binder.Name
返回“SampleProperty”。 属性 binder.IgnoreCase
指定成员名称是否区分大小写。
- result
- Object
获取操作的结果。 例如,如果为某个属性调用该方法,则可以为 result
指派该属性值。
返回
如果操作成功,则为 true
;否则为 false
。 如果此方法返回 false
,则该语言的运行时联编程序将决定行为。 (大多数情况下,将引发运行时异常。)
示例
假设你想要提供替代语法来访问字典中的值,这样就可以编写 (,而不是sampleDictionary["Text"] = "Sample text"
在 Visual Basic) 中编写 sampleDictionary.Text = "Sample text"
。sampleDictionary("Text") = "Sample text"
此外,此语法必须不区分大小写,以便等效 sampleDictionary.Text
于 sampleDictionary.text
。
下面的代码示例演示 DynamicDictionary
派生自 类的 DynamicObject 类。 类DynamicDictionary
包含 Visual Basic) 中 (Dictionary(Of String, Object)
类型的 对象Dictionary<string, object>
,用于存储键值对,并重写 TrySetMember 和 TryGetMember 方法以支持新语法。 它还提供一个 Count
属性,该属性显示字典包含的动态属性数。
// The class derived from DynamicObject.
public class DynamicDictionary : DynamicObject
{
// The inner dictionary.
Dictionary<string, object> dictionary
= new Dictionary<string, object>();
// This property returns the number of elements
// in the inner dictionary.
public int Count
{
get
{
return dictionary.Count;
}
}
// If you try to get a value of a property
// not defined in the class, this method is called.
public override bool TryGetMember(
GetMemberBinder binder, out object result)
{
// Converting the property name to lowercase
// so that property names become case-insensitive.
string name = binder.Name.ToLower();
// If the property name is found in a dictionary,
// set the result parameter to the property value and return true.
// Otherwise, return false.
return dictionary.TryGetValue(name, out result);
}
// If you try to set a value of a property that is
// not defined in the class, this method is called.
public override bool TrySetMember(
SetMemberBinder binder, object value)
{
// Converting the property name to lowercase
// so that property names become case-insensitive.
dictionary[binder.Name.ToLower()] = value;
// You can always add a value to a dictionary,
// so this method always returns true.
return true;
}
}
class Program
{
static void Main(string[] args)
{
// Creating a dynamic dictionary.
dynamic person = new DynamicDictionary();
// Adding new dynamic properties.
// The TrySetMember method is called.
person.FirstName = "Ellen";
person.LastName = "Adams";
// Getting values of the dynamic properties.
// The TryGetMember method is called.
// Note that property names are case-insensitive.
Console.WriteLine(person.firstname + " " + person.lastname);
// Getting the value of the Count property.
// The TryGetMember is not called,
// because the property is defined in the class.
Console.WriteLine(
"Number of dynamic properties:" + person.Count);
// The following statement throws an exception at run time.
// There is no "address" property,
// so the TryGetMember method returns false and this causes a
// RuntimeBinderException.
// Console.WriteLine(person.address);
}
}
// This example has the following output:
// Ellen Adams
// Number of dynamic properties: 2
' The class derived from DynamicObject.
Public Class DynamicDictionary
Inherits DynamicObject
' The inner dictionary.
Dim dictionary As New Dictionary(Of String, Object)
' This property returns the number of elements
' in the inner dictionary.
ReadOnly Property Count As Integer
Get
Return dictionary.Count
End Get
End Property
' If you try to get a value of a property that is
' not defined in the class, this method is called.
Public Overrides Function TryGetMember(
ByVal binder As System.Dynamic.GetMemberBinder,
ByRef result As Object) As Boolean
' Converting the property name to lowercase
' so that property names become case-insensitive.
Dim name As String = binder.Name.ToLower()
' If the property name is found in a dictionary,
' set the result parameter to the property value and return true.
' Otherwise, return false.
Return dictionary.TryGetValue(name, result)
End Function
Public Overrides Function TrySetMember(
ByVal binder As System.Dynamic.SetMemberBinder,
ByVal value As Object) As Boolean
' Converting the property name to lowercase
' so that property names become case-insensitive.
dictionary(binder.Name.ToLower()) = value
' You can always add a value to a dictionary,
' so this method always returns true.
Return True
End Function
End Class
Sub Main()
' Creating a dynamic dictionary.
Dim person As Object = New DynamicDictionary()
' Adding new dynamic properties.
' The TrySetMember method is called.
person.FirstName = "Ellen"
person.LastName = "Adams"
' Getting values of the dynamic properties.
' The TryGetMember method is called.
' Note that property names are now case-insensitive,
' although they are case-sensitive in C#.
Console.WriteLine(person.firstname & " " & person.lastname)
' Getting the value of the Count property.
' The TryGetMember is not called,
' because the property is defined in the class.
Console.WriteLine("Number of dynamic properties:" & person.Count)
' The following statement throws an exception at run time.
' There is no "address" property,
' so the TryGetMember method returns false and this causes
' a MissingMemberException.
' Console.WriteLine(person.address)
End Sub
' This examples has the following output:
' Ellen Adams
' Number of dynamic properties: 2
注解
派生自 类的 DynamicObject 类可以重写此方法,以指定应如何为动态对象执行获取成员值的操作。 当方法未重写时,语言的运行时绑定器将确定行为。 (大多数情况下,将引发运行时异常。)
如果具有 等 Console.WriteLine(sampleObject.SampleProperty)
语句,则调用此方法,其中 sampleObject
是派生自 类的类的 DynamicObject 实例。
还可以将自己的成员添加到派生自 类的类。DynamicObject
如果类定义属性并重写 TrySetMember 方法,则动态语言运行时 (DLR) 首先使用语言绑定器来查找类中属性的静态定义。 如果没有此类属性,DLR 将调用 TrySetMember 方法。