Beispiel #1
0
        public static CallerBase <T> Caller <T>(this T value)
        {
            var caller = (CallerBase <T>) DynamicCaller <T> .Create();

            caller.SetInstance(value);
            return(caller);
        }
Beispiel #2
0
        public void TestCall3()
        {
            //创建动态类实例代理
            var instance = DynamicCaller <TestB> .Create();

            instance.New();
            Assert.Equal("111", instance["Name"].Get <string>());

            //调用动态委托赋值
            instance.Set("Name", "222");

            Assert.Equal("222", instance["Name"].Get <string>());


            var c = instance["InstanceC"].Get <TestC>();

            Assert.Equal("abc", c.Name);


            instance["InstanceC"].Set(new TestC()
            {
                Name = "bbca"
            });
            Assert.Equal("bbca", instance["InstanceC"].Get <TestC>().Name);
        }
Beispiel #3
0
        public void TestCall1()
        {
            //ScriptComplier.Init();
            string text = @"using System;
using System.Collections;
using System.Linq;
using System.Text;
 
namespace HelloWorld
{
    public class Test
    {
        public Test(){
            Name=""111"";
        }

        public string Name;
        public int Age{get;set;}
    }
}";
            //根据脚本创建动态类
            Type type = RuntimeComplier.GetType(text);
            //创建动态类实例代理
            var instance = DynamicCaller.Create(type);

            instance.New();
            //Get动态调用
            Assert.Equal("111", instance["Name"].Get <string>());
            //调用动态委托赋值
            instance.Set("Name", "222");

            Assert.Equal("222", instance["Name"].Get <string>());
        }
Beispiel #4
0
        /// <summary>
        /// Get parameters for string formatting.
        /// </summary>
        /// <param name="parameters"><see cref="object"/> contains parameters.</param>
        /// <returns>Parameters for string formatting.</returns>
        /// <remarks>This method is used in <see cref="Format(IFormatProvider, object)"/>, <see cref="ToFormattableString(object)"/>.</remarks>
        protected virtual object[] GetParameters(object parameters)
        {
            var args = new object[this.arguments.Length];

            if (parameters is IList arr && createArguments(arr, args))
            {
                return(args);
            }

            if (parameters is IDictionary dic && createArguments(dic, args) == args.Length)
            {
                return(args);
            }

            var type   = parameters.GetType();
            var caller = default(DynamicCaller);

            foreach (var item in this.arguments)
            {
                if (args[item.Index] != null)
                {
                    continue;
                }
                if (caller is null)
                {
                    if (!callers.TryGetValue(type, out caller))
                    {
                        caller        = new DynamicCaller(type);
                        callers[type] = caller;
                    }
                }
                args[item.Index] = caller.Get(parameters, item.Name);
            }
            return(args);
        }
        protected override object ConvertMethod(MethodInfo method, object target)
        {
            if (method == null)
            {
                throw new ArgumentNullException(nameof(method));
            }
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            if (!_converterFunctions.Value.TryGetValue(method, out ConverterFunc func))
            {
                func = GetConverterFunc();
                _converterFunctions.Value.Add(method, func);
            }
            return(func);

            ConverterFunc GetConverterFunc()
            {
                ParameterInfo[] parameters = method.GetParameters();
                Type            targetType = target.GetType();
                CultureInfo     culture    = EffectiveCulture;

                if (method.IsStatic)
                {
                    switch (parameters.Length)
                    {
                    case 3:
                        return((v, p, c) => DynamicCaller.CallStatic <Func <Type, object, object, CultureInfo, object> >(method.Name)(targetType, v, p, c ?? culture));

                    case 2:
                        return((v, p, c) => DynamicCaller.CallStatic <Func <Type, object, object, object> >(method.Name)(targetType, v, p));

                    case 1:
                        return((v, p, c) => DynamicCaller.CallStatic <Func <Type, object, object> >(method.Name)(targetType, v));
                    }
                }
                else
                {
                    switch (parameters.Length)
                    {
                    case 3:
                        return((v, p, c) => DynamicCaller.Call <Func <Object, object, object, CultureInfo, object> >(method.Name)(target, v, p, c ?? culture));

                    case 2:
                        return((v, p, c) => DynamicCaller.Call <Func <Object, object, object, object> >(method.Name)(target, v, p));

                    case 1:
                        return((v, p, c) => DynamicCaller.Call <Func <Object, object, object> >(method.Name)(target, v));
                    }
                }
                return(null);
            }
        }
Beispiel #6
0
        public void CallInstancePropertyGetter()
        {
            var foo = new Foo {
                Property = 1
            };

            DynamicCaller.Get <object, object>(nameof(Foo.Property)).Invoke(foo).Should().Be(1);
            DynamicCaller.Get <object, int>(nameof(Foo.Property)).Invoke(foo).Should().Be(1);
            DynamicCaller.Get <Foo, object>(nameof(Foo.Property)).Invoke(foo).Should().Be(1);
            DynamicCaller.Get <Foo, int>(nameof(Foo.Property)).Invoke(foo).Should().Be(1);
        }
Beispiel #7
0
        public void TestCall2()
        {
            //创建动态类实例代理
            var instance = DynamicCaller <TestB> .Create();

            instance.New();
            Assert.Equal("111", instance["Name"].Get <string>());

            //调用动态委托赋值
            instance.Set("Name", "222");

            Assert.Equal("222", instance["Name"].Get <string>());
        }
        private static object Get([NotNull] object value, [NotNull] string memberName)
        {
            if (!_getterFunctions.Value.TryGetValue(memberName, out GetterDelegate func))
            {
                _getterFunctions.Value.Add(memberName, func = DynamicCaller.Get <object, object>(memberName));
            }

            try {
                return(func(value));
            }
            catch (RuntimeBinderException ex) {
                throw new InvalidOperationException($"Cannot resolve property or field '{memberName}'.", ex);
            }
        }
Beispiel #9
0
        public void CallInstancePropertySetter()
        {
            var foo = new Foo {
                Property = 1
            };

            DynamicCaller.Set <object, object>(nameof(Foo.Property)).Invoke(foo, 2);
            foo.Property.Should().Be(2);
            DynamicCaller.Set <object, int>(nameof(Foo.Property)).Invoke(foo, 3);
            foo.Property.Should().Be(3);
            DynamicCaller.Set <Foo, object>(nameof(Foo.Property)).Invoke(foo, 4);
            foo.Property.Should().Be(4);
            DynamicCaller.Set <Foo, int>(nameof(Foo.Property)).Invoke(foo, 5);
            foo.Property.Should().Be(5);
        }
Beispiel #10
0
        public void CallInstanceMethodWithOverloads()
        {
            const int DoubleResult = 4, IntResult = 3;

            DynamicCaller.Call <Func <Foo, object, object> >(nameof(Foo.InstanceMethod2)).Invoke(new Foo(), 5.0f).Should().Be(DoubleResult);
            DynamicCaller.Call <Func <Foo, object, object> >(nameof(Foo.InstanceMethod2)).Invoke(new Foo(), 5.0).Should().Be(DoubleResult);
            DynamicCaller.Call <Func <Foo, object, object> >(nameof(Foo.InstanceMethod2)).Invoke(new Foo(), 5).Should().Be(IntResult);
            DynamicCaller.Call <Func <Foo, object, int> >(nameof(Foo.InstanceMethod2)).Invoke(new Foo(), 5.0f).Should().Be(DoubleResult);
            DynamicCaller.Call <Func <Foo, object, int> >(nameof(Foo.InstanceMethod2)).Invoke(new Foo(), 5.0).Should().Be(DoubleResult);
            DynamicCaller.Call <Func <Foo, object, int> >(nameof(Foo.InstanceMethod2)).Invoke(new Foo(), 5).Should().Be(IntResult);
            DynamicCaller.Call <Func <Foo, double, object> >(nameof(Foo.InstanceMethod2)).Invoke(new Foo(), 5.0f).Should().Be(DoubleResult);
            DynamicCaller.Call <Func <Foo, double, object> >(nameof(Foo.InstanceMethod2)).Invoke(new Foo(), 5.0).Should().Be(DoubleResult);
            DynamicCaller.Call <Func <Foo, double, object> >(nameof(Foo.InstanceMethod2)).Invoke(new Foo(), 5).Should().Be(DoubleResult);
            DynamicCaller.Call <Func <Foo, double, int> >(nameof(Foo.InstanceMethod2)).Invoke(new Foo(), 5.0f).Should().Be(DoubleResult);
            DynamicCaller.Call <Func <Foo, double, int> >(nameof(Foo.InstanceMethod2)).Invoke(new Foo(), 5.0).Should().Be(DoubleResult);
            DynamicCaller.Call <Func <Foo, int, object> >(nameof(Foo.InstanceMethod2)).Invoke(new Foo(), 5).Should().Be(IntResult);
            DynamicCaller.Call <Func <Foo, int, int> >(nameof(Foo.InstanceMethod2)).Invoke(new Foo(), 5).Should().Be(IntResult);
        }
Beispiel #11
0
        public void CallStaticMethodWithOverloads()
        {
            const int DoubleResult = 4, IntResult = 3;

            DynamicCaller.CallStatic <Func <Type, object, object> >(nameof(Foo.StaticMethod2)).Invoke(typeof(Foo), 5.0f).Should().Be(DoubleResult);
            DynamicCaller.CallStatic <Func <Type, object, object> >(nameof(Foo.StaticMethod2)).Invoke(typeof(Foo), 5.0).Should().Be(DoubleResult);
            DynamicCaller.CallStatic <Func <Type, object, object> >(nameof(Foo.StaticMethod2)).Invoke(typeof(Foo), 5).Should().Be(IntResult);
            DynamicCaller.CallStatic <Func <Type, object, int> >(nameof(Foo.StaticMethod2)).Invoke(typeof(Foo), 5.0f).Should().Be(DoubleResult);
            DynamicCaller.CallStatic <Func <Type, object, int> >(nameof(Foo.StaticMethod2)).Invoke(typeof(Foo), 5.0).Should().Be(DoubleResult);
            DynamicCaller.CallStatic <Func <Type, object, int> >(nameof(Foo.StaticMethod2)).Invoke(typeof(Foo), 5).Should().Be(IntResult);
            DynamicCaller.CallStatic <Func <Type, double, object> >(nameof(Foo.StaticMethod2)).Invoke(typeof(Foo), 5.0f).Should().Be(DoubleResult);
            DynamicCaller.CallStatic <Func <Type, double, object> >(nameof(Foo.StaticMethod2)).Invoke(typeof(Foo), 5.0).Should().Be(DoubleResult);
            DynamicCaller.CallStatic <Func <Type, double, object> >(nameof(Foo.StaticMethod2)).Invoke(typeof(Foo), 5).Should().Be(DoubleResult);
            DynamicCaller.CallStatic <Func <Type, double, int> >(nameof(Foo.StaticMethod2)).Invoke(typeof(Foo), 5.0f).Should().Be(DoubleResult);
            DynamicCaller.CallStatic <Func <Type, double, int> >(nameof(Foo.StaticMethod2)).Invoke(typeof(Foo), 5.0).Should().Be(DoubleResult);
            DynamicCaller.CallStatic <Func <Type, int, object> >(nameof(Foo.StaticMethod2)).Invoke(typeof(Foo), 5).Should().Be(IntResult);
            DynamicCaller.CallStatic <Func <Type, int, int> >(nameof(Foo.StaticMethod2)).Invoke(typeof(Foo), 5).Should().Be(IntResult);
        }
Beispiel #12
0
        static void Main(string[] args)
        {
            TempTime = DateTime.Now;
            Stopwatch stopwatch = new Stopwatch();

            for (int j = 0; j < 20; j++)
            {
                Console.WriteLine("=========================================");


                stopwatch.Restart();
                for (int i = 0; i < 40000; i++)
                {
                    var tEntity = new TestB();
                    if (tEntity.A2ge712 == "111")
                    {
                        //调用动态委托赋值
                        tEntity.A2ge712 = "222";
                    }
                }
                stopwatch.Stop();
                Console.WriteLine("原生调用:\t\t" + stopwatch.Elapsed);



                var entity = DynamicCaller.Create(typeof(TestB));
                stopwatch.Restart();
                for (int i = 0; i < 40000; i++)
                {
                    entity.New();
                    if (entity.Get <string>("A2ge712") == "111")
                    {
                        //调用动态委托赋值
                        entity.Set("A2ge712", "222");
                    }
                }
                stopwatch.Stop();
                Console.WriteLine("NCaller SimpleCaller:\t" + stopwatch.Elapsed);


                stopwatch.Restart();
                for (int i = 0; i < 40000; i++)
                {
                    RunDynamic(new TestB());
                }
                stopwatch.Stop();
                Console.WriteLine("Dynamic :\t\t" + stopwatch.Elapsed);


                stopwatch.Restart();
                for (int i = 0; i < 40000; i++)
                {
                    var tEntity = (new TestB()).Caller();
                    if (tEntity.Get <string>("A2ge712") == "111")
                    {
                        //调用动态委托赋值
                        tEntity.Set("A2ge712", "222");
                    }
                }
                stopwatch.Stop();
                Console.WriteLine("NCaller Extension:\t" + stopwatch.Elapsed);


                //entity = DynamicCaller.Create(typeof(TestB));
                //stopwatch.Restart();
                //for (int i = 0; i < 40000; i++)
                //{
                //    entity.New();
                //    if (entity.Get<DateTime>("Time") != TempTime)
                //    {
                //        //调用动态委托赋值
                //        entity.Set("Time", TempTime);
                //    }
                //}
                //stopwatch.Stop();
                //Console.WriteLine("NCaller SimpleCaller:\t" + stopwatch.Elapsed);

                //stopwatch.Restart();
                //for (int i = 0; i < 40000; i++)
                //{
                //    RunDynamicTime(new TestB());
                //}
                //stopwatch.Stop();
                //Console.WriteLine("Dynamic :\t\t" + stopwatch.Elapsed);

                //entity = DynamicCaller.Create(typeof(TestB));
                //stopwatch.Restart();
                //for (int i = 0; i < 40000; i++)
                //{
                //    entity.New();
                //    if (entity.Get<DateTime>("Time") != TempTime)
                //    {
                //        //调用动态委托赋值
                //        entity.Set("Time", TempTime);
                //    }
                //}
                //stopwatch.Stop();
                //Console.WriteLine("NCaller SimpleCaller:\t" + stopwatch.Elapsed);
                Console.WriteLine("=========================================");
            }


            Console.ReadKey();
        }
Beispiel #13
0
        static void Main(string[] args)
        {
            Stopwatch stopwatch = new Stopwatch();

            for (int j = 0; j < 20; j++)
            {
                Console.WriteLine("=========================================");


                stopwatch.Restart();
                for (int i = 0; i < 50000; i++)
                {
                    var tEntity = new TestB();
                    if (tEntity.Name == "111")
                    {
                        //调用动态委托赋值
                        tEntity.Name = "222";
                    }
                }
                stopwatch.Stop();
                Console.WriteLine("原生调用:\t\t" + stopwatch.Elapsed);



                var entity = DynamicCaller.Create(typeof(TestB));
                stopwatch.Restart();
                for (int i = 0; i < 50000; i++)
                {
                    entity.New();
                    if (entity.Get <string>("Name") == "111")
                    {
                        //调用动态委托赋值
                        entity.Set("Name", "222");
                    }
                }
                stopwatch.Stop();
                Console.WriteLine("NCaller SimpleCaller:\t" + stopwatch.Elapsed);


                stopwatch.Restart();
                for (int i = 0; i < 50000; i++)
                {
                    RunDynamic(new TestB());
                }
                stopwatch.Stop();
                Console.WriteLine("Dynamic :\t\t" + stopwatch.Elapsed);


                stopwatch.Restart();
                for (int i = 0; i < 50000; i++)
                {
                    var tEntity = (new TestB()).Caller();
                    if (tEntity.Get <string>("Name") == "111")
                    {
                        //调用动态委托赋值
                        tEntity.Set("Name", "222");
                    }
                }
                stopwatch.Stop();
                Console.WriteLine("NCaller Extension:\t" + stopwatch.Elapsed);
                Console.WriteLine("=========================================");
            }
            Console.ReadKey();
        }
Beispiel #14
0
 public void CallInstanceMethodSimple()
 {
     DynamicCaller.Call <Func <Foo, object> >(nameof(Foo.InstanceMethod0)).Invoke(new Foo()).Should().Be(1);
     DynamicCaller.Call <Func <Foo, int> >(nameof(Foo.InstanceMethod0)).Invoke(new Foo()).Should().Be(1);
 }
Beispiel #15
0
 public void CallInstanceMethodWithArguments()
 {
     DynamicCaller.Call <Func <Foo, object, object, object> >(nameof(Foo.InstanceMethod1)).Invoke(new Foo(), 1, 2).Should().Be(3);
     DynamicCaller.Call <Func <Foo, object, object, int> >(nameof(Foo.InstanceMethod1)).Invoke(new Foo(), 1, 2).Should().Be(3);
     DynamicCaller.Call <Func <Foo, int, int, int> >(nameof(Foo.InstanceMethod1)).Invoke(new Foo(), 1, 2).Should().Be(3);
 }
Beispiel #16
0
 public void CallStaticMethodSimple()
 {
     DynamicCaller.CallStatic <Func <Type, object> >(nameof(Foo.StaticMethod0)).Invoke(typeof(Foo)).Should().Be(1);
     DynamicCaller.CallStatic <Func <Type, int> >(nameof(Foo.StaticMethod0)).Invoke(typeof(Foo)).Should().Be(1);
 }
Beispiel #17
0
 public void CallStaticMethodWithArguments()
 {
     DynamicCaller.CallStatic <Func <Type, object, object, object> >(nameof(Foo.StaticMethod1)).Invoke(typeof(Foo), 1, 2).Should().Be(3);
     DynamicCaller.CallStatic <Func <Type, object, object, int> >(nameof(Foo.StaticMethod1)).Invoke(typeof(Foo), 1, 2).Should().Be(3);
     DynamicCaller.CallStatic <Func <Type, int, int, int> >(nameof(Foo.StaticMethod1)).Invoke(typeof(Foo), 1, 2).Should().Be(3);
 }