public void TestPropertyTypePublicField() { PropertyType pt; pt = new PropertyType(typeof(b), "publicField"); Assert.AreEqual("publicField", pt.Name); Assert.AreEqual(typeof(ia), pt.Type); }
public void TestPropertyTypePrivateField() { PropertyType pt; //private field pt = new PropertyType(typeof(a), "privateField"); Assert.AreEqual("privateField", pt.Name); Assert.AreEqual(typeof(string), pt.Type); }
public void TestPropertyTypePrivateProperty() { PropertyType pt; //private property pt = new PropertyType(typeof(b), "PrivateProperty"); Assert.AreEqual("PrivateProperty", pt.Name); Assert.AreEqual(typeof(ia), pt.Type); }
public void TestPropertyTypePublicProperty() { PropertyType pt; //public property pt = new PropertyType(typeof(ia), "PropertyA"); Assert.AreEqual("PropertyA", pt.Name); Assert.AreEqual(typeof(string), pt.Type); }
/// <summary> /// Walks a heirarchy of properties from the given type down. You can specify in any of the /// following ways: "ClientRectangle.X", "ClientRectangle/X" /// </summary> /// <example> /// <code> /// //dotted notation: /// PropertyType pt = PropertyType.TraverseProperties(typeof(Form), "ClientRectangle.X"); /// //path notation: /// PropertyType pt = PropertyType.TraverseProperties(typeof(Form), "ClientRectangle/X"); /// //individual names: /// PropertyType pt = PropertyType.TraverseProperties(typeof(Form), "ClientRectangle", "X"); /// </code> /// </example> /// <param name="fromType">Any System.Type object to traverse from</param> /// <param name="propertyNames">The name of the properties or fields usually '.' delimited</param> public static PropertyType TraverseProperties(Type fromType, params string[] propertyNames) { PropertyType prop = null; Type t = fromType; foreach (string propnames in Check.NotEmpty(propertyNames)) { foreach (string name in Check.NotEmpty(propnames).Split('.', '/', '\\')) { prop = new PropertyType(t, Check.NotEmpty(name)); t = prop.Type; } } return Check.NotNull(prop); }
public void TestPropertyTypeExplicitIntefaceProperty() { PropertyType pt; //PropertyB is an explicit interface implementation which does not //generate a property on this class, as such it is not possible to //reflect directly via the class, but only via the interface. Yes, //the get_x and set_x methods are there, but no property. pt = new PropertyType(typeof(b), "PropertyB"); Assert.AreEqual("PropertyB", pt.Name); Assert.AreEqual(typeof(ia), pt.Type); }
public void TestPropertyTypeAttributes() { PropertyType pt = new PropertyType(typeof(a), "PropertyA"); Assert.IsTrue(pt.IsDefined(typeof(System.ComponentModel.DisplayNameAttribute), false)); Assert.AreEqual(1, pt.GetCustomAttributes(false).Length); Assert.AreEqual(1, pt.GetCustomAttributes(typeof(System.ComponentModel.DisplayNameAttribute), false).Length); Assert.AreEqual(0, pt.GetCustomAttributes(typeof(System.ComponentModel.DesignOnlyAttribute), false).Length); }
public void TestPropertyTypeGetSet() { a classa = new a(); classa.PropertyA = "hello"; Assert.AreEqual("hello", classa.PropertyA); PropertyType pt = new PropertyType(classa.GetType(), "PropertyA"); Assert.AreEqual("hello", pt.GetValue(classa)); pt.SetValue(classa, "world"); Assert.AreEqual("world", pt.GetValue(classa)); Assert.AreEqual("world", classa.PropertyA); classa.PropertyA = "hello";//set privateField back to "hello" Assert.AreEqual("hello", classa.PropertyA); pt = new PropertyType(classa.GetType(), "privateField"); Assert.AreEqual("hello", pt.GetValue(classa)); pt.SetValue(classa, "world"); Assert.AreEqual("world", pt.GetValue(classa)); Assert.AreEqual("world", classa.PropertyA); }