Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit a04c448

Browse files
committed
Change Attribute extensions to assume inherits=true
1 parent 0b90cb4 commit a04c448

File tree

1 file changed

+41
-39
lines changed

1 file changed

+41
-39
lines changed

src/ServiceStack.Text/ReflectionExtensions.cs

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -552,11 +552,11 @@ public static PropertyInfo[] GetSerializableProperties(this Type type)
552552
? publicReadableProperties.Where(attr =>
553553
attr.IsDefined(typeof(DataMemberAttribute), false)).ToArray()
554554
: publicReadableProperties.Where(attr =>
555-
attr.AllAttributes(false).Any(x => x.GetType().Name == DataMember)).ToArray();
555+
attr.AllAttributes().Any(x => x.GetType().Name == DataMember)).ToArray();
556556
}
557557

558558
// else return those properties that are not decorated with IgnoreDataMember
559-
return publicReadableProperties.Where(prop => prop.AllAttributes(false).All(attr => attr.GetType().Name != IgnoreDataMember)).ToArray();
559+
return publicReadableProperties.Where(prop => prop.AllAttributes().All(attr => attr.GetType().Name != IgnoreDataMember)).ToArray();
560560
}
561561

562562
public static FieldInfo[] GetSerializableFields(this Type type)
@@ -568,7 +568,7 @@ public static FieldInfo[] GetSerializableFields(this Type type)
568568
var publicFields = type.GetPublicFields();
569569

570570
// else return those properties that are not decorated with IgnoreDataMember
571-
return publicFields.Where(prop => prop.AllAttributes(false).All(attr => attr.GetType().Name != IgnoreDataMember)).ToArray();
571+
return publicFields.Where(prop => prop.AllAttributes().All(attr => attr.GetType().Name != IgnoreDataMember)).ToArray();
572572
}
573573

574574
#if !SILVERLIGHT && !MONOTOUCH
@@ -589,7 +589,7 @@ public static DataContractAttribute GetDataContract(this Type type)
589589

590590
public static DataMemberAttribute GetDataMember(this PropertyInfo pi)
591591
{
592-
var dataMember = pi.AllAttributes(typeof(DataMemberAttribute), false)
592+
var dataMember = pi.AllAttributes(typeof(DataMemberAttribute))
593593
.FirstOrDefault() as DataMemberAttribute;
594594

595595
#if !SILVERLIGHT && !MONOTOUCH && !XBOX
@@ -601,7 +601,7 @@ public static DataMemberAttribute GetDataMember(this PropertyInfo pi)
601601

602602
public static DataMemberAttribute GetDataMember(this FieldInfo pi)
603603
{
604-
var dataMember = pi.AllAttributes(typeof(DataMemberAttribute), false)
604+
var dataMember = pi.AllAttributes(typeof(DataMemberAttribute))
605605
.FirstOrDefault() as DataMemberAttribute;
606606

607607
#if !SILVERLIGHT && !MONOTOUCH && !XBOX
@@ -858,33 +858,33 @@ public static MemberInfo[] GetAllPublicMembers(this Type type)
858858
#endif
859859
}
860860

861-
public static bool HasAttribute<T>(this Type type, bool inherit = true)
861+
public static bool HasAttribute<T>(this Type type)
862862
{
863-
return type.AllAttributes(inherit).Any(x => x.GetType() == typeof(T));
863+
return type.AllAttributes().Any(x => x.GetType() == typeof(T));
864864
}
865865

866-
public static bool HasAttributeNamed(this Type type, string name, bool inherit = true)
866+
public static bool HasAttributeNamed(this Type type, string name)
867867
{
868868
var normalizedAttr = name.Replace("Attribute", "").ToLower();
869-
return type.AllAttributes(inherit).Any(x => x.GetType().Name.Replace("Attribute", "").ToLower() == normalizedAttr);
869+
return type.AllAttributes().Any(x => x.GetType().Name.Replace("Attribute", "").ToLower() == normalizedAttr);
870870
}
871871

872-
public static bool HasAttributeNamed(this PropertyInfo pi, string name, bool inherit = true)
872+
public static bool HasAttributeNamed(this PropertyInfo pi, string name)
873873
{
874874
var normalizedAttr = name.Replace("Attribute", "").ToLower();
875-
return pi.AllAttributes(inherit).Any(x => x.GetType().Name.Replace("Attribute", "").ToLower() == normalizedAttr);
875+
return pi.AllAttributes().Any(x => x.GetType().Name.Replace("Attribute", "").ToLower() == normalizedAttr);
876876
}
877877

878-
public static bool HasAttributeNamed(this FieldInfo fi, string name, bool inherit = true)
878+
public static bool HasAttributeNamed(this FieldInfo fi, string name)
879879
{
880880
var normalizedAttr = name.Replace("Attribute", "").ToLower();
881-
return fi.AllAttributes(inherit).Any(x => x.GetType().Name.Replace("Attribute", "").ToLower() == normalizedAttr);
881+
return fi.AllAttributes().Any(x => x.GetType().Name.Replace("Attribute", "").ToLower() == normalizedAttr);
882882
}
883883

884-
public static bool HasAttributeNamed(this MemberInfo mi, string name, bool inherit = true)
884+
public static bool HasAttributeNamed(this MemberInfo mi, string name)
885885
{
886886
var normalizedAttr = name.Replace("Attribute", "").ToLower();
887-
return mi.AllAttributes(inherit).Any(x => x.GetType().Name.Replace("Attribute", "").ToLower() == normalizedAttr);
887+
return mi.AllAttributes().Any(x => x.GetType().Name.Replace("Attribute", "").ToLower() == normalizedAttr);
888888
}
889889

890890
const string DataContract = "DataContractAttribute";
@@ -929,75 +929,77 @@ public static PropertyInfo[] AllProperties(this Type type)
929929
#endif
930930
}
931931

932-
public static object[] AllAttributes(this PropertyInfo propertyInfo, bool inherit = true)
932+
public static object[] AllAttributes(this PropertyInfo propertyInfo)
933933
{
934934
#if NETFX_CORE
935-
return propertyInfo.GetCustomAttributes(inherit).ToArray();
935+
return propertyInfo.GetCustomAttributes(true).ToArray();
936936
#else
937-
return propertyInfo.GetCustomAttributes(inherit);
937+
return propertyInfo.GetCustomAttributes(true);
938938
#endif
939939
}
940940

941-
public static object[] AllAttributes(this PropertyInfo propertyInfo, Type attrType, bool inherit = true)
941+
public static object[] AllAttributes(this PropertyInfo propertyInfo, Type attrType)
942942
{
943943
#if NETFX_CORE
944-
return propertyInfo.GetCustomAttributes(inherit).Where(x => x.GetType() == attrType).ToArray();
944+
return propertyInfo.GetCustomAttributes(true).Where(x => x.GetType() == attrType).ToArray();
945945
#else
946-
return propertyInfo.GetCustomAttributes(attrType, inherit);
946+
return propertyInfo.GetCustomAttributes(attrType, true);
947947
#endif
948948
}
949949

950-
public static object[] AllAttributes(this FieldInfo fieldInfo, bool inherit = true)
950+
public static object[] AllAttributes(this FieldInfo fieldInfo)
951951
{
952952
#if NETFX_CORE
953-
return fieldInfo.GetCustomAttributes(inherit).ToArray();
953+
return fieldInfo.GetCustomAttributes(true).ToArray();
954954
#else
955-
return fieldInfo.GetCustomAttributes(inherit);
955+
return fieldInfo.GetCustomAttributes(true);
956956
#endif
957957
}
958958

959-
public static object[] AllAttributes(this MemberInfo memberInfo, bool inherit = true)
959+
public static object[] AllAttributes(this MemberInfo memberInfo)
960960
{
961961
#if NETFX_CORE
962-
return memberInfo.GetCustomAttributes(inherit).ToArray();
962+
return memberInfo.GetCustomAttributes(true).ToArray();
963963
#else
964-
return memberInfo.GetCustomAttributes(inherit);
964+
return memberInfo.GetCustomAttributes(true);
965965
#endif
966966
}
967967

968-
public static object[] AllAttributes(this MemberInfo meberInfo, Type attrType, bool inherit = true)
968+
public static object[] AllAttributes(this MemberInfo meberInfo, Type attrType)
969969
{
970970
#if NETFX_CORE
971-
return meberInfo.GetCustomAttributes(inherit).Where(x => x.GetType() == attrType).ToArray();
971+
return meberInfo.GetCustomAttributes(true).Where(x => x.GetType() == attrType).ToArray();
972972
#else
973-
return meberInfo.GetCustomAttributes(attrType, inherit);
973+
return meberInfo.GetCustomAttributes(attrType, true);
974974
#endif
975975
}
976976

977-
public static object[] AllAttributes(this FieldInfo fieldInfo, Type attrType, bool inherit = true)
977+
public static object[] AllAttributes(this FieldInfo fieldInfo, Type attrType)
978978
{
979979
#if NETFX_CORE
980-
return fieldInfo.GetCustomAttributes(inherit).Where(x => x.GetType() == attrType).ToArray();
980+
return fieldInfo.GetCustomAttributes(true).Where(x => x.GetType() == attrType).ToArray();
981981
#else
982-
return fieldInfo.GetCustomAttributes(attrType, inherit);
982+
return fieldInfo.GetCustomAttributes(attrType, true);
983983
#endif
984984
}
985985

986-
public static object[] AllAttributes(this Type type, bool inherit = true)
986+
public static object[] AllAttributes(this Type type)
987987
{
988988
#if NETFX_CORE
989-
return type.GetTypeInfo().GetCustomAttributes(inherit).ToArray();
989+
return type.GetTypeInfo().GetCustomAttributes(true).ToArray();
990+
#elif SILVERLIGHT
991+
return type.GetCustomAttributes(true);
990992
#else
991-
return type.GetCustomAttributes(inherit);
993+
return TypeDescriptor.GetAttributes(type).Cast<object>().ToArray();
992994
#endif
993995
}
994996

995-
public static object[] AllAttributes(this Type type, Type attrType, bool inherit = true)
997+
public static object[] AllAttributes(this Type type, Type attrType)
996998
{
997999
#if NETFX_CORE
998-
return type.GetTypeInfo().GetCustomAttributes(inherit).Where(x => x.GetType() == attrType).ToArray();
1000+
return type.GetTypeInfo().GetCustomAttributes(true).Where(x => x.GetType() == attrType).ToArray();
9991001
#elif SILVERLIGHT
1000-
return type.GetCustomAttributes(attrType, inherit);
1002+
return type.GetCustomAttributes(attrType, true);
10011003
#else
10021004
return TypeDescriptor.GetAttributes(type).OfType<Attribute>().ToArray();
10031005
#endif

0 commit comments

Comments
 (0)