在 .NET 平臺使用 ReflectionDynamicObject 優化反射呼叫程式碼

Soar、毅發表於2022-03-01

基於封裝的原則,API 的設計者會將部分成員(屬性、欄位、方法等)隱藏以保證健壯性。但總有需要直接訪問這些私有成員的情況。

為了訪問一個型別的私有成員,除了更改 API 設計還有就是使用反射技術:

public class MyApi
{
	public MyApi()
	{
		_createdAt = DateTime.Now;
	}
	private DateTime _createdAt;
	public int ShowTimes { get; private set; }
	public void ShowCreateTime()
	{
		Console.WriteLine(_createdAt);
		ShowTimes++;
	}
}

void Main()
{
	var api = new MyApi();
	var field = api.GetType().GetField("_createdAt", BindingFlags.NonPublic | BindingFlags.Instance);
	var value = field.GetValue(api);
	Console.WriteLine(value);
}

這種寫法並不優雅:

  1. 程式碼冗長,編寫麻煩。
  2. 實現比較繞,不太直觀。

筆者基於“動態型別技術”探索出了一種相對來說比較優雅的方案用於美化上述程式碼,併為其命名為 ReflectionDynamicObject :

void Main()
{
    var api = new MyApi();
    dynamic wrapper = ReflectionDynamicObject.Wrap(api);
    Console.WriteLine(wrapper._createdAt);
}

除了支援獲取值,ReflectionDynamicObject 還支援賦值:

void Main()
{
    var api = new MyApi();
    dynamic wrapper = ReflectionDynamicObject.Wrap(api);
    wrapper._createdAt = new DateTime(2022, 2, 2, 22, 22, 22);
    api.ShowCreateTime();
}

除了欄位,當然也支援對屬性的操作:

void Main()
{
    var api = new MyApi();
    dynamic wrapper = ReflectionDynamicObject.Wrap(api);
    wrapper.ShowTimes = 100;
    Console.WriteLine(wraper.ShowTimes);
}

在對屬性的支援上,ReflectionDynamicObject 使用了“快速反射”技術,將取值和複製操作生成了委託以優化效能。

ReflectionDynamicObject 的實現原理

ReflectionDynamicObject 派生自 DynamicObject ,其內部通過反射技術獲取到所有的屬性和欄位並對其 getter 和 setter 方法進行儲存並通過 TryGetMember 和 TrySetMember 方法經執行時呼叫。

ReflectionDynamicObject 的原始碼

public sealed class ReflectionDynamicObject : DynamicObject
{
    private readonly object _instance;
    private readonly Accessor _accessor;
    private ReflectionDynamicObject(object instance)
    {
        _instance = instance ?? throw new ArgumentNullException(nameof(instance));
        _accessor = GetAccessor(instance.GetType());
    }
    public static ReflectionDynamicObject Wrap(Object value)
    {
        if (value == null) throw new ArgumentNullException(nameof(value));
        return new ReflectionDynamicObject(value);
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        if (_accessor.TryFindGetter(binder.Name, out var getter))
        {
            result = getter.Get(_instance);
            return true;
        }
        return base.TryGetMember(binder, out result);
    }

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        if (_accessor.TryFindSetter(binder.Name, out var setter))
        {
            setter.Set(_instance, value);
            return true;
        }
        return base.TrySetMember(binder, value);
    }

    #region 快速反射
    private interface IGetter
    {
        object Get(object instance);
    }
    private interface ISetter
    {
        void Set(object instance, object value);
    }

    private class Getter : IGetter
    {
        private FieldInfo _field;
        public Getter(FieldInfo field)
        {
            _field = field ?? throw new ArgumentNullException(nameof(field));
        }
        public object Get(object instance)
        {
            return _field.GetValue(instance);
        }
    }

    private class Setter : ISetter
    {
        private FieldInfo _field;
        public Setter(FieldInfo field)
        {
            _field = field ?? throw new ArgumentNullException(nameof(field));
        }
        public void Set(object instance, object value)
        {
            _field.SetValue(instance, value);
        }
    }

    private class Getter<T1, T2> : IGetter
    {
        private readonly Func<T1, T2> _getter;
        public Getter(Func<T1, T2> getter)
        {
            _getter = getter ?? throw new ArgumentNullException(nameof(getter));
        }
        public object Get(object instance)
        {
            return _getter((T1)instance);
        }
    }

    private class Setter<T1, T2> : ISetter
    {
        private readonly Action<T1, T2> _setter;
        public Setter(Action<T1, T2> setter)
        {
            this._setter = setter ?? throw new ArgumentNullException(nameof(setter));
        }
        public void Set(object instance, object value)
        {
            this._setter.Invoke((T1)instance, (T2)value);
        }
    }

    private class Accessor
    {
        public Accessor(Type type)
        {
            this._type = type ?? throw new ArgumentNullException(nameof(_type));
            var getter = new SortedDictionary<string, IGetter>();
            var setter = new SortedDictionary<string, ISetter>();

            var fields = _type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

            foreach (var field in fields)
            {
                getter[field.Name] = new Getter(field);
                setter[field.Name] = new Setter(field);
            }

            var props = _type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

            foreach (var item in props)
            {
                if (item.CanRead)
                {
                    var method = item.GetMethod;
                    var funcType = typeof(Func<,>).MakeGenericType(item.DeclaringType, item.PropertyType);
                    var func = method.CreateDelegate(funcType);
                    var getterType = typeof(Getter<,>).MakeGenericType(item.DeclaringType, item.PropertyType);
                    var get = (IGetter)Activator.CreateInstance(getterType, func);
                    getter[item.Name] = get;
                }
                if (item.CanWrite)
                {
                    var method = item.SetMethod;
                    var actType = typeof(Action<,>).MakeGenericType(item.DeclaringType, item.PropertyType);
                    var act = method.CreateDelegate(actType);
                    var setterType = typeof(Setter<,>).MakeGenericType(item.DeclaringType, item.PropertyType);
                    var set = (ISetter)Activator.CreateInstance(setterType, act);
                    setter[item.Name] = set;
                }
            }

            _getters = getter;
            _setters = setter;
        }
        private readonly Type _type;
        private readonly IReadOnlyDictionary<string, IGetter> _getters;
        private readonly IReadOnlyDictionary<string, ISetter> _setters;

        public bool TryFindGetter(string name, out IGetter getter) => _getters.TryGetValue(name, out getter);
        public bool TryFindSetter(string name, out ISetter setter) => _setters.TryGetValue(name, out setter);
    }
    private static Dictionary<Type, Accessor> _accessors = new Dictionary<Type, Accessor>();
    private static object _accessorsLock = new object();
    private static Accessor GetAccessor(Type type)
    {
        if (_accessors.TryGetValue(type, out var accessor)) return accessor;
        lock (_accessorsLock)
        {
            if (_accessors.TryGetValue(type, out accessor)) return accessor;
            accessor = new Accessor(type);
            var temp = new Dictionary<Type, Accessor>(_accessors);
            temp[type] = new Accessor(type);
            _accessors = temp;
            return accessor;
        }
    }
    #endregion
}

ReflectionDynamicObject 的侷限性

基於複雜度的考慮,ReflectionDynamicObject 並未新增對“方法”的支援。這也就意味著對方法的呼叫是缺失的。雖然動態行為讓程式擺脫了對字串的依賴,但是該實現對“重構”的支援仍然不友好。

哪裡用到了 ReflectionDynamicObject ?

Liquid 主題引擎 是筆者根據 Liquid 語言和 Shopify 主題機制並採用 Fluid 模板引擎實現的一套 HTML 主題引擎。該引擎允許終端使用者自由的修改自己的主題模板而不會對宿主造成影響。最終目標是做到多語言、多主題、高擴充套件性以及所見即所得。

在編寫 Liquid 主題引擎 時,筆者需要重寫 Fluid 模板引擎的 render 標籤讓子檢視從 snippets 資料夾載入。在實現該標籤時,需要訪問 TemplateContext 的 LocalScope 和 RootScope 欄位,不幸的是上述欄位被標記為了 internal ,無法在外部程式集中訪問到。於是便有了 ReflectionDynamicObject ,幫助筆者完成對 LocalScope 和 RootScope 的訪問。

參考連結

Liquid 模板語言: https://www.coderbusy.com/liquid

Fluid 模板引擎:https://github.com/sebastienros/fluid

Liquid 主題引擎:https://gitee.com/zyingnet_kf/liquid-theme-engine

相關文章