Example #1
0
        protected DynamicVisitor()
        {
            ShouldCallBaseTypeVisitors = false;
            ShouldCallMultipleVisitors = false;

            _itemTypeVisitors = _itemTypeVisitorsCache.GetOrAdd(GetType(), new ConcurrentDictionary <Type, List <VisitorMethodInfo> >());

            // get and cache all the visitor methods for each visitor type
            _typeVisitorMethods = _typeVisitorMethodsCache.GetOrAdd(GetType(), t =>
            {
                var list = new List <VisitorMethodInfo>();

                foreach (var method in t.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly).Where(m => m.Name == "Visit"))
                {
                    var parameters = method.GetParameters();
                    if (parameters.Length == 1)
                    {
                        list.Add(new VisitorMethodInfo(parameters[0].ParameterType, DelegateFactory.CreateMethod(method)));
                    }
                    if (parameters.Length == 2 && parameters[1].ParameterType == typeof(CancelEventArgs))
                    {
                        list.Add(new VisitorMethodInfo(parameters[0].ParameterType, DelegateFactory.CreateMethod(method), true));
                    }
                }

                return(list);
            });
        }
Example #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MethodAccessor"/> class.
 /// </summary>
 /// <param name="methodInfo">The method info.</param>
 public MethodAccessor(MethodInfo methodInfo)
 {
     _methodInfo      = methodInfo;
     _name            = methodInfo.Name;
     _lateBoundMethod = new Lazy <LateBoundMethod>(() => DelegateFactory.CreateMethod(_methodInfo));
 }
        public void PrivateMethods()
        {
            BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public;

            var  test = new TestClass();
            Type t    = typeof(TestClass);

            var m = t.GetMethod("PrivateOpen1", flags);

            Assert.IsNotNull(m);
            var d = DelegateFactory.CreateMethod(m);

            Assert.IsNotNull(d);
            var parameters = new object[] { "Test" };
            var r          = d.Invoke(test, parameters);

            Assert.IsNull(r);

            m = t.GetMethod("PrivateStaticOpen1", flags);
            Assert.IsNotNull(m);
            d = DelegateFactory.CreateMethod(m);
            Assert.IsNotNull(d);
            parameters = new object[] { "Test" };
            r          = d.Invoke(test, parameters);
            Assert.IsNull(r);


            m = t.GetMethod("PrivateOpen2", flags);
            Assert.IsNotNull(m);
            d = DelegateFactory.CreateMethod(m);
            Assert.IsNotNull(d);
            parameters = new object[] { "Test", 2 };
            r          = d.Invoke(test, parameters);
            Assert.IsNull(r);


            m = t.GetMethod("PrivateOpenReturn", flags);
            Assert.IsNotNull(m);
            d = DelegateFactory.CreateMethod(m);
            Assert.IsNotNull(d);
            parameters = new object[] { "Test", 2 };
            r          = d.Invoke(test, parameters);
            Assert.IsNotNull(r);

#if !SILVERLIGHT
            m = t.GetMethod("PrivateOpenOut", flags);
            Assert.IsNotNull(m);
            d = DelegateFactory.CreateMethod(m);
            Assert.IsNotNull(d);
            parameters = new object[] { "Test", null };
            r          = d.Invoke(test, parameters);
            Assert.IsNull(r);
            Assert.AreEqual("1234", parameters[1]);


            m = t.GetMethod("PrivateOpenOutReturn", flags);
            Assert.IsNotNull(m);
            d = DelegateFactory.CreateMethod(m);
            Assert.IsNotNull(d);
            parameters = new object[] { "test", null };
            r          = d.Invoke(test, parameters);
            Assert.IsNotNull(r);
            Assert.AreEqual("1234", parameters[1]);


            m = t.GetMethod("PrivateOpenRef", flags);
            Assert.IsNotNull(m);
            d = DelegateFactory.CreateMethod(m);
            Assert.IsNotNull(d);
            parameters = new object[] { "test", "" };
            r          = d.Invoke(test, parameters);
            Assert.IsNull(r);
            Assert.AreEqual("1234", parameters[1]);


            m = t.GetMethod("PrivateOpenRefReturn", flags);
            Assert.IsNotNull(m);
            d = DelegateFactory.CreateMethod(m);
            Assert.IsNotNull(d);
            parameters = new object[] { "test", "" };
            r          = d.Invoke(test, parameters);
            Assert.IsNotNull(r);
            Assert.AreEqual("1234", parameters[1]);
#endif

            m = t.GetMethod("PrivateOpenGeneric", flags);
            Assert.IsNotNull(m);
            m = m.MakeGenericMethod(typeof(string));
            d = DelegateFactory.CreateMethod(m);
            Assert.IsNotNull(d);
            parameters = new object[] { "test" };
            r          = d.Invoke(test, parameters);
            Assert.IsNull(r);

            m = t.GetMethod("PrivateOpenReturnGeneric", flags);
            Assert.IsNotNull(m);
            m = m.MakeGenericMethod(typeof(string));
            d = DelegateFactory.CreateMethod(m);
            Assert.IsNotNull(d);
            parameters = new object[] { "test", 1 };
            r          = d.Invoke(test, parameters);
            Assert.IsNotNull(r);
        }
        public void PublicMethods()
        {
            var  test = new TestClass();
            Type t    = typeof(TestClass);

            var m = t.GetMethod("BaseOpen");

            Assert.IsNotNull(m);
            var d = DelegateFactory.CreateMethod(m);

            Assert.IsNotNull(d);
            var parameters = new object[] { "Test" };
            var r          = d.Invoke(test, parameters);

            Assert.IsNull(r);

            m = t.GetMethod("PublicOpen1");
            Assert.IsNotNull(m);
            d = DelegateFactory.CreateMethod(m);
            Assert.IsNotNull(d);
            parameters = new object[] { "Test" };
            r          = d.Invoke(test, parameters);
            Assert.IsNull(r);

            m = t.GetMethod("PublicStaticOpen1");
            Assert.IsNotNull(m);
            d = DelegateFactory.CreateMethod(m);
            Assert.IsNotNull(d);
            parameters = new object[] { "Test" };
            r          = d.Invoke(test, parameters);
            Assert.IsNull(r);


            m = t.GetMethod("PublicOpen2");
            Assert.IsNotNull(m);
            d = DelegateFactory.CreateMethod(m);
            Assert.IsNotNull(d);
            parameters = new object[] { "Test", 2 };
            r          = d.Invoke(test, parameters);
            Assert.IsNull(r);


            m = t.GetMethod("PublicOpenReturn");
            Assert.IsNotNull(m);
            d = DelegateFactory.CreateMethod(m);
            Assert.IsNotNull(d);
            parameters = new object[] { "Test", 2 };
            r          = d.Invoke(test, parameters);
            Assert.IsNotNull(r);

#if !SILVERLIGHT
            m = t.GetMethod("PublicOpenOut");
            Assert.IsNotNull(m);
            d = DelegateFactory.CreateMethod(m);
            Assert.IsNotNull(d);
            parameters = new object[] { "Test", null };
            r          = d.Invoke(test, parameters);
            Assert.IsNull(r);
            Assert.AreEqual("1234", parameters[1]);

            m = t.GetMethod("PublicOpenOutReturn");
            Assert.IsNotNull(m);
            d = DelegateFactory.CreateMethod(m);
            Assert.IsNotNull(d);
            parameters = new object[] { "test", null };
            r          = d.Invoke(test, parameters);
            Assert.IsNotNull(r);
            Assert.AreEqual("1234", parameters[1]);


            m = t.GetMethod("PublicOpenRef");
            Assert.IsNotNull(m);
            d = DelegateFactory.CreateMethod(m);
            Assert.IsNotNull(d);
            parameters = new object[] { "test", "" };
            r          = d.Invoke(test, parameters);
            Assert.IsNull(r);
            Assert.AreEqual("1234", parameters[1]);


            m = t.GetMethod("PublicOpenRefReturn");
            Assert.IsNotNull(m);
            d = DelegateFactory.CreateMethod(m);
            Assert.IsNotNull(d);
            parameters = new object[] { "test", "" };
            r          = d.Invoke(test, parameters);
            Assert.IsNotNull(r);
            Assert.AreEqual("1234", parameters[1]);
#endif

            m = t.GetMethod("PublicOpenGeneric");
            Assert.IsNotNull(m);
            m = m.MakeGenericMethod(typeof(string));
            d = DelegateFactory.CreateMethod(m);
            Assert.IsNotNull(d);
            parameters = new object[] { "test" };
            r          = d.Invoke(test, parameters);
            Assert.IsNull(r);

            m = t.GetMethod("PublicOpenReturnGeneric");
            Assert.IsNotNull(m);
            m = m.MakeGenericMethod(typeof(string));
            d = DelegateFactory.CreateMethod(m);
            Assert.IsNotNull(d);
            parameters = new object[] { "test", 1 };
            r          = d.Invoke(test, parameters);
            Assert.IsNotNull(r);
        }
Example #5
0
        protected DynamicVisitor()
        {
            this.ShouldCallBaseTypeVisitors = false;
            this.ShouldCallMultipleVisitors = false;
            this._itemTypeVisitors          = DynamicVisitor <TBase> ._itemTypeVisitorsCache.GetOrAdd(base.GetType(), new ConcurrentDictionary <Type, List <DynamicVisitor <TBase> .VisitorMethodInfo> >());

            this._typeVisitorMethods = DynamicVisitor <TBase> ._typeVisitorMethodsCache.GetOrAdd(base.GetType(), delegate(Type t)
            {
                List <DynamicVisitor <TBase> .VisitorMethodInfo> list = new List <DynamicVisitor <TBase> .VisitorMethodInfo>();
                foreach (MethodInfo current in
                         from m in t.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic)
                         where m.Name == "Visit"
                         select m)
                {
                    ParameterInfo[] parameters = current.GetParameters();
                    if (parameters.Length == 1)
                    {
                        list.Add(new DynamicVisitor <TBase> .VisitorMethodInfo(parameters[0].ParameterType, DelegateFactory.CreateMethod(current), false));
                    }
                    if (parameters.Length == 2 && parameters[1].ParameterType == typeof(CancelEventArgs))
                    {
                        list.Add(new DynamicVisitor <TBase> .VisitorMethodInfo(parameters[0].ParameterType, DelegateFactory.CreateMethod(current), true));
                    }
                }
                return(list);
            });
        }