コード例 #1
0
        public void NotifyPropertyChangedTestObject()
        {
            CustomTypeCreator ct = new CustomTypeCreator("testassembly");
            var cPerson          = ct.CreateNewNotifyPropertyChangedType("Person");

            cPerson.AddProperty("FirstName", typeof(string));
            cPerson.AddProperty("LastName", typeof(string));
            cPerson.AddProperty("Uid", typeof(Guid));

            var types   = ct.Build();
            var tPerson = types.FirstOrDefault(w => w.Name == "Person");

            var iPerson    = tPerson.CreateInstanceWithPropertyGetAndSet() as BaseClasses.NotifyPropertyChangedBase;
            var hadChanges = false;

            iPerson.PropertyChanged += (s, a) => {
                hadChanges = true;
            };

            Guid guid = Guid.NewGuid();


            iPerson.SetPropertyValue("FirstName", "Lukas");
            iPerson.SetPropertyValue("LastName", "Dorn-Fussenegger");
            iPerson.SetPropertyValue("Uid", guid);


            Assert.IsTrue(iPerson.GetPropertyValue <string>("FirstName") == "Lukas");
            Assert.IsTrue(iPerson.GetPropertyValue <string>("LastName") == "Dorn-Fussenegger");
            Assert.IsTrue(iPerson.GetPropertyValue <Guid>("Uid") == guid);

            Assert.IsTrue(hadChanges);
        }
コード例 #2
0
        public Type[] Compile(string assemblyName)
        {
            CustomTypeCreator ct = new CustomTypeCreator(assemblyName);
            List <ToCompile>  l  = new List <ToCompile>();

            foreach (var t in Types)
            {
                switch (t.Type)
                {
                case DefinitionTypeEnums.SimpleType:
                    l.Add(new ToCompile()
                    {
                        CompileType = ct.CreateNewSimpleType(t.Name).SetCustomCustomTypeFlagsFlags(CustomTypeBaseFlags.AddIInitializeableImplementation, true), DefinitionType = t
                    });
                    break;

                case DefinitionTypeEnums.NotifyPropertyChanged:
                    l.Add(new ToCompile()
                    {
                        CompileType = ct.CreateNewNotifyPropertyChangedType(t.Name, false).SetCustomCustomTypeFlagsFlags(CustomTypeBaseFlags.AddIInitializeableImplementation, true), DefinitionType = t
                    });
                    break;

                case DefinitionTypeEnums.NotifyPropertyChangedWithChangeTracker:
                    l.Add(new ToCompile()
                    {
                        CompileType = ct.CreateNewNotifyPropertyChangedType(t.Name, true).SetCustomCustomTypeFlagsFlags(CustomTypeBaseFlags.AddIInitializeableImplementation, true), DefinitionType = t
                    });
                    break;

                default:
                    break;
                }
            }

            foreach (var t in l)
            {
                foreach (var p in t.DefinitionType.Properties)
                {
                    t.CompileType.AddProperty(p.Name, LocateType(p.Type, l));
                }
                t.CompileType.AddPropertyGetAndSet();
            }

            return(ct.Build());
        }
コード例 #3
0
        public void TypeOfLongTest()
        {
            CustomTypeCreator ct = new CustomTypeCreator("testassembly");
            var cTest            = ct.CreateNewNotifyPropertyChangedType("Test");

            cTest.AddProperty("TLong", typeof(long));
            cTest.AddIPropertyDescriptorImplementation();
            var instance = ct.Build().FirstOrDefault().CreateInstanceWithPropertyGetAndSet();

            instance.SetPropertyValue("TLong", (long)2020);

            Assert.IsTrue(instance.GetPropertyValue <long>("TLong") == 2020);
        }
コード例 #4
0
        public void GetPropertyNamesAndTypes()
        {
            CustomTypeCreator ct = new CustomTypeCreator("testassembly");
            var cPerson          = ct.CreateNewNotifyPropertyChangedType("Person", true);

            cPerson.AddProperty("FirstName", typeof(string));
            cPerson.AddProperty("LastName", typeof(string));
            cPerson.AddProperty("Uid", typeof(Guid));
            cPerson.AddIPropertyDescriptorImplementation();

            var types    = ct.Build();
            var tPerson  = types.FirstOrDefault(w => w.Name == "Person");
            var instance = tPerson.CreateInstance <Interfaces.IPropertyDescriptor>();


            Assert.IsTrue(instance.GetAllPropertyNames().Length == 3);
            Assert.IsTrue(instance.GetAllPropertyNames()[0] == "FirstName");

            Assert.IsTrue(instance.GetAllPropertyTypes().Length == 3);
            Assert.IsTrue(instance.GetAllPropertyTypes()[0] == typeof(string));
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: dornfussenegger/DynamicIL
        private static void Test1()
        {
            CustomTypeCreator ct = new CustomTypeCreator("testassembly", "test.dll");
            var cPerson          = ct.CreateNewNotifyPropertyChangedType("Person", true).SetCustomCustomTypeFlagsFlags(CustomTypeBaseFlags.AddIInitializeableImplementation, true);

            cPerson.AddProperty("FirstName", typeof(string));
            cPerson.AddProperty("LastName", typeof(string));
            cPerson.AddProperty("Uid", typeof(Guid));
            cPerson.AddProperty("WebClient", typeof(System.Net.WebClient)).SetCustomPropertyFlags(CustomPropertyFlags.InitializeInInitializeMethod, true);

            var cContract = ct.CreateNewNotifyPropertyChangedType("Contract", true).SetCustomCustomTypeFlagsFlags(CustomTypeBaseFlags.AddIInitializeableImplementation, true);

            cContract.AddProperty("ContractNumber", typeof(string));
            cContract.AddProperty("Date", typeof(DateTime));
            cPerson.AddProperty("Contract", cContract.TypeBuilder).SetCustomPropertyFlags(CustomPropertyFlags.InitializeInInitializeMethod, true);
            cPerson.AddPropertyOfGenericList("ConnectedPersons", cPerson.TypeBuilder).SetCustomPropertyFlags(CustomPropertyFlags.InitializeInInitializeMethod, true);

            cPerson.AddPropertyGetAndSet();
            cContract.AddPropertyGetAndSet();

            //cPerson.AddIInitializeableImplementation();
            //cContract.AddIInitializeableImplementation();
            cPerson.AddIPropertyDescriptorImplementation();
            cContract.AddIPropertyDescriptorImplementation();


            var types    = ct.Build();
            var tPerson  = types.FirstOrDefault(w => w.Name == "Person");
            var tVertrag = types.FirstOrDefault(w => w.Name == "Contract");
            var iPerson  = tPerson.CreateInstance();
            var iVertrag = tVertrag.CreateInstance <Interfaces.IPropertyDescriptor>();

            foreach (var item in iVertrag.GetAllPropertyNames())
            {
                Console.WriteLine(item);
            }
            foreach (var item in iVertrag.GetAllPropertyTypes())
            {
                Console.WriteLine(item.FullName);
            }

            //var propChangedOnPerson = (System.ComponentModel.INotifyPropertyChanged)iPerson;
            //propChangedOnPerson.PropertyChanged += (s, a) =>
            //{
            //    Console.WriteLine($"PropertyChanged: {a.PropertyName}");
            //};

            //var wiPerson = (Interfaces.IPropertyGetAndSet)iPerson;
            //var wiVertrag = (Interfaces.IPropertyGetAndSet)iVertrag;
            //wiPerson.SetPropertyValue("FirstName", "Lukas");
            //wiPerson.SetPropertyValue("LastName", "Dorn-Fussenegger");
            //wiPerson.SetPropertyValue("Uid", Guid.NewGuid());
            ////wiPerson.SetPropertyValue("Contract", wiVertrag);

            //wiVertrag.SetPropertyValue("ContractNumber", "1234-5678");
            //wiVertrag.SetPropertyValue("Date", DateTime.Now);
            ////wiPerson.SetPropertyValue("ConnectedPersons", cPerson.BuildType.CreateInstanceOfList());
            //for (int i = 0; i < 5; i++)
            //{

            //    var li = wiPerson.GetPropertyValue("ConnectedPersons") as System.Collections.IList;

            //    var ci = tPerson.CreateInstanceWithPropertyGetAndSet();
            //    ci.SetPropertyValue("FirstName", $"Person{i}");
            //    ci.SetPropertyValue("Uid", Guid.NewGuid());
            //    li.Add(ci);

            //}


            //Console.WriteLine(
            //    Newtonsoft.Json.JsonConvert.SerializeObject(wiPerson, Newtonsoft.Json.Formatting.Indented)
            //    );
        }