English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Detailed Explanation of C# Dynamic Object (dynamic) (Implementation Methods and Dynamic Properties)

C#的动态对象的属性实现比较简单,如果要实现动态语言那种动态方法就比较困难,因为对于dynamic对象,扩展方法,匿名方法都是不能用直接的,这里还是利用对象和委托来模拟这种动态方法的实现,看起来有点javascript的对象味道:

1) 定义一个委托,参数个数可变,参数都是object类型:这里的委托多有个dynamic参数,代表调用这个委托的动态对象本身。

public delegate object MyDelegate(dynamic Sender, params object[] PMs);

2) 定义一个委托转载对象,因为dynamic对象不能直接用匿名方法,这里用对象去承载:

public class DelegateObj
  {
    private MyDelegate _delegate;
    public MyDelegate CallMethod
    {
      get { return _delegate; }
    }
    private DelegateObj(MyDelegate D)
    {
      _delegate = D;
    }
    /// <summary>
    /// 构造委托对象,让它看起来有点javascript定义的味道.
    /// </summary>
    /// <param name="D"></param>
    /// <returns></returns>
    public static DelegateObj Function(MyDelegate D)
    {
      return new DelegateObj(D);
    }
  }

3) 定义一个动态对象:

public class DynObj : DynamicObject
  {
    //保存对象动态定义的属性值
    private Dictionary<string, object> _values;
    public DynObj()
    {
      _values = new Dictionary<string, object>();
    }
    /// <summary>
    /// 获取属性值
    /// </summary>
    /// <param name="propertyName"></param>
    /// <returns></returns>
    public object GetPropertyValue(string propertyName)
    {
      if (_values.ContainsKey(propertyName) == true)
      {
        return _values[propertyName];
      }
      return null;
    }
    /// <summary>
    /// Set property value
    /// </summary>
    /// <param name="propertyName"></param>
    /// <param name="value"></param>
    public void SetPropertyValue(string propertyName,object value)
    {
      if (_values.ContainsKey(propertyName) == true)
      {
        _values[propertyName] = value;
      }
      else
      {
        _values.Add(propertyName, value);
      }
    }
    /// <summary>
    /// Method to implement accessing a dynamic object's property members, getting the value of the specified property
    /// </summary>
    /// <param name="binder"></param>
    /// <param name="result"></param>
    /// <returns></returns>
    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
      result = GetPropertyValue(binder.Name);
      return result == null ? false : true;
    }
    /// <summary>
    /// Method to implement setting the value of a dynamic object's property.
    /// </summary>
    /// <param name="binder"></param>
    /// <param name="value"></param>
    /// <returns></returns>
    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
      SetPropertyValue(binder.Name, value);
      return true;
    }
    /// <summary>
    /// The actual code executed when dynamically invoking an object's method
    /// </summary>
    /// <param name="binder"></param>
    /// <param name="args"></param>
    /// <param name="result"></param>
    /// <returns></returns>
    public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
    {
      var theDelegateObj = GetPropertyValue(binder.Name) as DelegateObj;
      if (theDelegateObj == null || theDelegateObj.CallMethod == null)
      {
        result = null;
        return false;
      }
      result = theDelegateObj.CallMethod(this,args);
      return true;
    }
    public override bool TryInvoke(InvokeBinder binder, object[] args, out object result)
    {
      return base.TryInvoke(binder, args, out result);
    }
  }

Application test code:

dynamic theObj = new DynObj();
      theObj.aaa = "this is a test";//Dynamic properties
      //Dynamic methods, you cannot define parameters arbitrarily here. When calling, it can be any number of parameters, and the specific parameter types and meanings can only be carefully handled by yourself.
      theObj.show = DelegateObj.Function((s, pms) =>
      {
        if (pms != null && pms.Length > 0)
        {
          MessageBox.Show(pms[0].ToString()) + : + s.aaa);
        }
        else
        {
          MessageBox.Show(s.aaa);
        }
        return null;
      }
      );
theObj.show("hello");

Although it looks a bit like the Js object definition method, since C# is a static language, the dynamic simulation mechanism provided is still limited. It looks dynamic, but all values and methods need to be written by themselves to handle.

The above code in vs2010,windows 2008 server, framework4.0 is tested OK.

This C# dynamic object (dynamic) explanation (implementation methods and dynamic properties) is all the content I share with you. I hope it can be a reference for you, and I also hope everyone will support the Yelling Tutorial more.

You May Also Like