/// <summary> /// Prints the contents of <paramref name="this"/> to the given <see cref="TextWriter"/>. /// </summary> /// <param name="this">The <see cref="Exception"/> to print.</param> /// <param name="writer">The <see cref="TextWriter"/> to write exception information to.</param> /// <exception cref="ArgumentNullException"> /// Thrown if either <paramref name="this"/> or <paramref name="writer"/> is <c>null</c>. /// </exception> public static void Print(this Exception @this, TextWriter writer) { @this.CheckParameterForNull("@this"); writer.CheckParameterForNull("writer"); writer.WriteLine("Type Name: {0}", @this.GetType().FullName); #if !SILVERLIGHT writer.WriteLine("\tSource: {0}", @this.Source); writer.WriteLine("\tTargetSite: {0}", ExceptionExtensions.FormatMethod(@this.TargetSite)); #endif writer.WriteLine("\tMessage: {0}", @this.Message); #if !SILVERLIGHT writer.WriteLine("\tHelpLink: {0}", @this.HelpLink); #endif @this.PrintCustomProperties(writer); @this.PrintStackTrace(writer); @this.PrintData(writer); if (@this.InnerException != null) { writer.WriteLine(); @this.InnerException.Print(writer); } }
/// <summary> /// Checks to see if the given provider has an attribute of a specific type. /// </summary> /// <param name="this">The <see cref="ICustomAttributeProvider"/> to check.</param> /// <param name="attributeType">The type of the custom attribute.</param> /// <param name="inherit">When <c>true</c>, look up the hierarchy chain for the inherited custom attribute.</param> /// <returns>Returns <c>true</c> if the provider has the attribute, otherwise <c>false</c>.</returns> /// <exception cref="ArgumentNullException"> /// Thrown if either <paramref name="this"/> or <paramref name="attributeType"/> is <c>null</c>. /// </exception> public static bool HasAttribute(this ICustomAttributeProvider @this, Type attributeType, bool inherit) { @this.CheckParameterForNull("@this"); attributeType.CheckParameterForNull("attribute"); return (from attribute in @this.GetCustomAttributes(attributeType, inherit) where attributeType.IsAssignableFrom(attribute.GetType()) select attribute).Any(); }
/// <summary> /// Gets the root element type for a given <see cref="Type"/>. /// </summary> /// <param name="this">The <see cref="Type"/> to check.</param> /// <returns>Returns the root element <see cref="Type"/>.</returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="this"/> is <c>null</c>.</exception> /// <remarks> /// Certain types (e.g. arrays and byrefs) have an element type. For example, /// a byref array of integers ("int[]&") has an element type of "int[]", /// which in turn has an element type of "int". This method makes it easier /// to find the root element type for any given <see cref="Type"/>. /// </remarks> public static Type GetRootElementType(this Type @this) { @this.CheckParameterForNull("@this"); var type = @this; while(type.HasElementType) { type = type.GetElementType(); } return type; }
/// <summary> /// Gets the types of the parameters for the given method. /// </summary> /// <param name="this">The <see cref="MethodBase"/> to get parameter types for.</param> /// <returns>An array of <see cref="Type"/>s that map directly to the parameters (in terms of location) in the given method.</returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="this"/> is <c>null</c>.</exception> /// <remarks> /// If <paramref name="this"/> is a <see cref="MethodBuilder"/> or <see cref="ConstructorBuilder"/>, /// no parameter type information can be determined, so the returned array will be empty. /// </remarks> public static Type[] GetParameterTypes(this MethodBase @this) { @this.CheckParameterForNull("@this"); var parameterTypes = Type.EmptyTypes; if (!(@this is MethodBuilder) && !(@this is ConstructorBuilder)) { parameterTypes = (from target in @this.GetParameters() select target.ParameterType).ToArray(); } return parameterTypes; }
/// <summary> /// Checks to see if the object has a specific attribute. /// </summary> /// <param name="this">The object to check.</param> /// <param name="attributeType">The type of the custom attribute.</param> /// <param name="inherit">When <c>true</c>, look up the hierarchy chain for the inherited custom attribute.</param> /// <returns>Returns <c>true</c> if the object has the attribute, otherwise <c>false</c>.</returns> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="this"/> is <c>null</c>. /// </exception> public static bool HasAttribute(this object @this, Type attributeType, bool inherit) { @this.CheckParameterForNull("@this"); return ICustomAttributeProviderExtensions.HasAttribute( @this.GetType(), attributeType, inherit); }