コード例 #1
0
        public object Invoke(object o)
        {
            if (cache.TryGet(o, out var result))
            {
                return(result(o));
            }

            CallsiteFunc <object, object> f = null;

            var oType = o == null ? typeof(Object) : o.GetType();
            var field = oType.GetField(MemberName);

            if (field != null)
            {
                f = DelegateHelpers.GetZeroArityDelegate(field);
            }
            var property = oType.GetProperty(MemberName);

            if (property != null)
            {
                f = DelegateHelpers.GetZeroArityDelegate(property);
            }
            var method = oType.GetMethod(MemberName, Type.EmptyTypes);

            if (method != null)
            {
                f = DelegateHelpers.GetZeroArityDelegate(method);
            }
            if (f != null)
            {
                cache.CacheMethod(o, f);
                return(f(o));
            }
            throw new ArgumentException($"Could not invoke zero arity member `{MemberName}` on target {(o == null ? "null" : o.ToString())}.");
        }
コード例 #2
0
        public object Invoke(object arg0)
        {
            if (cache.TryGet(arg0, out var result))
            {
                return(result(arg0));
            }

            var method = Dispatch.BindToMethod(BindingFlags.Public | BindingFlags.Instance, Target, MemberName, new [] { arg0 });

            if (method != null)
            {
                var f = DelegateHelpers.GetMethodDelegate01(method);
                cache.CacheMethod(arg0, f);
                return(f(arg0));
            }
            throw new ArgumentException($"Could not invoke static method `{MemberName}` on type {Target} with argument types {arg0.GetType()}.");
        }
コード例 #3
0
        public T Invoke(object arg0)
        {
            if (cache.TryGet(arg0, out var result))
            {
                return((T)result(arg0));
            }

            var ctor = Dispatch.BindToConstructor(typeof(T), new [] { arg0 });

            if (ctor != null)
            {
                var f = DelegateHelpers.GetMethodDelegate01(ctor);
                cache.CacheMethod(arg0, f);
                return((T)f(arg0));
            }
            throw new ArgumentException($"Could not invoke constructor `{typeof(T)}` with types {arg0.GetType()}.");
        }