/// <summary> /// Sets metainfo about the xml file where meta info is saved in /// </summary> private static void Initialize() { if (!_initialized) { MetaInfo.SetAttribute(typeof(EntryList), "XmlItemType", "Oatc.OpenMI.Sdk.DevelopmentSupport.MetaInfoEntry"); MetaInfo.SetAttribute(typeof(MetaInfoEntry), "Properties", "XmlItemType", "Oatc.OpenMI.Sdk.DevelopmentSupport.MetaInfoClass"); MetaInfo.SetAttribute(typeof(MetaInfoClass), "ObjectAggregate", "Oatc.OpenMI.Sdk.DevelopmentSupport.MetaInfoClassAggregate"); _initialized = true; } }
/// <summary> /// Gets all metainfo concerning a given class and creates an empty block if not found /// </summary> /// <param name="target">The class name</param> /// <returns>All metainfo about a class</returns> private static MetaInfoEntry GetEntryForced(string target) { MetaInfoEntry entry = MetaInfo.GetEntry(target); if (entry == null) { entry = new MetaInfoEntry(target); _targets.Add(entry); } return(entry); }
/// <summary> /// Gets the stored information for a string /// </summary> /// <param name="target">The string about which information will be retrieved</param> /// <param name="subject">The type of information required</param> /// <param name="defaultValue">Default value if the information is not found</param> /// <returns>The information stored for this class, property and type, the default value if not found</returns> public static object GetAttributeDefault(string target, string subject, object defaultValue) { MetaInfoEntry table = MetaInfo.GetEntry(target); if (table != null) { if (table.Contains(null, subject)) { return(table.GetValue(null, subject)); } } return(defaultValue); }
/// <summary> /// Stores information about a class and property /// </summary> /// <param name="target">The class about which information is stored (usually as class type or string)</param> /// <param name="property">The property in the class</param> /// <param name="subject">The type of information (e.g. how the property is named in an xml file)</param> /// <param name="targetValue">The actual value</param> public static void SetAttribute(object target, string property, string subject, object targetValue) { if (target is Type) { target = ((Type)target).FullName; } else if (target is Assembly) { target = ((Assembly)target).GetName().Name; } MetaInfoEntry table = MetaInfo.GetEntryForced(target.ToString()); table.SetValue(property, subject, targetValue); }
/// <summary> /// Gets a list of all properties in a class, for which a value has been stored. /// All superclasses and implemented interfaces of the class are examined too. /// </summary> /// <param name="targetClass">The class</param> /// <returns>List of properties</returns> public static string[] GetProperties(Type targetClass) { // try to find appropriate value in super class ArrayList subjects = new ArrayList(); Type targetType = targetClass; while (targetType != null) { string fullName = targetType.FullName; MetaInfoEntry entry = MetaInfo.GetEntry(fullName); if (entry != null) { foreach (MetaInfoClass property in entry.Properties) { if (!subjects.Contains(property.Name)) { subjects.Add(property.Name); } } } targetType = targetType.BaseType; } // try to find interfaces implemented by the target type foreach (Type implementedInterface in targetClass.GetInterfaces()) { string[] interfaceProperties = MetaInfo.GetProperties(implementedInterface); foreach (string interfaceProperty in interfaceProperties) { if (!subjects.Contains(interfaceProperty)) { subjects.Add(interfaceProperty); } } } return((string[])subjects.ToArray(typeof(string))); }
/// <summary> /// Reads all metainfo from file /// </summary> /// <param name="file">The file</param> public static void Read(FileInfo file) { MetaInfo.Initialize(); XmlFile.Read(_targets, file); }
/// <summary> /// Writes all metainfo to a file /// </summary> /// <param name="file">The file</param> public static void Write(FileInfo file) { MetaInfo.Initialize(); XmlFile.Write(_targets, file); }
/// <summary> /// Gets the stored information for an assembly. /// </summary> /// <param name="targetAssembly">The assembly about which information will be retrieved</param> /// <param name="subject">The type of information required</param> /// <param name="defaultValue">Default value if the information is not found</param> /// <returns>The information stored for this class, property and type, the default value if not found</returns> public static object GetAttributeDefault(Assembly targetAssembly, string subject, object defaultValue) { return(MetaInfo.GetAttributeDefault(targetAssembly.GetName().Name, subject, defaultValue)); }
/// <summary> /// Gets the stored information for a class. /// Not only the class is examined, but also all superclasses and implemented interfaces. /// </summary> /// <param name="targetClass">The object about which information will be retrieved</param> /// <param name="subject">The type of information required</param> /// <param name="defaultValue">Default value if the information is not found</param> /// <returns>The information stored for this object and type, the default value if not found</returns> public static object GetAttributeDefault(Type targetClass, string subject, object defaultValue) { return(MetaInfo.GetAttributeDefault(targetClass, null, subject, defaultValue)); }
/// <summary> /// Gets the stored information for a class and property. /// Not only the class is examined, but also all superclasses and implemented interfaces. /// </summary> /// <param name="target">The class type about which information will be retrieved</param> /// <param name="property">The property for which information is to be required</param> /// <param name="subject">The type of information required</param> /// <returns>The information stored for the class and property, null if not found</returns> public static object GetAttribute(Type target, string property, string subject) { return(MetaInfo.GetAttributeDefault(target, property, subject, null)); }
/// <summary> /// Stores information about a class /// </summary> /// <param name="target">The class about which information is stored (usually as class type or string)</param> /// <param name="subject">The type of information (e.g. how the class is named in an xml file)</param> /// <param name="targetValue">The actual value</param> public static void SetAttribute(object target, string subject, object targetValue) { MetaInfo.SetAttribute(target, null, subject, targetValue); }
/// <summary> /// Gets the deep copy /// </summary> /// <param name="source">The object to be copied</param> /// <param name="target">The object which will be the copy</param> /// <param name="copiedObjects">Collection of objects and their copied equivalents. Will be populated and queried during copy.</param> /// <param name="path">Relative path to location where files will be copied to</param> /// <param name="copyValue">Indicates whether the new object should refer to a copy or to the same object</param> private static void Copy(object source, object target, Hashtable copiedObjects, string path, bool copyValue) { if (source is IList) { IList copiedArray = (IList)target; copiedArray.Clear(); for (int j = 0; j < ((IList)source).Count; j++) { object copiedArrayValue = ((IList)source)[j]; if (copyValue) { copiedArrayValue = ObjectSupport.GetCopy(copiedArrayValue, copiedObjects, path); } copiedArray.Add(copiedArrayValue); } } else if (source is IDictionary) { IDictionary copiedArray = (IDictionary)target; copiedArray.Clear(); IDictionaryEnumerator dictionaryEnumerator = ((IDictionary)source).GetEnumerator(); while (dictionaryEnumerator.MoveNext()) { object copiedArrayKey = dictionaryEnumerator.Key; object copiedArrayValue = dictionaryEnumerator.Value; if (copyValue) { copiedArrayKey = ObjectSupport.GetCopy(copiedArrayKey, copiedObjects, path); copiedArrayValue = ObjectSupport.GetCopy(copiedArrayValue, copiedObjects, path); } if (!copiedArray.Contains(copiedArrayKey)) { copiedArray.Add(copiedArrayKey, copiedArrayValue); } } } // Special handling for files else if (source is FileSystemInfo) { if (copyValue && (path != null) && (!path.Trim().Equals(""))) { object copiedValue = CopyFile((FileSystemInfo)source, path); } } else { PropertyInfo[] property = source.GetType().GetProperties(); for (int i = 0; i < property.Length; i++) { object sourceValue = property[i].GetValue(source, null); if (sourceValue != null) { // Default copied value. This is just a reference to the original value object copiedValue = sourceValue; // If the value has been copied before, use the same copied equivalent if (copiedObjects.ContainsKey(sourceValue)) { copiedValue = copiedObjects[sourceValue]; } else { // Determine whether the property is to be copied bool copyPropertyValue = (Boolean)MetaInfo.GetAttributeDefault(source.GetType(), property[i].Name, "ObjectCopy", false); if (copyPropertyValue) { if (property[i].CanWrite) { copiedValue = ObjectSupport.GetCopy(sourceValue, copiedObjects, path); } else { object targetValue = property[i].GetValue(target, null); Copy(sourceValue, targetValue, copiedObjects, path); } } else if ((sourceValue is IList) || (sourceValue is IDictionary)) { object targetValue = property[i].GetValue(target, null); Copy(sourceValue, targetValue, copiedObjects, path, copyPropertyValue); } } // Populate the target object if (property[i].CanWrite) { property[i].SetValue(target, copiedValue, null); } if (!copiedObjects.Contains(sourceValue)) { copiedObjects.Add(sourceValue, copiedValue); } } } } }