public void Test_EmitManager_Field()
        {
            var manager = new EmitManager();

            var te = manager.CreateInstance <TestEmit>();

            Assert.IsNull(te.Name);

            manager.SetField(te, "Age", 1);
            Assert.AreEqual(1, manager.GetField(te, "Age"));
            Assert.AreEqual(1, manager.GetField(te, typeof(TestEmit).GetField("Age")));

            Exception exception = null;

            try
            {
                DynamicEngine.GetField(te, "Age2");
            }
            catch (MissingMemberException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            exception = null;
            try
            {
                DynamicEngine.SetField(te, "Age3", 8);
            }
            catch (MissingMemberException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);
        }
示例#2
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="MainForm" /> class.
        /// </summary>
        public MainForm()
        {
            this.InitializeComponent();

            this.personListBox.DisplayMember = "Name";

            this.dataGrid.Columns[0].ValueType = typeof(string);
            this.dataGrid.Columns[1].ValueType = typeof(int);
            this.dataGrid.Columns[2].ValueType = typeof(string);

            DynamicEngine.SetBindingManager(new EmitManager());

            this.SetGlobalBinding();
        }
        public void Test_DynamicEngine_CreateInstance()
        {
            DynamicEngine.ClearCache();
            var te = DynamicEngine.CreateInstance <TestEmit>();

            Assert.IsNotNull(te);

            te = DynamicEngine.CreateInstance <TestEmit>(2);
            Assert.IsNotNull(te);
            Assert.AreEqual(te.Age, 2);

            DynamicEngine.SetBindingManager(new ReflectManager());

            te = DynamicEngine.CreateInstance(typeof(TestEmit), 2) as TestEmit;
            Assert.IsNotNull(te);
            Assert.AreEqual(te.Age, 2);
        }
        public void Test_EmitManager_Property()
        {
            var manager = new EmitManager();

            var te = manager.CreateInstance <TestEmit>();

            Assert.IsNull(te.Name);

            const string name = "yohan";

            manager.SetProperty(te, "Name", name);
            Assert.AreEqual(name, manager.GetProperty(te, "Name"));

            manager.SetIndexProperty(te, 0, 2);
            Assert.AreEqual(2, manager.GetIndexProperty(te, 0));

            Exception exception = null;

            try
            {
                DynamicEngine.GetProperty(te, "Name9");
            }
            catch (MissingMemberException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            exception = null;
            try
            {
                DynamicEngine.SetProperty(te, "Name9", 4);
            }
            catch (MissingMemberException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            manager.SetIndexProperty(te, 0, 2);
            Assert.AreEqual(2, manager.GetIndexProperty(te, 0));
        }
        public void Test_EmitManager_Method()
        {
            var manager = new EmitManager();

            var te = manager.CreateInstance <TestEmit>();

            Assert.IsNull(te.Name);

            manager.InvokeMethod(te, "SetAge", new object[] { 9 });
            Assert.AreEqual(9, te.Age);

            var age1 = (int)manager.InvokeMethod(te, "GetAge", null);

            Assert.AreEqual(9, age1);

            string propertyName = null;

            te.PropertyChanged += (sender, args) => { propertyName = args.PropertyName; };

            var methodInfo = manager.GetMethodInfo(te.GetType(), "OnPropertyChanged", new object[] { "parameter" });

            Assert.IsNotNull(methodInfo);

            manager.RaiseEvent(te, "PropertyChanged", new PropertyChangedEventArgs("Age"));
            Assert.AreEqual(propertyName, "Age");

            Exception exception = null;

            try
            {
                manager.InvokeMethod(te, "GetAgeEx", null);
            }
            catch (MissingMemberException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            exception = null;
            try
            {
                manager.RaiseEvent(te, "NoEvent", null);
            }
            catch (MissingMemberException e)
            {
                exception = e;
            }

            Assert.IsNotNull(exception);

            manager.RegisterExtensionType(typeof(TestEmit), typeof(TestEmitExtension));

            methodInfo = manager.GetMethodInfo(te.GetType(), "GetAgeEx", null);
            Assert.IsNotNull(methodInfo);

            var age = (int)manager.InvokeMethod(te, "GetAgeEx", null);

            Assert.AreEqual(9, age);

            manager.UnRegisterExtensionType(typeof(TestEmit), typeof(TestEmitExtension));

            exception = null;
            try
            {
                manager.InvokeMethod(te, "GetAgeEx", null);
            }
            catch (MissingMemberException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            te.Name2  = "2";
            exception = null;
            try
            {
                DynamicEngine.RaiseEvent(te, "name2", new PropertyChangedEventArgs("Age"));
            }
            catch (MissingMemberException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            manager.RegisterExtensionType(typeof(TestEmit), typeof(TestEmitExtension2));
            age = (int)manager.InvokeMethod(te, "GetAgeEx2", null);
            Assert.AreEqual(9, age);

            TestEmit.AgeEx = 0;
            object age2 = manager.InvokeMethod(typeof(TestEmit), "AddAgeEx", null);

            Assert.AreEqual(1, age2);

            age2 = manager.InvokeMethod(te, manager.GetMethodInfo(typeof(TestEmit), "AddAgeEx", null), null);
            Assert.AreEqual(2, age2);

            age2 = manager.InvokeMethod(typeof(TestEmit), "SetAgeEx", new object[] { 5 });
            Assert.AreEqual(5, age2);

            age2 = manager.InvokeMethod(te, "SetAgeEx", new object[] { 7 });
            Assert.AreEqual(7, age2);

            const string name       = "yohan";
            var          parameters = new object[] { name, 1 };

            manager.InvokeMethod(te, "ChangeProperty", parameters);
            Assert.AreEqual("yohan1", parameters[0]);

            parameters = new object[] { name };
            manager.InvokeMethod(te, "ChangeProperty", parameters);
            Assert.AreEqual("yohan", parameters[0]);
        }
示例#6
0
        private static void CompareReflectAndEmitBinding()
        {
            const int count = 2000;

            Console.ForegroundColor = ConsoleColor.Green;
            System.Console.WriteLine("\n=======  Compare Reflect and Emit Binding performance =======");


            var viewModels  = new TestViewModel[count];
            var viewModel2s = new TestViewModel2[count];

            DynamicEngine.SetBindingManager(new ReflectManager());

            for (int i = 0; i < count; i++)
            {
                var viewModel2 = new TestViewModel2();
                var viewModel  = new TestViewModel();
                viewModels[i]  = viewModel;
                viewModel2s[i] = viewModel2;

                BindingEngine.SetPropertyBinding(viewModel2, testView => testView.Age, viewModel, model => model.Age);
            }

            var sw = new Stopwatch();

            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                viewModels[i].Age = i;
            }

            sw.Stop();

            Console.ForegroundColor = ConsoleColor.White;
            System.Console.WriteLine("Reflect manager time elapsed {0}", sw.ElapsedMilliseconds);

            DynamicEngine.SetBindingManager(new EmitManager());

            for (int i = 0; i < count; i++)
            {
                var viewModel2 = new TestViewModel2();
                var viewModel  = new TestViewModel();
                viewModels[i]  = viewModel;
                viewModel2s[i] = viewModel2;

                BindingEngine.SetPropertyBinding(viewModel2, testView => testView.Age, viewModel, model => model.Age);
            }

            sw = new Stopwatch();
            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                viewModels[i].Age = i;
            }

            sw.Stop();

            Console.ForegroundColor = ConsoleColor.Yellow;
            System.Console.WriteLine("Emit manager time elapsed {0}", sw.ElapsedMilliseconds);
        }
        public void Test_DynamicEngine_Method()
        {
            var te = DynamicEngine.CreateInstance <TestEmit>();

            Assert.IsNull(te.Name);

            DynamicEngine.InvokeMethod(te, "SetAge", new object[] { 9 });
            Assert.AreEqual(9, te.Age);

            var addVlue = (int)DynamicEngine.InvokeMethod(te, "Add", new object[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 });

            Assert.AreEqual(10, addVlue);

            string propertyName = null;

            te.PropertyChanged += (sender, args) => { propertyName = args.PropertyName; };

            DynamicEngine.RaiseEvent(te, "PropertyChanged", new PropertyChangedEventArgs("Age"));
            Assert.AreEqual(propertyName, "Age");

            Exception exception = null;

            try
            {
                DynamicEngine.InvokeMethod(te, "GetAgeEx", null);
            }
            catch (MissingMemberException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            exception = null;
            try
            {
                DynamicEngine.RaiseEvent(te, "PropertyChanged22", new PropertyChangedEventArgs("Age"));
            }
            catch (MissingMemberException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            DynamicEngine.RegisterExtensionType(typeof(TestEmit), typeof(TestEmitExtension));
            DynamicEngine.RegisterExtensionType(typeof(TestEmit), typeof(TestEmitExtension));
            DynamicEngine.RegisterExtensionType(typeof(TestEmit), typeof(TestEmitExtension2));

            MethodInfo methodInfo = DynamicEngine.GetMethodInfo(te.GetType(), "GetAgeEx", null);

            Assert.IsNotNull(methodInfo);

            var age = (int)DynamicEngine.InvokeMethod(te, "GetAgeEx", null);

            Assert.AreEqual(9, age);

            DynamicEngine.UnRegisterExtensionType(typeof(TestEmit), typeof(TestEmitExtension));
            DynamicEngine.UnRegisterExtensionType(typeof(TestEmit), typeof(TestEmitExtension2));

            exception = null;
            try
            {
                DynamicEngine.InvokeMethod(te, "GetAgeEx", null);
            }
            catch (MissingMemberException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            DynamicEngine.RegisterExtensionType(typeof(TestEmit), typeof(TestEmitExtension2));
            age = (int)DynamicEngine.InvokeMethod(te, "GetAgeEx2", null);
            Assert.AreEqual(9, age);

            TestEmit.AgeEx = 0;
            object age1 = DynamicEngine.InvokeMethod(typeof(TestEmit), "AddAgeEx", null);

            Assert.AreEqual(1, age1);

            age1 = DynamicEngine.InvokeMethod(typeof(TestEmit), "SetAgeEx", new object[] { 5 });
            Assert.AreEqual(5, age1);

            const string name       = "yohan";
            var          parameters = new object[] { name, 1 };

            DynamicEngine.InvokeMethod(te, "ChangeProperty", parameters);
            Assert.AreEqual("yohan1", parameters[0]);

            parameters = new object[] { name };
            DynamicEngine.InvokeMethod(te, "ChangeProperty", parameters);
            Assert.AreEqual("yohan", parameters[0]);
        }
示例#8
0
 private void lvAssemblyLine_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
     DynamicEngine.ShowDynamicUI(spDynamicUI, lvAssemblyLine.SelectedItem, StateChangeHandler);
 }