/// <summary> /// <para>Searches for a named property, declared within a specified <see cref="Type"/>.</para> /// <para>Returns <see cref="PropertyInfo"/> object, representing either instance or static property with either private or public access level.</para> /// </summary> /// <param name="self">Type whose property is to be located.</param> /// <param name="name">Unique name of property.</param> /// <returns><see cref="PropertyInfo"/> object representing the property of <paramref name="self"/>. If property cannot be found, returns <c>null</c>.</returns> /// <exception cref="ArgumentNullException">If either <paramref name="self"/> or <paramref name="name"/> is a <c>null</c> reference.</exception> /// <exception cref="ArgumentException">If <paramref name="name"/> is <see cref="string.Empty"/> string.</exception> /// <seealso cref="Type.GetProperty(string, BindingFlags)"/> public static PropertyInfo AnyProperty(this Type self, string name) { Assertion.NotNull(self); Assertion.NotEmpty(name); return(self.GetProperty(name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static)); }
public void assert_not_empty() { Assert.Throws <ArgumentNullException>(() => Assertion.NotEmpty(null)); Assert.Throws <ArgumentException>(() => Assertion.NotEmpty(Enumerable.Empty <object>())); Assert.Throws <ArgumentException>(() => Assertion.NotEmpty(new object[] { })); Assertion.NotEmpty(new[] { new object() }); }
/// <summary> /// <para>Returns contents of specified embedded text resource of the assembly.</para> /// <para>Returns a <c>null</c> reference if resource with specified name cannot be found.</para> /// </summary> /// <param name="self">Assembly with resource.</param> /// <param name="name">The case-sensitive name of the manifest resource being requested.</param> /// <param name="encoding">Optional encoding to be used when reading resource's data. If not specified, <see cref="Encoding.UTF8"/> encoding is used.</param> /// <returns>Text data of assembly manifest's resource.</returns> /// <exception cref="ArgumentNullException">If either <paramref name="self"/> or <paramref name="name"/> is a <c>null</c> reference.</exception> /// <exception cref="ArgumentException">If <paramref name="name"/> is <see cref="string.Empty"/> string.</exception> public static string Resource(this Assembly self, string name, Encoding encoding = null) { Assertion.NotNull(self); Assertion.NotEmpty(name); return(self.GetManifestResourceStream(name)?.Text(true, encoding)); }
/// <summary> /// <para>Determines whether there is a named property, declared within a specified <see cref="Type"/>.</para> /// </summary> /// <param name="self">Type whose property is to be located.</param> /// <param name="name">Unique name of property.</param> /// <returns><c>true</c> if either instance or static property with either private or public access level is declared for <paramref name="self"/>, <c>false</c> otherwise.</returns> /// <exception cref="ArgumentNullException">If either <paramref name="self"/> or <paramref name="name"/> is a <c>null</c> reference.</exception> /// <exception cref="ArgumentException">If <paramref name="name"/> is <see cref="string.Empty"/> string.</exception> public static bool HasProperty(this Type self, string name) { Assertion.NotNull(self); Assertion.NotEmpty(name); return(self.AnyProperty(name) != null); }
/// <summary> /// <para>Sets the value of given property on specified target object.</para> /// </summary> /// <typeparam name="T">Type of target object.</typeparam> /// <param name="self">Target object whose property is to be changed.</param> /// <param name="property">Name of property to change.</param> /// <param name="value">New value of object's property.</param> /// <returns>Back reference to the current target object.</returns> /// <exception cref="ArgumentNullException">If either <paramref name="self"/> or <paramref name="property"/> is a <c>null</c> reference.</exception> /// <exception cref="ArgumentException">If <paramref name="property"/> is <see cref="string.Empty"/> string.</exception> /// <seealso cref="Property(object, string)"/> /// <seealso cref="Properties{T}(T, IDictionary{string, object})"/> /// <seealso cref="Properties{T}(T, object)"/> public static T Property <T>(this T self, string property, object value) { Assertion.NotNull(self); Assertion.NotEmpty(property); self.GetType().AnyProperty(property)?.SetValue(self, value, null); return(self); }
/// <summary> /// <para>Creates new instance of attribute.</para> /// </summary> /// <param name="properties">Comma-separated list of properties to be used in equality comparison and hash codes calculation.</param> /// <exception cref="ArgumentNullException">If <paramref name="properties"/> is a <c>null</c> reference.</exception> /// <exception cref="ArgumentException">If <paramref name="properties"/> is <see cref="string.Empty"/> string.</exception> public EqualsAndHashCodeAttribute(string properties) { Assertion.NotEmpty(properties); this.properties = properties.Split(',').Select(property => property.Trim()).ToArray(); }
/// <summary> /// <para>Converts specified string into <see cref="Uri"/> value.</para> /// </summary> /// <param name="self">String to be converted.</param> /// <returns>The <see cref="Uri"/> value to which string <paramref name="self"/> was converted.</returns> /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception> /// <exception cref="ArgumentException">If <paramref name="self"/> is <see cref="string.Empty"/> string.</exception> /// <seealso cref="ToUri(string, out Uri)"/> public static Uri ToUri(this string self) { Assertion.NotEmpty(self); return(new Uri(self)); }
/// <summary> /// <para>Converts specified string into <see cref="Single"/> value.</para> /// </summary> /// <param name="self">String to be converted.</param> /// <returns>The <see cref="Single"/> value to which string <paramref name="self"/> was converted.</returns> /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception> /// <exception cref="ArgumentException">If <paramref name="self"/> is empty string.</exception> /// <seealso cref="Single.Parse(string)"/> /// <seealso cref="ToSingle(string, out Single)"/> public static Single ToSingle(this string self) { Assertion.NotEmpty(self); return(Single.Parse(self)); }
/// <summary> /// <para>Converts specified string into <see cref="long"/> value.</para> /// </summary> /// <param name="self">String to be converted.</param> /// <returns>The <see cref="long"/> value to which string <paramref name="self"/> was converted.</returns> /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception> /// <exception cref="ArgumentException">If <paramref name="self"/> is empty string.</exception> /// <seealso cref="long.Parse(string)"/> /// <seealso cref="ToInt64(string, out long)"/> public static long ToInt64(this string self) { Assertion.NotEmpty(self); return(long.Parse(self)); }
/// <summary> /// <para>Converts specified string into <see cref="short"/> value.</para> /// </summary> /// <param name="self">String to be converted.</param> /// <returns>The <see cref="short"/> value to which string <paramref name="self"/> was converted.</returns> /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception> /// <exception cref="ArgumentException">If <paramref name="self"/> is empty string.</exception> /// <seealso cref="short.Parse(string)"/> /// <seealso cref="ToInt16(string, out short)"/> public static short ToInt16(this string self) { Assertion.NotEmpty(self); return(short.Parse(self)); }
/// <summary> /// <para>Converts specified string into <see cref="Guid"/> value.</para> /// </summary> /// <param name="self">String to be converted.</param> /// <returns>The <see cref="Guid"/> value to which string <paramref name="self"/> was converted.</returns> /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception> /// <exception cref="ArgumentException">If <paramref name="self"/> is empty string.</exception> /// <seealso cref="ToGuid(string, out Guid)"/> public static Guid ToGuid(this string self) { Assertion.NotEmpty(self); return(new Guid(self)); }
/// <summary> /// <para>Converts specified string into <see cref="double"/> value.</para> /// </summary> /// <param name="self">String to be converted.</param> /// <returns>The <see cref="double"/> value to which string <paramref name="self"/> was converted.</returns> /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception> /// <exception cref="ArgumentException">If <paramref name="self"/> is empty string.</exception> /// <seealso cref="double.Parse(string)"/> /// <seealso cref="ToDouble(string, out double)"/> public static double ToDouble(this string self) { Assertion.NotEmpty(self); return(double.Parse(self)); }
/// <summary> /// <para>Converts specified string into <see cref="decimal"/> value.</para> /// </summary> /// <param name="self">String to be converted.</param> /// <returns>The <see cref="decimal"/> value to which string <paramref name="self"/> was converted.</returns> /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception> /// <exception cref="ArgumentException">If <paramref name="self"/> is empty string.</exception> /// <seealso cref="decimal.Parse(string)"/> /// <seealso cref="ToDecimal(string, out decimal)"/> public static decimal ToDecimal(this string self) { Assertion.NotEmpty(self); return(decimal.Parse(self)); }
/// <summary> /// <para>Converts specified string into <see cref="DateTime"/> value.</para> /// </summary> /// <param name="self">String to be converted.</param> /// <returns>The <see cref="DateTime"/> value to which string <paramref name="self"/> was converted.</returns> /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception> /// <exception cref="ArgumentException">If <paramref name="self"/> is <see cref="string.Empty"/> string.</exception> /// <seealso cref="DateTime.Parse(string)"/> /// <seealso cref="ToDateTime(string, out DateTime)"/> public static DateTime ToDateTime(this string self) { Assertion.NotEmpty(self); return(DateTime.Parse(self)); }
/// <summary> /// <para>Converts specified string into <see cref="byte"/> value.</para> /// </summary> /// <param name="self">String to be converted.</param> /// <returns>The <see cref="byte"/> value to which string <paramref name="self"/> was converted.</returns> /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception> /// <exception cref="ArgumentException">If <paramref name="self"/> is empty string.</exception> /// <seealso cref="byte.Parse(string)"/> /// <seealso cref="ToByte(string, out byte)"/> public static byte ToByte(this string self) { Assertion.NotEmpty(self); return(byte.Parse(self)); }
/// <summary> /// <para>Converts specified string into <see cref="bool"/> value.</para> /// </summary> /// <param name="self">String to be converted.</param> /// <returns><c>true</c> if <paramref name="self"/> is equivalent to <see cref="bool.TrueString"/>, <c>false otherwise</c>.</returns> /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception> /// <exception cref="ArgumentException">If <paramref name="self"/> is <see cref="string.Empty"/> string.</exception> /// <seealso cref="bool.Parse(string)"/> /// <seealso cref="ToBoolean(string, out bool)"/> public static bool ToBoolean(this string self) { Assertion.NotEmpty(self); return(bool.Parse(self)); }
/// <summary> /// <para>Converts specified string into <see cref="int"/> value.</para> /// </summary> /// <param name="self">String to be converted.</param> /// <returns>The <see cref="int"/> value to which string <paramref name="self"/> was converted.</returns> /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception> /// <exception cref="ArgumentException">If <paramref name="self"/> is empty string.</exception> /// <seealso cref="int.Parse(string)"/> /// <seealso cref="ToInt32(string, out int)"/> public static int ToInt32(this string self) { Assertion.NotEmpty(self); return(int.Parse(self)); }
/// <summary> /// <para>Deserializes XML string text into object of specified type.</para> /// </summary> /// <typeparam name="T">Type of object which is to be the result of deserialization process.</typeparam> /// <param name="self">XML data for deserialization.</param> /// <param name="types">Additional types to be used by <see cref="XmlSerializer"/> for deserialization purposes.</param> /// <returns>Deserialized XML contents of <paramref name="self"/> string as the object (or objects graph with a root element) of type <typeparamref name="T"/>.</returns> /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception> /// <exception cref="ArgumentException">If <paramref name="self"/> is <see cref="string.Empty"/> string.</exception> /// <seealso cref="XmlSerializer"/> public static T AsXml <T>(this string self, params Type[] types) { Assertion.NotEmpty(self); return(new StringReader(self).AsXml <T>(true, types)); }