public void CreateComplexPOCOClassConstructorTest() { var propertyList = new List <RuntimePropertyInfo> { new RuntimePropertyInfo { PropertyName = "StringProperty", PropertyType = typeof(string) }, new RuntimePropertyInfo { PropertyName = "IntProperty", PropertyType = typeof(int) }, new RuntimePropertyInfo { PropertyName = "DoubleProperty", PropertyType = typeof(double) }, new RuntimePropertyInfo { PropertyName = "FloatProperty", PropertyType = typeof(float) } }; IPocoBuilder PocoBuilder = new ReflectionEmitBuilder(); Type myNewType = PocoBuilder.Build("POCO", propertyList); dynamic myObject = Activator.CreateInstance(myNewType, "String", 0, 2, 1.0f); Assert.AreEqual(myObject.StringProperty, "String"); Assert.AreEqual(myObject.IntProperty, 0); Assert.AreEqual(myObject.DoubleProperty, 2); Assert.AreEqual(myObject.FloatProperty, 1.0f); }
public static void DynamicMessageDecodingTestHelper() { #region Dynamic Type var propertyList = new List <RuntimePropertyInfo> { new RuntimePropertyInfo { PropertyName = "Name", PropertyType = typeof(string) } }; IPocoBuilder PocoBuilder = new ReflectionEmitBuilder(); Type myNewType = PocoBuilder.Build("POCO", propertyList); dynamic myObject = Activator.CreateInstance(myNewType); myObject.Name = "DynamicType"; #endregion NetworkServer server; server = new NetworkServer(); //server.Connect(new ServerConfig { Name = "local", Port = 14241 }); NetIncomingMessage inc; while (true) { if ((inc = server.ReadMessage()) != null) { switch (inc.MessageType) { case NetIncomingMessageType.ConnectionApproval: Console.WriteLine("Incoming LOGIN"); inc.SenderConnection.Approve(); break; case NetIncomingMessageType.Data: var outgoingmessage = server.CreateMessage(); MessageSerializer.Encode(myObject, ref outgoingmessage); server.SendToAll(outgoingmessage); Console.WriteLine("Sended a package with some string"); break; } } } }
public void RegisterTypesAndResolveTest() { #region Types Definitions var typeInfo1 = new List <RuntimePropertyInfo> { new RuntimePropertyInfo { PropertyName = "Type1", PropertyType = typeof(string) } }; var typeInfo2 = new List <RuntimePropertyInfo> { new RuntimePropertyInfo { PropertyName = "Type2", PropertyType = typeof(string) } }; var typeInfo3 = new List <RuntimePropertyInfo> { new RuntimePropertyInfo { PropertyName = "Type3", PropertyType = typeof(string) } }; #endregion IPocoBuilder PocoBuilder = new ReflectionEmitBuilder(); Type type1 = PocoBuilder.Build("Type1", typeInfo1); Type type2 = PocoBuilder.Build("Type2", typeInfo2); Type type3 = PocoBuilder.Build("Type3", typeInfo3); var typeRepository = new FlyweightRepository <Type, string>(); typeRepository.Add("Type1", type1); typeRepository.Add("Type2", type2); typeRepository.Add("Type3", type3); Assert.IsTrue(typeRepository.GetById("Type1").Equals(type1)); Assert.IsTrue(typeRepository.GetById("Type2").Equals(type2)); Assert.IsTrue(typeRepository.GetById("Type3").Equals(type3)); }
public void CreateComplexPOCOClassTest() { var propertyList = new List <RuntimePropertyInfo> { new RuntimePropertyInfo { PropertyName = "StringProperty", PropertyType = typeof(string) }, new RuntimePropertyInfo { PropertyName = "IntProperty", PropertyType = typeof(int) }, new RuntimePropertyInfo { PropertyName = "DoubleProperty", PropertyType = typeof(double) }, new RuntimePropertyInfo { PropertyName = "FloatProperty", PropertyType = typeof(float) } }; IPocoBuilder PocoBuilder = new ReflectionEmitBuilder(); Type myNewType = PocoBuilder.Build("POCO", propertyList); dynamic myObject = Activator.CreateInstance(myNewType); Assert.IsTrue(myObject.GetType().Name == "POCO"); var stringProperty = myObject.GetType().GetProperty("StringProperty"); var intProperty = myObject.GetType().GetProperty("IntProperty"); var doubleProperty = myObject.GetType().GetProperty("DoubleProperty"); var floatProperty = myObject.GetType().GetProperty("FloatProperty"); Assert.IsNull(stringProperty.GetValue(myObject)); myObject.StringProperty = "String"; myObject.IntProperty = 0; myObject.DoubleProperty = 2; myObject.FloatProperty = 1.0f; Assert.AreEqual(stringProperty.GetValue(myObject), "String"); Assert.AreEqual(intProperty.GetValue(myObject), 0); Assert.AreEqual(doubleProperty.GetValue(myObject), 2); Assert.AreEqual(floatProperty.GetValue(myObject), 1.0f); }
public void GetSetDynamicObjectValueTest() { var propertyList = new List<RuntimePropertyInfo> { new RuntimePropertyInfo{ PropertyName = "Name", PropertyType = typeof(string) } }; IPocoBuilder PocoBuilder = new ReflectionEmitBuilder(); Type myNewType = PocoBuilder.Build("POCO", propertyList); object myObject = Activator.CreateInstance(myNewType); myObject.SetValue("Name", "Value"); var value = myObject.GetValue("Name"); Assert.AreEqual("Value", value); }
public void SetDynamicObjectValueTest() { var propertyList = new List<RuntimePropertyInfo> { new RuntimePropertyInfo{ PropertyName = "StringParameter", PropertyType = typeof(string) }, new RuntimePropertyInfo{ PropertyName = "IntParameter", PropertyType = typeof(Byte) } }; IPocoBuilder PocoBuilder = new ReflectionEmitBuilder(); Type myNewType = PocoBuilder.Build("POCO", propertyList); object myObject = Activator.CreateInstance(myNewType, "string", (byte)1); var value = myObject.GetValue("StringParameter"); Assert.AreEqual(value, "string"); }
public void CreateNewPOCOClassConstructorTest() { var propertyList = new List <RuntimePropertyInfo> { new RuntimePropertyInfo { PropertyName = "Name", PropertyType = typeof(string) } }; IPocoBuilder PocoBuilder = new ReflectionEmitBuilder(); Type myNewType = PocoBuilder.Build("POCO", propertyList); dynamic myObject = Activator.CreateInstance(myNewType, "Test"); Assert.IsTrue(myObject.GetType().Name == "POCO"); var propertyInfo = myObject.GetType().GetProperty("Name"); //Assert.IsNull(propertyInfo.GetValue(myObject)); Assert.AreEqual("Test", myObject.Name); }