Пример #1
0
 /// <summary>
 /// Creates a new proxy instance, with proxyTarget as the proxied object
 /// </summary>
 /// <param name="proxyTarget">The object to proxy</param>
 /// <param name="strict">Should type support (for casts) be strict or loose</param>
 /// <param name="supportedTypes">A List of supported types. Only used if strict is true. May be null</param>
 protected internal DynamicProxyImpl(object proxyTarget, InvocationDelegate invocationHandler, bool strict, Type[] supportedTypes) : base(typeof(IDynamicProxy))
 {
     this.proxyTarget       = proxyTarget;
     this.invocationHandler = invocationHandler;
     this.strict            = strict;
     this.supportedTypes    = supportedTypes;
 }
Пример #2
0
 static public Object BuildProxy(InvocationDelegate invocationHandler, Object data, params Type[] interfaces)
 {
     if (interfaces.Length == 0)
     {
         throw new ArgumentOutOfRangeException("interfaces must contain at least 1 interface type");
     }
     return(createInstance(invocationHandler, data, recurseInterfaces(interfaces)));
 }
 /// <summary>
 /// Creates a new proxy instance, with proxyTarget as the proxied object
 /// </summary>
 /// <param name="proxyTarget">The object to proxy</param>
 /// <param name="strict">Should type support (for casts) be strict or loose</param>
 /// <param name="supportedTypes">A List of supported types. Only used if strict is true. May be null</param>
 protected internal InterfaceDynamicProxyImpl(object proxyTarget, InvocationDelegate invocationHandler, bool strict, Type[] supportedTypes)
     : base(typeof(IDynamicProxy))
 {
     this.proxyTarget = proxyTarget;
     this.invocationHandler = invocationHandler;
     this.strict = strict;
     this.supportedTypes = supportedTypes;
 }
        public MarshallableProxy(Type type1, MarshalByRefObject targetObject, InvocationDelegate invoker)
            : base(type1)
        {
            ProxyTargetTyped = targetObject;
            InvocationHandler = invoker;

            ObjRef myObjRef = RemotingServices.Marshal(ProxyTargetTyped);
            URI = myObjRef.URI;
        }
        /// <summary>
        /// Create a proxy for the target object
        /// </summary>
        /// <param name="target">The object to create a proxy for</param>
        /// <param name="invocationHandler">The invocation handler for the proxy</param>
        /// <param name="strict">Indicates if the cast support should be strict. If strict is true all casts are checked before being performed. The supportedType list will enabled support for more interfaces than the target object supports</param>
        /// <param name="supportedTypes">List of types that are supported for casts. Is only checked if strict is true.</param>
        /// <returns>The dynamic proxy instance</returns>
        public object CreateProxy(object target, InvocationDelegate invocationHandler, bool strict, Type[] supportedTypes)
        {
            DynamicProxyImpl dynamicProxyImpl = new DynamicProxyImpl(target, invocationHandler, strict, supportedTypes);

            // The transparent proxy provides the illusion that the actual object resides in the client's space.
            object transparentProxy = dynamicProxyImpl.GetTransparentProxy();

            return(transparentProxy);
        }
Пример #6
0
        public void TestCreateConstructor5()
        {
            new ExampleClass1();
            Assert.IsNull(default(ExampleClass1));
            ConstructorInfo    ci     = Accelerator.RetrieveStandardConstructor(typeof(ExampleClass1));
            InvocationDelegate create = Accelerator.CreateInvocationDelegate(ci);

            Assert.IsNotNull(create);
            Assert.IsTrue(create() is ExampleClass1);
        }
        public MarshallableProxy(Type type1, MarshalByRefObject targetObject, InvocationDelegate invoker)
            : base(type1)
        {
            ProxyTargetTyped  = targetObject;
            InvocationHandler = invoker;

            ObjRef myObjRef = RemotingServices.Marshal(ProxyTargetTyped);

            URI = myObjRef.URI;
        }
        public InvocationDelegate Build()
        {
            InvocationDelegate app = invocation => invocation.InvokeOnTarget();

            foreach (var component in _components.Reverse())
            {
                app = component(app);
            }

            return(app);
        }
        public void TestPropertySetCachedResult()
        {
            Type        t     = typeof(Sample1);
            MethodCache cache = new MethodCache();

            PropertyInfo       p1 = t.GetProperty("Prop");
            InvocationDelegate d1 = cache.GetSetter(p1);

            PropertyInfo       p2 = t.GetProperty("Prop");
            InvocationDelegate d2 = cache.GetSetter(p2);

            Assert.AreSame(p1, p2);
            Assert.AreSame(d1, d2);
        }
        public void TestMethodCachedResult()
        {
            Type        t     = typeof(Sample1);
            MethodCache cache = new MethodCache();

            MethodInfo         m1 = t.GetMethod("Method");
            InvocationDelegate d1 = cache.GetInvoker(m1);

            MethodInfo         m2 = t.GetMethod("Method");
            InvocationDelegate d2 = cache.GetInvoker(m2);

            Assert.AreSame(m1, m2);
            Assert.AreSame(d1, d2);
        }
Пример #11
0
        /// <summary>
        /// Creates an instance of a class.  The class type is determined by the set of interfaces passed.
        /// For each set passed there is 1 type created.  All instances are then created from the same type.
        /// </summary>
        /// <param name="invoker">delegate for the callback on any function or property setter/getter</param>
        /// <param name="data">any data the user wishes to store in the proxy instance</param>
        /// <param name="interfaces">set of interfaces to implement</param>
        /// <returns>an instance of the proxy with the bound callback and data</returns>
        static internal Object  createInstance(InvocationDelegate invoker, Object data, Type[] interfaces)
        {
            Type  newType = getClassFor(interfaces);
            Proxy proxy   = (Proxy)Activator.CreateInstance(newType, true);

            proxy._invocationDelegate = invoker;
            proxy.data = data;
            if (isDelegate(interfaces))
            {
                return(proxy.target = Delegate.CreateDelegate(interfaces[0], proxy, "Invoke"));
            }
            else
            {
                return(proxy.target = proxy);
            }
        }
Пример #12
0
        /// <summary>
        /// Creates a new proxy instance, with proxyTarget as the proxied object
        /// </summary>
        /// <param name="proxyTarget">The object to proxy</param>
        /// <param name="strict">Should type support (for casts) be strict or loose</param>
        /// <param name="supportedTypes">A List of supported types. Only used if strict is true. May be null</param>
        protected internal DynamicProxyImpl(object proxyTarget, InvocationDelegate invocationHandler, bool strict, Type[] supportedTypes) : base(typeof(IDynamicProxy))
        {
            if (proxyTarget == null)
            {
                throw new ArgumentNullException("proxyTarget");
            }
            if (!(proxyTarget is MarshalByRefObject))
            {
                throw new ArgumentException("The proxy target is of type '" + proxyTarget.GetType().FullName
                                            + "', which does not inherit from System.MarshalByRefObject.", "proxyTarget");
            }

            this.proxyTarget       = proxyTarget;
            this.invocationHandler = invocationHandler;
            this.strict            = strict;
            this.supportedTypes    = supportedTypes;
        }
        public async Task InterceptAsync(IInvocationV2 invocation, InvocationDelegate proceed)
        {
            await Task.Delay(500);

            Console.WriteLine($"-> {_id}");

            proceed(invocation);
            await(Task) invocation.ReturnValue;

            Console.WriteLine($"- retry ({_id}) -");
            proceed(invocation);
            await(Task) invocation.ReturnValue;

            await Task.Delay(500);

            Console.WriteLine($"<- {_id}");
        }
Пример #14
0
        public void TestInvokeInstanceFunction2()
        {
            string             n      = nameof(ClassWithConstructors.Update);
            InvocationDelegate update = typeof(ClassWithConstructors).CreateInvocationDelegate(n, new[] { typeof(int), typeof(string) });

            var o = new ClassWithConstructors();

            Assert.AreEqual(0, o.Value);
            Assert.AreEqual(null, o.Text);

            o.Update(123, "ABC");
            Assert.AreEqual(123, o.Value);
            Assert.AreEqual("ABC", o.Text);

            update(o, 456, "XYZ");
            Assert.AreEqual(456, o.Value);
            Assert.AreEqual("XYZ", o.Text);
        }
Пример #15
0
        public void TestInvokeInstanceFunction1()
        {
            string             n     = nameof(ClassWithConstructors.Reset);
            InvocationDelegate reset = typeof(ClassWithConstructors).CreateInvocationDelegate(n);

            var o = new ClassWithConstructors();

            o.Update(123, "ABC");
            Assert.AreEqual(123, o.Value);
            Assert.AreEqual("ABC", o.Text);
            o.Reset();
            Assert.AreEqual(0, o.Value);
            Assert.AreEqual(null, o.Text);

            o.Update(123, "ABC");
            Assert.IsNull(reset(o));
            Assert.AreEqual(0, o.Value);
            Assert.AreEqual(null, o.Text);
        }
Пример #16
0
        public void TestInvokeConstructor()
        {
            Type               t      = typeof(ClassWithConstructors);
            ConstructorInfo    ci     = t.GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, new[] { typeof(int), typeof(string) }, null);
            InvocationDelegate create = Accelerator.CreateInvocationDelegate(ci);

            Assert.IsNotNull(create);

            var o = new ClassWithConstructors(123, "ABC");

            Assert.AreEqual(123, o.Value);
            Assert.AreEqual("ABC", o.Text);

            o = (ClassWithConstructors)create(123, "ABC");
            Assert.AreEqual(123, o.Value);
            Assert.AreEqual("ABC", o.Text);

            o = (ClassWithConstructors)create(0, null);
            Assert.AreEqual(0, o.Value);
            Assert.AreEqual(null, o.Text);
        }
Пример #17
0
        public object EvalExpression(FSMContext context, object expr)
        {
            var ctx = new FSMExpressionContextAdapter(context);
            InvocationDelegate eval = null;

            if (expr is string)
            {
                // eval = GetOrCacheExprEval(ctx, expr as string);
            }
            else
            {
                eval = GetOrCacheExprEval(ctx, expr as Expression);
            }
            if (eval == null)
            {
                return(null);
            }
            object result = eval(ctx);

            return(result);
        }
Пример #18
0
        public void TestInvokeFactoryFunction()
        {
            string             n      = nameof(ClassWithConstructors.Create);
            InvocationDelegate create = typeof(ClassWithConstructors).CreateInvocationDelegate(n, new[] { typeof(int), typeof(string) });

            var o = ClassWithConstructors.Create(0, null);

            Assert.AreEqual(0, o.Value);
            Assert.AreEqual(null, o.Text);

            o = ClassWithConstructors.Create(123, "ABC");
            Assert.AreEqual(123, o.Value);
            Assert.AreEqual("ABC", o.Text);

            o = (ClassWithConstructors)create(0, null);
            Assert.AreEqual(0, o.Value);
            Assert.AreEqual(null, o.Text);

            o = (ClassWithConstructors)create(123, "ABC");
            Assert.AreEqual(123, o.Value);
            Assert.AreEqual("ABC", o.Text);
        }
 /// <summary>
 /// Create a proxy for the target object
 /// </summary>
 /// <param name="target">The object to create a proxy for</param>
 /// <param name="invocationHandler">The invocation handler for the proxy</param>
 /// <param name="strict">Indicates if the cast support should be strict. If strict is true all casts are checked before being performed. The supportedType list will enabled support for more interfaces than the target object supports</param>
 /// <param name="supportedTypes">List of types that are supported for casts. Is only checked if strict is true.</param>
 /// <returns>The dynamic proxy instance</returns>
 public object CreateProxy(object target, InvocationDelegate invocationHandler, bool strict, Type[] supportedTypes)
 {
     return(new DynamicProxyImpl(target, invocationHandler, strict, supportedTypes).GetTransparentProxy());
 }
 /// <summary>
 /// Create a proxy for the target object
 /// </summary>
 /// <param name="target">The object to create a proxy for</param>
 /// <param name="invocationHandler">The invocation handler for the proxy</param>
 /// <param name="strict">Indicates if the cast support should be strict. If strict is true all casts are checked before being performed</param>
 /// <returns>The dynamic proxy instance</returns>
 public object CreateProxy(object target, InvocationDelegate invocationHandler, bool strict)
 {
     return(CreateProxy(target, invocationHandler, strict, null));
 }
 /// <summary>
 /// Create a proxy for the target object
 /// </summary>
 /// <param name="target">The object to create a proxy for</param>
 /// <param name="invocationHandler">The invocation handler for the proxy</param>
 /// <returns>The dynamic proxy instance</returns>
 public object CreateProxy(object target, InvocationDelegate invocationHandler)
 {
     return(CreateProxy(target, invocationHandler, false, null));
 }
Пример #22
0
        public static T MonoCast <T>(this IntPtr ptr)
        {
            #region MonoCast [IntPtr to Object]
            if (typeof(T) == typeof(string))
            {
                return((T)(object)IL2Import.IntPtrToString(ptr));
            }
            else if (BlazeTools.Types.unmanagedTypes.Contains(typeof(T)))
            {
                unsafe
                {
                    // SByte
                    if (typeof(T) == typeof(sbyte))
                    {
                        return((T)(object)ptr.Unbox <sbyte>());
                    }
                    // Byte
                    if (typeof(T) == typeof(byte))
                    {
                        return((T)(object)ptr.Unbox <byte>());
                    }
                    // Short
                    if (typeof(T) == typeof(short))
                    {
                        return((T)(object)ptr.Unbox <short>());
                    }
                    // UShort
                    if (typeof(T) == typeof(ushort))
                    {
                        return((T)(object)ptr.Unbox <ushort>());
                    }
                    // Int
                    if (typeof(T) == typeof(int))
                    {
                        return((T)(object)ptr.Unbox <int>());
                    }
                    // UInt
                    if (typeof(T) == typeof(uint))
                    {
                        return((T)(object)ptr.Unbox <uint>());
                    }
                    // Long
                    if (typeof(T) == typeof(long))
                    {
                        return((T)(object)ptr.Unbox <long>());
                    }
                    // ULong
                    if (typeof(T) == typeof(ulong))
                    {
                        return((T)(object)ptr.Unbox <ulong>());
                    }
                    // Char
                    if (typeof(T) == typeof(char))
                    {
                        return((T)(object)ptr.Unbox <char>());
                    }
                    // Float
                    if (typeof(T) == typeof(float))
                    {
                        return((T)(object)ptr.Unbox <float>());
                    }
                    // Double
                    if (typeof(T) == typeof(double))
                    {
                        return((T)(object)ptr.Unbox <double>());
                    }
                    // Decimal
                    if (typeof(T) == typeof(decimal))
                    {
                        return((T)(object)ptr.Unbox <decimal>());
                    }
                    // Bool
                    if (typeof(T) == typeof(bool))
                    {
                        return((T)(object)ptr.Unbox <bool>());
                    }
                }
            }
            else if (BlazeTools.Types.otherUnmanagedTypes.Contains(typeof(T)))
            {
                unsafe
                {
                    // UnityEngine.Color
                    if (typeof(T) == typeof(UnityEngine.Color))
                    {
                        return((T)(object)ptr.Unbox <UnityEngine.Color>());
                    }
                    // UnityEngine.Mathf
                    if (typeof(T) == typeof(UnityEngine.Mathf))
                    {
                        return((T)(object)ptr.Unbox <UnityEngine.Mathf>());
                    }
                    // UnityEngine.Quaternion
                    if (typeof(T) == typeof(UnityEngine.Quaternion))
                    {
                        return((T)(object)ptr.Unbox <UnityEngine.Quaternion>());
                    }
                    // UnityEngine.Ray
                    if (typeof(T) == typeof(UnityEngine.Ray))
                    {
                        return((T)(object)ptr.Unbox <UnityEngine.Ray>());
                    }
                    // UnityEngine.RaycastHit
                    if (typeof(T) == typeof(UnityEngine.RaycastHit))
                    {
                        return((T)(object)ptr.Unbox <UnityEngine.RaycastHit>());
                    }
                    // UnityEngine.Rect
                    if (typeof(T) == typeof(UnityEngine.Rect))
                    {
                        return((T)(object)ptr.Unbox <UnityEngine.Rect>());
                    }
                    // UnityEngine.Vector2
                    if (typeof(T) == typeof(UnityEngine.Vector2))
                    {
                        return((T)(object)ptr.Unbox <UnityEngine.Vector2>());
                    }
                    // UnityEngine.Vector3
                    if (typeof(T) == typeof(UnityEngine.Vector3))
                    {
                        return((T)(object)ptr.Unbox <UnityEngine.Vector3>());
                    }
                    // UnityEngine.Vector4
                    if (typeof(T) == typeof(UnityEngine.Vector4))
                    {
                        return((T)(object)ptr.Unbox <UnityEngine.Vector4>());
                    }
                }
            }
            else if (typeof(T).IsEnum)
            {
                return((T)(object)ptr.Unbox <int>());
            }

            InvocationDelegate fastInvoke = GetMethodInvoker(typeof(T).GetConstructors().First(x => x.GetParameters().Length == 1));
            return((T)fastInvoke(null, new object[] { ptr }));

            #endregion
        }
 public void Intercept(IInvocationV2 invocation, InvocationDelegate proceed)
 {
     invocation.ReturnValue = InterceptAsync(invocation, proceed);
 }
Пример #24
0
 public MarshallableProxy(MarshalByRefObject targetObject, InvocationDelegate invoker)
     : this(targetObject.GetType(), targetObject, invoker)
 {
 }
Пример #25
0
 public EasyProxy(InvocationDelegate handler)
 {
     this.handler = handler;
 }
Пример #26
0
 public MarshallableProxy(MarshalByRefObject targetObject, InvocationDelegate invoker)
     : this(targetObject.GetType(), targetObject, invoker)
 {
 }
        /// <summary>

        /// Create a proxy for the target object

        /// </summary>

        /// <param name="target">The object to create a proxy for</param>

        /// <param name="invocationHandler">The invocation handler for the proxy</param>

        /// <param name="strict">Indicates if the cast support should be strict. If strict is true all casts are checked before being performed. The supportedType list will enabled support for more interfaces than the target object supports</param>

        /// <param name="supportedTypes">List of types that are supported for casts. Is only checked if strict is true.</param>

        /// <returns>The dynamic proxy instance</returns>

        public object CreateProxy(object target, InvocationDelegate invocationHandler, bool strict, Type[] supportedTypes) {

            return new DynamicProxyImpl(target, invocationHandler, strict, supportedTypes).GetTransparentProxy();

        }
        /// <summary>

        /// Create a proxy for the target object

        /// </summary>

        /// <param name="target">The object to create a proxy for</param>

        /// <param name="invocationHandler">The invocation handler for the proxy</param>

        /// <param name="strict">Indicates if the cast support should be strict. If strict is true all casts are checked before being performed</param>

        /// <returns>The dynamic proxy instance</returns>

        public object CreateProxy(object target, InvocationDelegate invocationHandler, bool strict) {

            return CreateProxy(target, invocationHandler, strict, null);

        }
        /// <summary>

        /// Create a proxy for the target object

        /// </summary>

        /// <param name="target">The object to create a proxy for</param>

        /// <param name="invocationHandler">The invocation handler for the proxy</param>

        /// <returns>The dynamic proxy instance</returns>

        public object CreateProxy(object target, InvocationDelegate invocationHandler) {

            return CreateProxy(target, invocationHandler, false, null);

        }
Пример #30
0
 public virtual ValueTask <object> InvokeMethodAsync(InvocationContext context, InvocationDelegate next) => next(context);
Пример #31
0
 /// <summary>
 /// Creates an instance of a class.  The class type is determined by the set of interfaces passed.
 /// For each set passed there is 1 type created.  All instances are then created from the same type.
 /// </summary>
 /// <param name="invoker">delegate for the callback on any function or property setter/getter</param>
 /// <param name="data">any data the user wishes to store in the proxy instance</param>
 /// <param name="interfaces">set of interfaces to implement</param>
 /// <returns>an instance of the proxy with the bound callback and data</returns>
 internal static Object createInstance(InvocationDelegate invoker,Object data,Type[] interfaces)
 {
     Type	newType				=	getClassFor(interfaces);
     Proxy	proxy				=	(Proxy)Activator.CreateInstance(newType,true);
     proxy._invocationDelegate	=	invoker;
     proxy.data					=	data;
     if(isDelegate(interfaces))
         return	proxy.target	=	Delegate.CreateDelegate(interfaces[0],proxy,"Invoke");
     else
         return	proxy.target	=	proxy;
 }
Пример #32
0
 protected internal InterfaceProxy(object proxyTarget, InvocationDelegate invocationHandler)
     : base(typeof(IDynamicProxy))
 {
     this.proxyTarget       = proxyTarget;
     this.invocationHandler = invocationHandler;
 }
Пример #33
0
 public object CreateMarshallableProxy(MarshalByRefObject target, InvocationDelegate invocationHandler)
 {
     return new MarshallableDynamicProxyImpl(target, invocationHandler).GetTransparentProxy();
 }
 public InterceptorAdapter(InvocationDelegate invocationDelegate)
 {
     _invocationDelegate = invocationDelegate;
 }
Пример #35
0
 public static Object BuildProxy(InvocationDelegate invocationHandler,Object data,params Type[]	interfaces)
 {
     if(interfaces.Length==0)
         throw new ArgumentOutOfRangeException("interfaces must contain at least 1 interface type");
     return	createInstance(invocationHandler,data,recurseInterfaces(interfaces));
 }
Пример #36
0
 protected internal InterfaceProxy(object proxyTarget, InvocationDelegate invocationHandler)
     : base(typeof(IDynamicProxy))
 {
     this.proxyTarget = proxyTarget;
     this.invocationHandler = invocationHandler;
 }