WEB开发网
开发学院软件开发C语言 [C# 4.0] 2. Dynamic (2) 阅读

[C# 4.0] 2. Dynamic (2)

 2010-09-30 21:04:19 来源:WEB开发网   
核心提示: DynamicObject 和 ExpandoObject 都实现了 IDynamicMetaObjectProvider 接口,这是 DLR 进行动态操作所必须的,[C# 4.0] 2. Dynamic (2)(3),publicclassDynamicObject:IDynamicMetaO

DynamicObject 和 ExpandoObject 都实现了 IDynamicMetaObjectProvider 接口,这是 DLR 进行动态操作所必须的。

public class DynamicObject : IDynamicMetaObjectProvider
{
  // Methods
  protected DynamicObject();
  public virtual IEnumerable<string> GetDynamicMemberNames();
  public virtual DynamicMetaObject GetMetaObject(Expression parameter);
  public virtual bool TryBinaryOperation(BinaryOperationBinder binder, object arg, out object result);
  public virtual bool TryConvert(ConvertBinder binder, out object result);
  public virtual bool TryCreateInstance(CreateInstanceBinder binder, object[] args, out object result);
  public virtual bool TryDeleteIndex(DeleteIndexBinder binder, object[] indexes);
  public virtual bool TryDeleteMember(DeleteMemberBinder binder);
  public virtual bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result);
  public virtual bool TryGetMember(GetMemberBinder binder, out object result);
  public virtual bool TryInvoke(InvokeBinder binder, object[] args, out object result);
  public virtual bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result);
  public virtual bool TrySetIndex(SetIndexBinder binder, object[] indexes, object value);
  public virtual bool TrySetMember(SetMemberBinder binder, object value);
  public virtual bool TryUnaryOperation(UnaryOperationBinder binder, out object result);
}
 
public sealed class ExpandoObject : IDynamicMetaObjectProvider, IDictionary<string, object>, 
  ICollection<KeyValuePair<string, object>>, IEnumerable<KeyValuePair<string, object>>, 
  IEnumerable, INotifyPropertyChanged
{
  // Events
  event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged;
  // Methods
  public ExpandoObject();
  void ICollection<KeyValuePair<string, object>>.Add(KeyValuePair<string, object> item);
  void ICollection<KeyValuePair<string, object>>.Clear();
  bool ICollection<KeyValuePair<string, object>>.Contains(KeyValuePair<string, object> item);
  void ICollection<KeyValuePair<string, object>>.CopyTo(KeyValuePair<string, object>[] array, int arrayIndex);
  bool ICollection<KeyValuePair<string, object>>.Remove(KeyValuePair<string, object> item);
  void IDictionary<string, object>.Add(string key, object value);
  bool IDictionary<string, object>.ContainsKey(string key);
  bool IDictionary<string, object>.Remove(string key);
  bool IDictionary<string, object>.TryGetValue(string key, out object value);
  IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator();
  IEnumerator IEnumerable.GetEnumerator();
  
  DynamicMetaObject IDynamicMetaObjectProvider.GetMetaObject(Expression parameter);
  // Properties
  int ICollection<KeyValuePair<string, object>>.Count { get; }
  bool ICollection<KeyValuePair<string, object>>.IsReadOnly { get; }
  object IDictionary<string, object>.this[string key] { get; set; }
  ICollection<string> IDictionary<string, object>.Keys { get; }
  ICollection<object> IDictionary<string, object>.Values { get; }
}

DynamicObject 集中实现成员调用,可以更灵活地调度。而 ExpandoObject 则更轻便,更接近于动态语言编程习惯。

上一页  1 2 3 

Tags:Dynamic

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接