public void SimpleObject() { CustomTypeCreator ct = new CustomTypeCreator("testassembly"); var cPerson = ct.CreateNewSimpleType("Person"); cPerson.AddProperty("FirstName", typeof(string)); cPerson.AddProperty("LastName", typeof(string)); cPerson.AddProperty("Uid", typeof(Guid)); cPerson.AddPropertyGetAndSet(); var types = ct.Build(); var tPerson = types.FirstOrDefault(w => w.Name == "Person"); var iPerson = tPerson.CreateInstanceWithPropertyGetAndSet(); 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); }
private static void Test3() { CustomTypeCreator ct = new CustomTypeCreator("testassembly2", "test2.dll"); var cPerson = ct.CreateNewSimpleType("Person"); cPerson.AddProperty("FirstName", typeof(string)); cPerson.AddProperty("LastName", typeof(string)); cPerson.AddProperty("Uid", typeof(Guid)); cPerson.AddProperty("WebClient", typeof(System.Net.WebClient)); var cContract = ct.CreateNewSimpleType("Contract"); cContract.AddProperty("ContractNumber", typeof(string)); cContract.AddProperty("Date", typeof(DateTime)); cPerson.AddProperty("Contract", cContract.TypeBuilder); cPerson.AddPropertyGetAndSet(); cContract.AddPropertyGetAndSet(); //cPerson.AddIInitializeableImplementation(); //cContract.AddIInitializeableImplementation(); 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(); }
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); }
public static void GenerateAndTestTypeCreation() { CustomTypeCreator ct = new CustomTypeCreator("testassembly"); var cPerson = ct.CreateNewSimpleType("Person"); cPerson.AddProperty("FirstName", typeof(string)); cPerson.AddProperty("LastName", typeof(string)); cPerson.AddProperty("Uid", typeof(Guid)); cPerson.AddPropertyGetAndSet(); var types = ct.Build(); var tPerson = types.FirstOrDefault(w => w.Name == "Person"); var iPerson = tPerson.CreateInstanceWithPropertyGetAndSet(); Guid guid = Guid.NewGuid(); iPerson.SetPropertyValue("FirstName", "Lukas"); iPerson.SetPropertyValue("LastName", "Dorn-Fussenegger"); iPerson.SetPropertyValue("Uid", guid); if (iPerson.GetPropertyValue <string>("FirstName") != "Lukas") { throw new Exception("Check failed"); } }
public void CollectionObject() { CustomTypeCreator ct = new CustomTypeCreator("testassembly"); var cPerson = ct.CreateNewSimpleType("Person"); cPerson.AddProperty("FirstName", typeof(string)); cPerson.AddProperty("LastName", typeof(string)); cPerson.AddProperty("Uid", typeof(Guid)); var cContract = ct.CreateNewSimpleType("Contract"); cContract.AddProperty("ContractNumber", typeof(string)); cContract.AddProperty("Date", typeof(DateTime)); cPerson.AddProperty("Contract", cContract.TypeBuilder); cPerson.AddPropertyOfGenericList("ConnectedPersons", cPerson.TypeBuilder); cPerson.AddPropertyGetAndSet(); cContract.AddPropertyGetAndSet(); var types = ct.Build(); var tPerson = types.FirstOrDefault(w => w.Name == "Person"); var tVertrag = types.FirstOrDefault(w => w.Name == "Contract"); var iPerson = Activator.CreateInstance(tPerson, Array.Empty <object>()); var iVertrag = Activator.CreateInstance(tVertrag, Array.Empty <object>()); 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); } var l = wiPerson.GetPropertyValue("ConnectedPersons") as System.Collections.IList; Assert.IsTrue(l.Count == 5); var last = l[4] as Interfaces.IPropertyGetAndSet; Assert.IsTrue(last.GetPropertyValue <string>("FirstName") == "Person4"); }
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); }
public static void RegisterCustomTypeCreator(Type type, CustomTypeCreator customTypeCreator) { if (type == null) { throw new ArgumentNullException(nameof(type)); } if (customTypeCreator == null) { customTypeCreators.Remove(type); } else { customTypeCreators[type] = customTypeCreator; } }
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()); }
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)); }
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) // ); }