public static void SetExtensionDataAttributeValue(this object o, string name, object value) { if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentException("Invalid name"); } try { foreach (Attribute a in System.ComponentModel.TypeDescriptor.GetAttributes(o)) { if (a is ExtensionDataAttribute) { ((ExtensionDataAttribute)a)[name] = value; return; } } } catch { } try { ExtensionDataAttribute extensionData = new ExtensionDataAttribute(); extensionData[name] = value; extensionData.AddTo(o); } catch { } }
/// <summary> /// Adds a description to the specified string object /// </summary> /// <param name="s">The string to describe</param> /// <param name="description">The description to set</param> public static void SetDescription(this string s, string description) { if (string.IsNullOrWhiteSpace(description)) { description = ""; } foreach (Attribute a in System.ComponentModel.TypeDescriptor.GetAttributes(s)) { if (a is ExtensionDataAttribute) { ((ExtensionDataAttribute)a)["Description"] = description; return; } } ExtensionDataAttribute extensionData = new ExtensionDataAttribute(); extensionData["Description"] = description; extensionData.AddTo(s); }