public static bool HasAttribute(MethodInfo methodInfo, Type attributeType, bool inherit = false, bool throwOnError = true) { if (Throw.IfIsNull(methodInfo, nameof(methodInfo), throwOnError)) { return(false); } if (!TypeHelpers.InheritFromClass <Attribute>(attributeType, false)) { throw new ArgumentException($"Parameter '{nameof(attributeType)}' has to be an Attribute Type!"); } return(methodInfo.GetCustomAttribute(attributeType, inherit) != null); }
public static bool TryAs(object value, Type type, out object outValue) { if (value == null) { outValue = null; return(false); } var t = value.GetType(); if (t == type) { outValue = value; return(true); } if (TypeHelpers.ImplementsInterface(t, type, false) || TypeHelpers.InheritFromClass(t, type, true)) { outValue = value; return(true); } var method = TypeHelpers.GetImplicitCastMethodTo(t, type, false); if (method != null) { outValue = method.Invoke(null, new object[] { value }); return(true); } if (TypeHelpers.IsNullableType(type)) { var underlingType = Nullable.GetUnderlyingType(type); if (TryAs(value, underlingType, out var innerValue)) { outValue = innerValue; return(true); } } outValue = null; return(false); }