Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复使用扩展方法不能获取到指定的Attibute的问题 #653

Merged
merged 1 commit into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}
}
}