Skip to content

Commit

Permalink
Merge pull request #653 from Endures/Fix-Get-Custom-Attributes
Browse files Browse the repository at this point in the history
修复使用扩展方法不能获取到指定的Attibute的问题
  • Loading branch information
liiir1985 authored Feb 10, 2022
2 parents c490b06 + bd52e1b commit 3ee7017
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
2 changes: 1 addition & 1 deletion ILRuntime/Reflection/ILRuntimeFieldInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit)
if (customAttributes == null)
InitializeCustomAttribute();

List<object> res = new List<object>();
List<Attribute> res = new List<Attribute>();
for (int i = 0; i < customAttributes.Length; i++)
{
if (attributeTypes[i].Equals(attributeType) || attributeTypes[i].IsSubclassOf(attributeType))
Expand Down
2 changes: 1 addition & 1 deletion ILRuntime/Reflection/ILRuntimeMethodInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit)
{
if (customAttributes == null)
InitializeCustomAttribute();
List<object> res = new List<object>();
List<Attribute> res = new List<Attribute>();
for (int i = 0; i < customAttributes.Length; i++)
{
if (attributeTypes[i].Equals(attributeType))
Expand Down
8 changes: 4 additions & 4 deletions ILRuntime/Reflection/ILRuntimeType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,9 @@ public override object[] GetCustomAttributes(bool inherit)
InitializeCustomAttribute();
if (inherit && BaseType != null)
{
List<object> result = new List<object>();
List<Attribute> result = new List<Attribute>();
result.AddRange(customAttributes);
result.AddRange(BaseType.GetCustomAttributes(inherit));
result.AddRange(BaseType.GetCustomAttributes(inherit) as Attribute[]);
return result.ToArray();
}
return customAttributes;
Expand All @@ -231,15 +231,15 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit)
{
if (customAttributes == null)
InitializeCustomAttribute();
List<object> res = new List<object>();
List<Attribute> res = new List<Attribute>();
for (int i = 0; i < customAttributes.Length; i++)
{
if (attributeTypes[i].Equals((object)attributeType))
res.Add(customAttributes[i]);
}
if (inherit && BaseType != null)
{
res.AddRange(BaseType.GetCustomAttributes(attributeType, inherit));
res.AddRange(BaseType.GetCustomAttributes(attributeType, inherit) as Attribute[]);
}
return res.ToArray();
}
Expand Down
45 changes: 44 additions & 1 deletion TestCases/TestLL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ class XmlFieldMapAttribute : XmlAttribute

}


[XmlFieldMapAttribute]
public class TestLL
{
[XmlFieldMapAttribute]
public Dictionary<int, int> tempMap = new Dictionary<int, int>();
[XmlFieldMapAttribute]
public Dictionary<int, int> tempMapProperty => tempMap;
[XmlFieldMapAttribute]
public Dictionary<int, int> GetTempMap() => tempMap;

public static string TestIsAssignableFrom()
{
Expand Down Expand Up @@ -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");
}
}
}

0 comments on commit 3ee7017

Please sign in to comment.