ReflectionExtensions.GetCustomAttributes C# (CSharp) Method

GetCustomAttributes() public static method

public static GetCustomAttributes ( this type, Type attributeType, bool inherit ) : object[]
type this
attributeType Type
inherit bool
return object[]
    public static object[] GetCustomAttributes(this Type type, Type attributeType, bool inherit)
    {
        return type.GetTypeInfo().GetCustomAttributes(attributeType, inherit).ToArray();
    }
}

Same methods

ReflectionExtensions::GetCustomAttributes ( this type, bool inherit ) : object[]

Usage Example

Ejemplo n.º 1
0
        public EntityDataLog Logs(T entity)
        {
            EntityDataLog entityDataLog = new EntityDataLog();
            Type          type          = typeof(T);

            entityDataLog.FullName = type.FullName;
            DisplayNameAttribute[] customAttributes1 = ReflectionExtensions.GetCustomAttributes <DisplayNameAttribute>(type);
            DisplayAttribute[]     customAttributes2 = ReflectionExtensions.GetCustomAttributes <DisplayAttribute>(type);
            if (customAttributes1 != null && customAttributes1.Length > 0)
            {
                entityDataLog.DisplayName = customAttributes1[0].DisplayName;
            }
            else if (customAttributes2 != null && customAttributes2.Length > 0)
            {
                entityDataLog.DisplayName = customAttributes2[0].Name;
            }
            // ISSUE: reference to a compiler-generated method
            DbEntityEntry <T> dbEntityEntry = this.DataContext.Entry <T>(entity);

            if (dbEntityEntry.State == EntityState.Modified)
            {
                foreach (string propertyName in dbEntityEntry.OriginalValues.PropertyNames)
                {
                    EntityPropertyData entityPropertyData = new EntityPropertyData();
                    entityPropertyData.Name = propertyName;
                    DisplayNameAttribute displayAttribute1 = ClassExtensions <T> .GetDisplayAttribute <DisplayNameAttribute>(propertyName);

                    DisplayAttribute displayAttribute2 = ClassExtensions <T> .GetDisplayAttribute(propertyName);

                    if (!string.IsNullOrEmpty(displayAttribute1.DisplayName))
                    {
                        entityPropertyData.DisplayName = displayAttribute1.DisplayName;
                    }
                    else if (!string.IsNullOrEmpty(displayAttribute2.Name))
                    {
                        entityPropertyData.DisplayName = displayAttribute2.Name;
                    }
                    entityPropertyData.OldValue = dbEntityEntry.OriginalValues[propertyName];
                    entityPropertyData.NewValue = dbEntityEntry.CurrentValues[propertyName];
                    entityDataLog.ChangedProperties.Add(entityPropertyData);
                }
            }
            else
            {
                foreach (PropertyInfo property in typeof(T).GetProperties())
                {
                    EntityPropertyData entityPropertyData = new EntityPropertyData();
                    entityPropertyData.Name = property.Name;
                    DisplayNameAttribute displayAttribute1 = ClassExtensions <T> .GetDisplayAttribute <DisplayNameAttribute>(entityPropertyData.Name);

                    DisplayAttribute displayAttribute2 = ClassExtensions <T> .GetDisplayAttribute(entityPropertyData.Name);

                    if (!string.IsNullOrEmpty(displayAttribute1.DisplayName))
                    {
                        entityPropertyData.DisplayName = displayAttribute1.DisplayName;
                    }
                    else if (!string.IsNullOrEmpty(displayAttribute2.Name))
                    {
                        entityPropertyData.DisplayName = displayAttribute2.Name;
                    }
                    entityPropertyData.OldValue = property.GetValue((object)entity, (object[])null);
                    entityDataLog.ChangedProperties.Add(entityPropertyData);
                }
            }
            return(entityDataLog);
        }