// Intentionally empty currently. May be extended one day to specify loading/inlining information to skip inlining for certain packages. #region Methods /// <summary> /// Searches for the <see cref="T:AlarmWorkflowPackageAttribute"/> defined in the given assembly. /// </summary> /// <param name="assembly">The assembly to search in.</param> /// <param name="attribute">If the attribute was defined in the assembly, contains it as the result.</param> /// <returns>Whether or not the attribute was specified in the assembly.</returns> public static bool TryGetAttribute(Assembly assembly, out AlarmWorkflowPackageAttribute attribute) { Assertions.AssertNotNull(assembly, "assembly"); AlarmWorkflowPackageAttribute[] attributes = (AlarmWorkflowPackageAttribute[])assembly.GetCustomAttributes(typeof(AlarmWorkflowPackageAttribute), false); attribute = attributes.FirstOrDefault(); return(attribute != null); }
/// <summary> /// Adds all items from the specified enumerable to the specified collection. /// </summary> /// <typeparam name="T">The type of the items.</typeparam> /// <param name="collection">The collection to add all items to.</param> /// <param name="enumerable">The enumerable containing the items to add.</param> public static void AddRange <T>(this ICollection <T> collection, IEnumerable <T> enumerable) { Assertions.AssertNotNull(collection, "collection"); Assertions.AssertNotNull(enumerable, "enumerable"); foreach (T item in enumerable) { collection.Add(item); } }
/// <summary> /// Performs a check against a <see cref="XDocument"/> to see if the schema is valid. /// </summary> /// <param name="doc">The <see cref="XDocument"/> to check its schema.</param> /// <param name="schema">The schema to use for validation.</param> public static void ValidateXml(this XDocument doc, string schema) { Assertions.AssertNotNull(doc, "doc"); Assertions.AssertNotEmpty(schema, "schema"); XmlSchemaSet xss = new XmlSchemaSet(); xss.Add(string.Empty, XmlReader.Create(new StringReader(schema))); doc.Validate(xss, null, false); }
/// <summary> /// Makes an inverse search to find out the alias for a specific type. /// </summary> /// <param name="type">The type to get the alias for. Must not be null.</param> /// <returns>A string representing the alias for the given type.</returns> public static string GetExportAlias(Type type) { Assertions.AssertNotNull(type, "type"); ExportAttribute[] attributes = (ExportAttribute[])type.GetCustomAttributes(typeof(ExportAttribute), false); if (attributes.Length == 1) { return(attributes[0].Alias); } return(null); }
/// <summary> /// Retrieves the resource string of a resource specified in the assembly's main resources. /// </summary> /// <param name="sourceType">The type of which to infer the assembly's resources from.</param> /// <param name="resourceName">The resource name to retrieve the string resource.</param> /// <returns>The resource specified by the given name. /// -or- null, if no resource with such name was found.</returns> public static string GetResourceString(this Type sourceType, string resourceName) { Assertions.AssertNotNull(sourceType, "sourceType"); Assertions.AssertNotEmpty(resourceName, "resourceName"); string resmantype = sourceType.Assembly.GetName().Name + ".Properties.Resources"; ResourceManager resman = new ResourceManager(resmantype, sourceType.Assembly); return(resman.GetString(resourceName)); }
/// <summary> /// Tries to return the value from the given key, or returns a default value if not found. /// </summary> /// <typeparam name="TKey">The type of the key.</typeparam> /// <typeparam name="TValue">The type of the value.</typeparam> /// <param name="dictionary">The dictionary.</param> /// <param name="key">The key to return the value from.</param> /// <param name="defaultValue">The default value, if the key was not found.</param> /// <returns>Returns the value from the given key, or returns a default value if not found.</returns> public static TValue SafeGetValue <TKey, TValue>(this IDictionary <TKey, TValue> dictionary, TKey key, TValue defaultValue) { Assertions.AssertNotNull(dictionary, "dictionary"); TValue value = default(TValue); if (dictionary.TryGetValue(key, out value)) { return(value); } return(defaultValue); }
/// <summary> /// Returns the tag of the given type, or nothing if no <see cref="InformationAttribute"/> was specified. /// </summary> /// <param name="type"></param> /// <returns></returns> public static object GetTag(Type type) { Assertions.AssertNotNull(type, "type"); InformationAttribute attribute = GetAttribute(type); if (attribute != null) { return(attribute.Tag); } return(String.Empty); }
/// <summary> /// Returns the description of the given type, or nothing if no <see cref="InformationAttribute"/> was specified. /// </summary> /// <param name="type"></param> /// <returns></returns> public static string GetDescription(Type type) { Assertions.AssertNotNull(type, "type"); InformationAttribute attribute = GetAttribute(type); if (attribute != null) { string res = type.GetResourceString(attribute.Description); return(res ?? attribute.Description); } return(String.Empty); }
/// <summary> /// Returns the display name of the given type, or the type name itself if no <see cref="InformationAttribute"/> was specified. /// </summary> /// <param name="type"></param> /// <returns></returns> public static string GetDisplayName(Type type) { Assertions.AssertNotNull(type, "type"); InformationAttribute attribute = GetAttribute(type); if (attribute != null) { string res = type.GetResourceString(attribute.DisplayName); return(res ?? attribute.DisplayName); } return(type.Name); }
/// <summary> /// Computes the SHA1-checksum of a given buffer. /// </summary> /// <param name="buffer">The buffer to compute its checksum from. Must not be null.</param> /// <returns>The SHA1-checksum of the given buffer as a string.</returns> public static string ComputeSHA1(byte[] buffer) { Assertions.AssertNotNull(buffer, "buffer"); using (SHA1 md5 = SHA1.Create()) { byte[] result = md5.ComputeHash(buffer); StringBuilder strBuilder = new StringBuilder(); for (int i = 0; i < result.Length; i++) { strBuilder.Append(result[i].ToString("x2")); } return(strBuilder.ToString()); } }
/// <summary> /// Computes the MD5-checksum of a given stream. /// </summary> /// <param name="stream">The stream to compute its checksum from. Must not be null.</param> /// <returns>The MD5-checksum of the given stream as a string.</returns> public static string ComputeMD5(Stream stream) { Assertions.AssertNotNull(stream, "stream"); using (MD5 md5 = MD5.Create()) { byte[] result = md5.ComputeHash(stream); StringBuilder strBuilder = new StringBuilder(); for (int i = 0; i < result.Length; i++) { strBuilder.Append(result[i].ToString("x2")); } return(strBuilder.ToString()); } }
/// <summary> /// Performs a check against a <see cref="XDocument"/> to see if the schema is valid. /// </summary> /// <param name="doc">The <see cref="XDocument"/> to check its schema.</param> /// <param name="schema">The schema to use for validation.</param> /// <returns>A boolean value indicating whether or not the schema of the given <see cref="XDocument"/> is valid, or not.</returns> public static bool IsXmlValid(this XDocument doc, string schema) { Assertions.AssertNotNull(doc, "doc"); Assertions.AssertNotEmpty(schema, "schema"); try { ValidateXml(doc, schema); } catch (XmlSchemaException) { return(false); } catch (XmlException) { return(false); } return(true); }