diff --git a/ILRuntime/Reflection/ILRuntimeFieldInfo.cs b/ILRuntime/Reflection/ILRuntimeFieldInfo.cs index 15a0f004..021c5529 100644 --- a/ILRuntime/Reflection/ILRuntimeFieldInfo.cs +++ b/ILRuntime/Reflection/ILRuntimeFieldInfo.cs @@ -154,7 +154,7 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit) if (customAttributes == null) InitializeCustomAttribute(); - List res = new List(); + List res = new List(); for (int i = 0; i < customAttributes.Length; i++) { if (attributeTypes[i].Equals(attributeType) || attributeTypes[i].IsSubclassOf(attributeType)) diff --git a/ILRuntime/Reflection/ILRuntimeMethodInfo.cs b/ILRuntime/Reflection/ILRuntimeMethodInfo.cs index ed2218bc..d81f8fc2 100644 --- a/ILRuntime/Reflection/ILRuntimeMethodInfo.cs +++ b/ILRuntime/Reflection/ILRuntimeMethodInfo.cs @@ -132,7 +132,7 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit) { if (customAttributes == null) InitializeCustomAttribute(); - List res = new List(); + List res = new List(); for (int i = 0; i < customAttributes.Length; i++) { if (attributeTypes[i].Equals(attributeType)) diff --git a/ILRuntime/Reflection/ILRuntimeType.cs b/ILRuntime/Reflection/ILRuntimeType.cs index 5e0bd7dc..62f0bc8a 100644 --- a/ILRuntime/Reflection/ILRuntimeType.cs +++ b/ILRuntime/Reflection/ILRuntimeType.cs @@ -219,9 +219,9 @@ public override object[] GetCustomAttributes(bool inherit) InitializeCustomAttribute(); if (inherit && BaseType != null) { - List result = new List(); + List result = new List(); result.AddRange(customAttributes); - result.AddRange(BaseType.GetCustomAttributes(inherit)); + result.AddRange(BaseType.GetCustomAttributes(inherit) as Attribute[]); return result.ToArray(); } return customAttributes; @@ -231,7 +231,7 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit) { if (customAttributes == null) InitializeCustomAttribute(); - List res = new List(); + List res = new List(); for (int i = 0; i < customAttributes.Length; i++) { if (attributeTypes[i].Equals((object)attributeType)) @@ -239,7 +239,7 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit) } if (inherit && BaseType != null) { - res.AddRange(BaseType.GetCustomAttributes(attributeType, inherit)); + res.AddRange(BaseType.GetCustomAttributes(attributeType, inherit) as Attribute[]); } return res.ToArray(); } diff --git a/TestCases/TestLL.cs b/TestCases/TestLL.cs index 166c0bfe..6d2d53f4 100644 --- a/TestCases/TestLL.cs +++ b/TestCases/TestLL.cs @@ -22,11 +22,15 @@ class XmlFieldMapAttribute : XmlAttribute } - + [XmlFieldMapAttribute] public class TestLL { [XmlFieldMapAttribute] public Dictionary tempMap = new Dictionary(); + [XmlFieldMapAttribute] + public Dictionary tempMapProperty => tempMap; + [XmlFieldMapAttribute] + public Dictionary GetTempMap() => tempMap; public static string TestIsAssignableFrom() { @@ -59,5 +63,44 @@ public static string TestILRuntimeTypeEquals() return "Test finish!"; } + + public static void TestFieldGetCustomAttribte() + { + FieldInfo fieldInfo = typeof(TestLL).GetField("tempMap"); + + Console.WriteLine($"test GetCustomAttribute using System.Reflection.CustomAttributeExtensions.GetCustomAttribute"); + var attribute = fieldInfo.GetCustomAttribute(typeof(XmlFieldMapAttribute), true); + Console.WriteLine($"TestLL.tempMap has {(attribute == null ? "NO " : "")}XmlFieldMapAttribute"); + + Console.WriteLine($"test GetCustomAttribute using System.Attribute.GetCustomAttribute"); + attribute = Attribute.GetCustomAttribute(fieldInfo, typeof(XmlFieldMapAttribute), true); + Console.WriteLine($"TestLL.tempMap has {(attribute == null ? "NO " : "")}XmlFieldMapAttribute"); + } + + public static void TestTypeGetCustomAttribte() + { + var type = typeof(TestLL); + + Console.WriteLine($"test GetCustomAttribute using System.Reflection.CustomAttributeExtensions.GetCustomAttribute"); + var attribute = type.GetCustomAttribute(typeof(XmlFieldMapAttribute), true); + Console.WriteLine($"TestLL has {(attribute == null ? "NO " : "")}XmlFieldMapAttribute"); + + Console.WriteLine($"test GetCustomAttribute using System.Attribute.GetCustomAttribute"); + attribute = Attribute.GetCustomAttribute(type, typeof(XmlFieldMapAttribute), true); + Console.WriteLine($"TestLL has {(attribute == null ? "NO " : "")}XmlFieldMapAttribute"); + } + + public static void TestMethodGetCustomAttribte() + { + var methodInfo = typeof(TestLL).GetMethod("GetTempMap"); + + Console.WriteLine($"test GetCustomAttribute using System.Reflection.CustomAttributeExtensions.GetCustomAttribute"); + var attribute = methodInfo.GetCustomAttribute(typeof(XmlFieldMapAttribute), true); + Console.WriteLine($"TestLL.GetTempMap has {(attribute == null ? "NO " : "")}XmlFieldMapAttribute"); + + Console.WriteLine($"test GetCustomAttribute using System.Attribute.GetCustomAttribute"); + attribute = Attribute.GetCustomAttribute(methodInfo, typeof(XmlFieldMapAttribute), true); + Console.WriteLine($"TestLL.GetTempMap has {(attribute == null ? "NO " : "")}XmlFieldMapAttribute"); + } } } \ No newline at end of file