//******************************************************************************************************
// MemberInfoExtensions.cs - Gbtc
//
// Copyright © 2012, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://www.opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 01/30/2009 - J. Ritchie Carroll
// Generated original version of source code.
// 09/14/2009 - Stephen C. Wills
// Added new header and license agreement.
// 12/14/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
namespace GSF.Reflection
{
///
/// Defines extensions methods related to objects and derived types (e.g., ,
/// , , etc.).
///
public static class MemberInfoExtensions
{
///
/// Gets the friendly class name of the provided object, trimming generic parameters.
///
/// The object over which to get friendly class name.
/// Friendly class name of the provided member, or if is null.
public static string GetFriendlyClassName(this TMemberInfo member) where TMemberInfo : MemberInfo
{
// Compiler may get confused about which extension function to use, so we specify explicitly to avoid potential recursive call
return (object)member != null ? TypeExtensions.GetFriendlyClassName(member.DeclaringType) : string.Empty;
}
///
/// Returns a boolean flag that determines if the specified exists.
///
/// The object over which to search attributes.
/// or derived type to get from.
/// of to attempt to retrieve.
/// true if attribute exists; otherwise, false.
[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
public static bool AttributeExists(this TMemberInfo member) where TMemberInfo : MemberInfo where TAttribute : Attribute
{
TAttribute value;
return member.TryGetAttribute(out value);
}
///
/// Attempts to get the specified from a object, returning true if it does.
///
/// The object over which to search attributes.
/// The that was found, if any.
/// true if was found; otherwise false.
/// or derived type to get from.
/// of to attempt to retrieve.
///
/// If more than of the same type of attribute exists on the member, only the first one is returned.
///
public static bool TryGetAttribute(this TMemberInfo member, out TAttribute attribute) where TMemberInfo : MemberInfo where TAttribute : Attribute
{
object[] customAttributes = member.GetCustomAttributes(typeof(TAttribute), true);
if (customAttributes.Length > 0)
{
attribute = customAttributes[0] as TAttribute;
return true;
}
attribute = null;
return false;
}
///
/// Attempts to get the specified from a object, returning true if it does.
///
/// The object over which to search attributes.
/// The array of objects that were found, if any.
/// true if was found; otherwise false.
/// or derived type to get from.
/// of to attempt to retrieve.
public static bool TryGetAttributes(this TMemberInfo member, out TAttribute[] attributes) where TMemberInfo : MemberInfo where TAttribute : Attribute
{
object[] customAttributes = member.GetCustomAttributes(typeof(TAttribute), true);
if (customAttributes.Length > 0)
{
attributes = customAttributes.Cast().ToArray();
return true;
}
attributes = null;
return false;
}
///
/// Attempts to get the specified from a object, returning true if it does.
///
/// The object over which to search attributes.
/// The actual type of the to look for.
/// The that was found, if any.
/// true if was found; otherwise false.
/// or derived type to get from.
///
/// If more than of the same type of attribute exists on the member, only the first one is returned.
///
public static bool TryGetAttribute(this TMemberInfo member, Type attributeType, out Attribute attribute) where TMemberInfo : MemberInfo
{
object[] customAttributes = member.GetCustomAttributes(attributeType, true);
if (customAttributes.Length > 0)
{
attribute = customAttributes[0] as Attribute;
return true;
}
attribute = null;
return false;
}
///
/// Attempts to get the specified from a object, returning true if it does.
///
/// The object over which to search attributes.
/// The actual type of the objects to look for.
/// The array of objects that were found, if any.
/// true if was found; otherwise false.
/// or derived type to get from.
public static bool TryGetAttributes(this TMemberInfo member, Type attributeType, out Attribute[] attributes) where TMemberInfo : MemberInfo
{
object[] customAttributes = member.GetCustomAttributes(attributeType, true);
if (customAttributes.Length > 0)
{
attributes = customAttributes.Cast().ToArray();
return true;
}
attributes = null;
return false;
}
///
/// Attempts to get the specified from a object, returning true if it does.
///
/// The object over which to search attributes.
/// The name of the type of the to look for.
/// The that was found, if any.
/// true if was found; otherwise false.
/// or derived type to get from.
///
/// If more than of the same type of attribute exists on the member, only the first one is returned.
///
public static bool TryGetAttribute(this TMemberInfo member, string attributeName, out Attribute attribute) where TMemberInfo : MemberInfo
{
object[] customAttributes = member.GetCustomAttributes(true);
attribute = customAttributes.FirstOrDefault(attr => attr.GetType().FullName == attributeName) as Attribute;
return (object)attribute != null;
}
///
/// Attempts to get the specified from a object, returning true if it does.
///
/// The object over which to search attributes.
/// The name of the type of the objects to look for.
/// The array of objects that were found, if any.
/// true if was found; otherwise false.
/// or derived type to get from.
public static bool TryGetAttributes(this TMemberInfo member, string attributeName, out Attribute[] attributes) where TMemberInfo : MemberInfo
{
object[] customAttributes = member.GetCustomAttributes(true);
attributes = customAttributes.Where(attr => attr.GetType().FullName == attributeName).Cast().ToArray();
if (attributes.Length != 0)
return true;
attributes = null;
return false;
}
}
}