/// <summary> /// Generates all the instance nodes. /// </summary> /// <param name="parent"></param> /// <param name="source"></param> /// <param name="sourceType"></param> private static void GenerateInstanceNodes(TreeNode parent, ICCNetObject source, Type sourceType) { InstanceTreeNodeAttribute[] attributes = CoreUtil.GetCustomAttributes <InstanceTreeNodeAttribute>(sourceType); foreach (InstanceTreeNodeAttribute attribute in attributes) { ReflectionInstanceTreeNode node = new ReflectionInstanceTreeNode(source, attribute); node.Text = attribute.NodeName; parent.Nodes.Add(node); } }
/// <summary> /// Generates the child nodes based on the attributes. /// </summary> /// <param name="parent">The node to add the children to.</param> /// <param name="source">The object to retrieve the settings from.</param> public static void GenerateChildNodes(TreeNode parent, ICCNetObject source) { if (source != null) { Type sourceType = source.GetType(); GenerateListNodes(parent, source, sourceType); GenerateInstanceNodes(parent, source, sourceType); } UpdateImages(parent); }
/// <summary> /// Changes the underlying data value. /// </summary> /// <returns></returns> private ICCNetObject ChangeDataValue(ICCNetObject value) { ICCNetObject data = null; Type dataType = parent.GetType(); PropertyInfo property = dataType.GetProperty(source.Property); if (property != null) { property.SetValue(parent, value, new object[0]); } return(data); }
/// <summary> /// Initialise a new <see cref="ReflectionItemTreeNode"/>. /// </summary> /// <param name="value">The associated data item.</param> /// <param name="source">The source of the data.</param> public ReflectionItemTreeNode(ICCNetObject value, ListTreeNodeAttribute source) : base(value) { this.source = source; if (value != null) { Text = value.ToString(); if (value is INotifyPropertyChanged) { (value as INotifyPropertyChanged).PropertyChanged += OnPropertyChanged; } } }
/// <summary> /// Retrieves the underlying data value. /// </summary> /// <returns></returns> private ICCNetObject RetrieveDataValue() { ICCNetObject data = null; Type dataType = parent.GetType(); PropertyInfo property = dataType.GetProperty(source.Property); if (property != null) { data = property.GetValue(parent, new object[0]) as ICCNetObject; } if (data == null) { data = Activator.CreateInstance(source.ItemType) as ICCNetObject; } return(data); }
/// <summary> /// Initialise a new <see cref="ReflectionInstanceTreeNode"/>. /// </summary> /// <param name="value">The associated data item.</param> /// <param name="source">The source of the data.</param> public ReflectionInstanceTreeNode(ICCNetObject value, InstanceTreeNodeAttribute source) : base(value) { this.source = source; if (value != null) { Text = value.ToString(); if (value is INotifyPropertyChanged) { (value as INotifyPropertyChanged).PropertyChanged += OnPropertyChanged; } } this.parent = value; DataItem = RetrieveDataValue(); ReflectionHelper.GenerateChildNodes(this, DataItem); }
/// <summary> /// Generates the change menu item. /// </summary> /// <returns></returns> private ToolStripMenuItem GenerateChangeMenu() { ToolStripMenuItem menuItem = new ToolStripMenuItem("Change to"); menuItem.Image = CCNetConfig.Properties.Resources.securitychange_16x16; List <Type> allowedItems = CoreUtil.GetAllItemsOfType(source.ItemType); foreach (Type allowedItem in allowedItems) { // Retrieve the display name of the security mode string displayName = allowedItem.Name; DisplayNameAttribute attribute = CoreUtil.GetCustomAttribute <DisplayNameAttribute>(allowedItem); if (attribute != null) { displayName = attribute.DisplayName; } // Add the actual menu item ToolStripMenuItem securityMenuItem = new ToolStripMenuItem(displayName); securityMenuItem.Image = CCNetConfig.Properties.Resources.applications_16x16; securityMenuItem.Tag = allowedItem; menuItem.DropDownItems.Add(securityMenuItem); securityMenuItem.Click += delegate(object sender, EventArgs e) { // Generate the new instance and update the display ICCNetObject value = Activator.CreateInstance((sender as ToolStripMenuItem).Tag as Type) as ICCNetObject; DataItem = value; ChangeDataValue(value); UpdateDisplay(); Nodes.Clear(); ReflectionHelper.GenerateChildNodes(this, DataItem); }; } return(menuItem); }
/// <summary> /// Initialise a new <see cref="ReflectionListTreeNode"/>. /// </summary> /// <param name="value">The associated data item.</param> /// <param name="source">The source of the data.</param> public ReflectionListTreeNode(ICCNetObject value, ListTreeNodeAttribute source) { dataItem = value; this.source = source; }
/// <summary> /// Deserializes the specified element. /// </summary> /// <param name="element">The element.</param> /// <param name="baseObject">The base object.</param> public void Deserialize(XmlElement element, T baseObject) { // this dynamically resets all the reflector properties only if they // are writeable and public Util.ResetObjectProperties <T> (baseObject); Type type = baseObject.GetType(); // get the reflector name for the type string rootTypeName = Util.GetReflectorNameAttributeValue(type); // get the version info for the type Version versionInfo = Util.GetTypeDescriptionProviderVersion(typeof(T)); // throw exception if we can not convert from the element to the type. if (string.Compare(element.Name, rootTypeName, false) != 0) { throw new InvalidCastException(string.Format("Unable to convert {0} to a {1}", element.Name, rootTypeName)); } List <PropertyInfo> props = GetReflectorProperies(baseObject); foreach (PropertyInfo pi in props) { string rname = Util.GetReflectorNameAttributeValue(pi); RequiredAttribute ra = Util.GetCustomAttribute <RequiredAttribute> (pi); Type valType = pi.PropertyType; XmlNode subElement = null; if (string.Compare(element.Name, rname, false) == 0) { subElement = element; } else { subElement = element.SelectSingleNode(rname) as XmlNode; } if (subElement == null) { subElement = element.SelectSingleNode(string.Format("@{0}", rname)); } bool required = ra != null; // property is required but it doesn't exist if (required && subElement == null) { throw new RequiredAttributeException(baseObject, rname); } // element doesn't exist but it's not required so we can move on if (subElement == null) { continue; } if (Util.IsNullable(valType)) { Type nullType = Nullable.GetUnderlyingType(valType); valType = nullType; } if (valType.IsPrimitive || valType == typeof(string) || valType == typeof(DateTime) || valType.IsEnum) { pi.SetValue(baseObject, StringTypeConverter.Convert(subElement.InnerText, valType), null); } else { if (valType.GetInterface(typeof(ICCNetObject).FullName) != null) { ConstructorInfo ci = valType.GetConstructor(new Type[] { }); if (ci == null) { throw new ArgumentException(string.Format("Unable to locate default constructor for type {0}", valType.Name)); } else { ICCNetObject valObject = ci.Invoke(null) as ICCNetObject; valObject.Deserialize(subElement as XmlElement); pi.SetValue(baseObject, valObject, null); } } else if (valType.IsGenericType && valType.GetGenericTypeDefinition().Equals(typeof(CloneableList <>))) { IList vlist = pi.GetValue(baseObject, null) as IList; Type gtype = valType.GetGenericArguments()[0]; if (vlist == null) { // create a new instance since it's null ConstructorInfo ci = valType.GetConstructor(new Type[] { }); if (ci == null) { throw new ArgumentException(string.Format("Unable to locate default constructor for type {0}", valType.Name)); } else { vlist = ci.Invoke(null) as IList; } } ReflectorArrayAttribute raa = Util.GetCustomAttribute <ReflectorArrayAttribute> (pi); if (raa != null) { XmlNodeList nodes = subElement.SelectNodes(raa.ItemName); foreach (XmlElement itemElement in nodes) { if (gtype.IsPrimitive || gtype == typeof(string)) { vlist.Add(StringTypeConverter.Convert(itemElement.InnerText, gtype)); } else if (gtype.GetInterface(typeof(ICCNetObject).FullName) != null) { ICCNetObject obj = null; ConstructorInfo ci = gtype.GetConstructor(new Type[] { }); if (ci == null) { throw new ArgumentException(string.Format("Unable to locate default constructor for type {0}", valType.Name)); } else { obj = ci.Invoke(null) as ICCNetObject; } obj.Deserialize(itemElement); vlist.Add(obj); } } } else { // it doesn't have an reflector array attribute, so we can assume that all items are ICCNetObects. XmlNodeList nodes = subElement.SelectNodes("*"); foreach (XmlElement itemElement in nodes) { if (gtype.GetInterface(typeof(ICCNetObject).FullName) != null) { ICCNetObject obj = null; ConstructorInfo ci = gtype.GetConstructor(new Type[] { }); if (ci == null) { throw new ArgumentException(string.Format("Unable to locate default constructor for type {0}", valType.Name)); } else { obj = ci.Invoke(null) as ICCNetObject; } obj.Deserialize(itemElement); vlist.Add(obj); } else { throw new ArgumentException(string.Format("Type {0} is not assignable from {1}", gtype.Name, typeof(ICCNetObject).Name)); } } } pi.SetValue(baseObject, vlist, null); } else if (valType == typeof(HiddenPassword)) { HiddenPassword hp = pi.GetValue(baseObject, null) as HiddenPassword; if (hp == null) { // need to create it. ConstructorInfo ci = valType.GetConstructor(new Type[] { }); if (ci == null) { throw new ArgumentException(string.Format("Unable to locate default constructor for type {0}", valType.Name)); } else { hp = ci.Invoke(null) as HiddenPassword; } } hp.Password = subElement.InnerText; pi.SetValue(baseObject, hp, null); } } } }
/// <summary> /// Initializes a new instance of the <see cref="RequiredAttributeException"/> class. /// </summary> /// <param name="owner">The owner.</param> /// <param name="name">The name.</param> public RequiredAttributeException(ICCNetObject owner, string name) : base(string.Format("Value of {0} in {1} can not be null or Empty", name, string.IsNullOrEmpty(owner.ToString()) ? owner.GetType().Name : owner.ToString())) { }
/// <summary> /// Initializes a new instance of the <see cref="RequiredAttributeException"/> class. /// </summary> /// <param name="owner">The owner.</param> public RequiredAttributeException(ICCNetObject owner) : this(owner, "UNKNOWN") { }
/// <summary> /// Initialises a new <see cref="ValidatingTreeNode"/> /// </summary> /// <param name="dataItem"></param> /// <param name="name"></param> /// <param name="indexImage"></param> /// <param name="selectedImageIndex"></param> public ValidatingTreeNode(ICCNetObject dataItem, string name, int indexImage, int selectedImageIndex) : base(name, indexImage, selectedImageIndex) { this.dataItem = dataItem; }
/// <summary> /// Initialises a new <see cref="ValidatingTreeNode"/>. /// </summary> /// <param name="dataItem"></param> /// <param name="name"></param> public ValidatingTreeNode(ICCNetObject dataItem, string name) : this(dataItem, name, 0, 0) { }
/// <summary> /// Initialises a new <see cref="ValidatingTreeNode"/>. /// </summary> /// <param name="dataItem"></param> public ValidatingTreeNode(ICCNetObject dataItem) : this(dataItem, string.Empty, 0, 0) { }