ExpandoObject,DynamicObject,DynamicMetaObject

时间:2022-03-23 11:05:43

ExpandoObject,DynamicObject,DynamicMetaObject

接上文:浅谈Dynamic关键字系列之三(上)

为什么TryXXX方法没有被调用??

将DynamicProduct 中的name修饰符改为private:

private string name;

可以在TrySetMember方法中设置断点,再次运行:

ExpandoObject,DynamicObject,DynamicMetaObject

ExpandoObject,DynamicObject,DynamicMetaObject

ExpandoObject,DynamicObject,DynamicMetaObject

为什么访问修饰符是Public不调用TrySetMember,是Private 就调用了呢??

难道是因为private抛出了异常吗??

再次看看Msdn对此的TrySetMember方法的解释:

Msdn备注

…………….动态语言运行库 (DLR) 将首先使用语言联编程序在类中查找属性的静态定义。 如果没有此类属性,DLR 调用 TrySetMember 方法。

问题的原因是这样的:首先DLR 使用语言联编程序在类中查找name的静态定义,

因为name是public,所以查找到了,然后返回,不会去调用TrySetMember方法了,

但是如果name是private,那么联编程序在类中没找到name的静态定义,于是DLR尝试调用TrySetMember方法。

修改TrySetMember方法如下:

public override bool TrySetMember(SetMemberBinder binder, object value)
{
Console.WriteLine("TrySetMember被调用了,Name:{0}", binder.Name);
bool result = base.TrySetMember(binder, value); return true;
}
 

运行,可以发现不会抛出异常了:

ExpandoObject,DynamicObject,DynamicMetaObject

总结:首先DLR会尝试查找属性的静态定义,如果没有找到则会调用相应的TryXXX 方法,如果TryXXX方法返回false,代表TryXXX方法运行失败,DLR随后会抛出异常。

为了验证是不是这样,将DynamicProduct中属性的静态定义全部注释掉,并且TryXXX方法全部返回True。完整的代码如下:

class DynamicProduct : DynamicObject
{
#region dynamicProduct 的一些属性的静态定义 //private string name;
//public int Id { get; set; } //public void ShowProduct()
//{
// Console.WriteLine("Id={0} ,Name={1}", Id, name);
//} #endregion #region Override DynamicObject 的方法 public override bool TryGetMember(GetMemberBinder binder, out object result)
{
Console.WriteLine("TryGetMember被调用了,Name:{0}", binder.Name);
bool tryResult = base.TryGetMember(binder, out result); return true;
} public override bool TrySetMember(SetMemberBinder binder, object value)
{
Console.WriteLine("TrySetMember被调用了,Name:{0}", binder.Name);
bool tryResult = base.TrySetMember(binder, value); return true;
} public override bool TryInvoke(InvokeBinder binder, object[] args, out object result)
{
Console.WriteLine("TryInvoke被调用了");
bool tryResult = base.TryInvoke(binder, args, out result); return true;
} public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
Console.WriteLine("TryInvokeMember被调用了,Name:{0}", binder.Name);
bool tryResult = base.TryInvokeMember(binder, args, out result); return true;
} #endregion
}
 

Main方法不变:

static void Main(string[] args)
{
dynamic dynProduct = new DynamicProduct(); dynProduct.name = "n1"; //调用TrySetMember方法
dynProduct.Id = 1;
dynProduct.Id = dynProduct.Id + 3;
dynProduct.ShowProduct(); Console.ReadLine();
}

运行结果如下:

ExpandoObject,DynamicObject,DynamicMetaObject

DynamicMetaObject: 表示动态绑定和参与动态绑定的对象的绑定逻辑。

新建类MyDynamicObject:

public class MyMetaObject : DynamicMetaObject
{
public MyMetaObject(Expression parameter, object value)
: base(parameter, BindingRestrictions.Empty, value)
{
} public override DynamicMetaObject BindInvokeMember(InvokeMemberBinder binder, DynamicMetaObject[] args)
{
return this.PrintAndReturnIdentity("InvokeMember of method {0}", binder.Name);
} public override DynamicMetaObject BindSetMember(SetMemberBinder binder, DynamicMetaObject value)
{
return this.PrintAndReturnIdentity("SetMember of property {0}", binder.Name);
} public override DynamicMetaObject BindGetMember(GetMemberBinder binder)
{
return this.PrintAndReturnIdentity("GetMember of property {0}", binder.Name);
} private DynamicMetaObject PrintAndReturnIdentity(string message, string name)
{
Console.WriteLine(String.Format(message, name)); return new DynamicMetaObject(
Expression,
BindingRestrictions.GetTypeRestriction(
Expression,
typeof(MyDynamicObject)));
}
}

Main 方法如下:

static void Main(string[] args)
{
dynamic d = new MyDynamicObject(); d.P3 = d.M1(d.P1, d.M2(d.P2)); Console.ReadLine();
}

运行,结果如下:

ExpandoObject,DynamicObject,DynamicMetaObject

d.P3 = d.M1(d.P1, d.M2(d.P2));

按照从左到右,从里到外的原则。

1:先调用d.P1,DLR会尝试调用d 的GetMetaObject 方法,此方法返回一个MyMetaObject对象。

接着DLR知道你调用的是一个属性,于是它调用返回的MyMetaObject对象的BindGetMember 方法,

输出为GetMember of property P1

2:调用d.P2,和调用d.P1 一样.

3:调用d.M2,同样DLR调用d的GetMetaObject方法,返回一个MyMetaObject对象,接着调用返回对象的BindInvokeMember 方法。

4:….

当前标签: dynamic

 
LoveJenny 2011-07-07 19:58 阅读:2523 评论:4
 
LoveJenny 2011-07-04 19:47 阅读:2168 评论:4
 
LoveJenny 2011-07-04 06:27 阅读:2668 评论:17