Esempio n. 1
0
        public void CanCreateProxyForInterface()
        {
            // Arrange
            var          generator = new ProxyGenerator();
            InterfaceMap map       = new InterfaceMap()
            {
                Subject = new Properties()
            };

            MatchingInterceptor <IProperties> interceptor;
            IProperties proxy;

            // Act
            interceptor = new MatchingInterceptor <IProperties>(map);

            proxy      = generator.CreateInterfaceProxyWithoutTarget <IProperties>(interceptor);
            proxy.Age  = 10;
            proxy.Data = "data";
            proxy.Name = "name";

            // Assert
            Verify.That(proxy).IsNotNull().Now();
            Verify.That(proxy.Age).IsEqualTo(10).Now();
            Verify.That(proxy.Data).IsEqualTo("data").Now();
            Verify.That(proxy.Name).IsEqualTo("name").Now();
        }
Esempio n. 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ProxyBuilder{TSubject}" /> class.
 /// </summary>
 /// <param name="generator">The generator.</param>
 /// <param name="subject">The subject.</param>
 public ProxyBuilder(ProxyGenerator generator, TSubject subject)
 {
     this.generator = generator;
     this.map       = new InterfaceMap
     {
         Subject = subject
     };
 }
        internal virtual IInterfaceMap CreateIInterfaceMap()
        {
            IInterfaceMap target = new InterfaceMap()
            {
                Subject = new TestClass()
            };

            return(target);
        }
Esempio n. 4
0
        ////////////////////
        ///
        ///   Methods to convert Interfaces to Java equivalents
        ///
        ////////////////////

        // Takes a list of interface names e.g. [System.Collections.Generic.ICollection, System.IEnumerable]
        // and a list of type variables to substitute into fragments and returns a string containing the
        // required methods
        // TypeVars in fragments have names T, T1, T2, T3, etc.
        public string getMethods(string iface, IList <string> tyArgs)
        {
            string ret = "";

            if (InterfaceMap.ContainsKey(iface))
            {
                string methods = InterfaceMap[iface];
                methods = rewriteCodeFragment(methods, tyArgs);
                ret    += methods;
            }

            return(ret);
        }
Esempio n. 5
0
        public void TestIfANotFoundPropertyCanBeRedirected()
        {
            // Arrange
            var          generator = new ProxyGenerator();
            InterfaceMap map       = new InterfaceMap()
            {
                Subject = new MoreProperties()
                {
                    UnmappedProperty = "5"
                }
            };

            MethodMapping mapping = new MethodMapping()
            {
                Subject = (proxied, parameters) =>
                {
                    MoreProperties subject = (MoreProperties)proxied;
                    return(int.Parse(subject.UnmappedProperty));
                },
                Name                 = "get_ExtraGetProperty",
                ArgumentTypes        = Type.EmptyTypes,
                GenericArgumentTypes = Type.EmptyTypes
            };

            map.Add(mapping);


            MatchingInterceptor <IMoreProperties> interceptor = new MatchingInterceptor <IMoreProperties>(map);

            int expected = 5;
            int actual   = 0;

            // Act
            IMoreProperties proxy  = generator.CreateInterfaceProxyWithoutTarget <IMoreProperties>(interceptor);
            TestDelegate    getter = () =>
            {
                actual = proxy.ExtraGetProperty;
            };

            // Assert
            Assert.That(proxy, Is.Not.Null);
            Assert.That(getter, Throws.Nothing);
            Assert.That(actual, Is.EqualTo(expected));
        }
Esempio n. 6
0
        static InterfaceMap <I> GetMap <I>() where I : class
        {
            System.Type iType = typeof(I);

            InterfaceMap     _map;
            InterfaceMap <I> map;

            if (!Maps.TryGetValue(iType, out _map))
            {
                //create a new generic map and return it
                map         = new InterfaceMap <I>();
                Maps[iType] = map;
            }
            else
            {
                //cast to the generic type
                map = _map as InterfaceMap <I>;
            }
            return(map);
        }
Esempio n. 7
0
        public void TestIfItFailsWhenItCantFindEquivalent()
        {
            // Arrange
            var          generator = new ProxyGenerator();
            InterfaceMap map       = new InterfaceMap()
            {
                Subject = new Properties()
            };

            MatchingInterceptor <IMoreProperties> interceptor;
            IMoreProperties proxy;

            // Act
            interceptor = new MatchingInterceptor <IMoreProperties>(map);
            proxy       = generator.CreateInterfaceProxyWithoutTarget <IMoreProperties>(interceptor);
            Func <int> getter = () =>
            {
                return(proxy.ExtraGetProperty);
            };

            // Assert
            Verify.That(proxy).IsNotNull().Now();
            Verify.That(getter).ThrowsException("The property ExtraProperty should fail to be found!");
        }
Esempio n. 8
0
        /// <summary>
        /// Returns the interface of the specified type attached to this GameObject.
        /// </summary>
        /// <typeparam name="I">Interface type.</typeparam>
        /// <param name="gameObject">GameObject to query.</param>
        /// <returns>Interface instance if found, otherwise null.</returns>
        public static I Get <I>(GameObject gameObject) where I : class
        {
            InterfaceMap <I> map = GetMap <I>();

            return(map.Get(gameObject));
        }
Esempio n. 9
0
 public HelloWorldContext(MonoBehaviour contextView, bool autostart, InterfaceMap map)
     : base(contextView, autostart)
 {
     _map = map;
 }
Esempio n. 10
0
 public ProxyBuilder(ProxyGenerator generator, InterfaceMap map)
 {
     this.generator = generator;
     this.map       = map;
 }