From 3408d167ba4f62e509b5c65f17eebc871d1ed7e3 Mon Sep 17 00:00:00 2001 From: tompeeters_macomi Date: Wed, 18 Sep 2024 19:42:41 +0200 Subject: [PATCH 1/2] Fix GetCustomAttribute(s) extension methods. --- .../CoreSystem/Reflection/Assembly.lua | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/CSharp.lua/CoreSystem.Lua/CoreSystem/Reflection/Assembly.lua b/CSharp.lua/CoreSystem.Lua/CoreSystem/Reflection/Assembly.lua index 01ebed56..ea922670 100644 --- a/CSharp.lua/CoreSystem.Lua/CoreSystem/Reflection/Assembly.lua +++ b/CSharp.lua/CoreSystem.Lua/CoreSystem/Reflection/Assembly.lua @@ -971,7 +971,7 @@ end local Attribute = System.Attribute function Attribute.GetCustomAttribute(element, attributeType, inherit) - return element:GetCustomAttributes(attributeType, inherit) + return element:GetCustomAttribute(attributeType, inherit) end function Attribute.GetCustomAttributes(element, attributeType, inherit) @@ -1078,17 +1078,36 @@ define("System.Activator", { }) define("System.Reflection.CustomAttributeExtensions", { - GetCustomAttribute = function (element, attributeType, inherit) + GetCustomAttributes = function (element, attributeType, inherit) if element == nil then throw(ArgumentNullException("element")) end - if attributeType == nil then throw(ArgumentNullException("attributeType")) end if type(attributeType) == "boolean" then - attributeType, inherit = inherit, attributeType + attributeType, inherit = nil, attributeType + elseif inherit == nil then + inherit = true + end + if attributeType == nil then + return element:GetCustomAttributes(inherit) end if getmetatable(attributeType) ~= Type then attributeType = typeof(attributeType) end return element:GetCustomAttributes(attributeType, inherit) end, + GetCustomAttribute = function (element, attributeType, inherit) + if element == nil then throw(ArgumentNullException("element")) end + if attributeType == nil then throw(ArgumentNullException("attributeType")) end + if getmetatable(attributeType) ~= Type then + attributeType = typeof(attributeType) + end + local attributes = element:GetCustomAttributes(attributeType, inherit) + local size = #attributes + if size == 0 then + return nil + elseif size > 1 then + throw(AmbiguousMatchException()) + end + return attributes:get(0) + end, IsDefined = function (element, attributeType, inherit) if element == nil then throw(ArgumentNullException("element")) end return element:IsDefined(attributeType, inherit) From 21ee097da6b9e72b07d27eee42f71c2922cc8243 Mon Sep 17 00:00:00 2001 From: tompeeters_macomi Date: Wed, 18 Sep 2024 20:21:30 +0200 Subject: [PATCH 2/2] Properly fix Attribute.GetCustomAttribute. --- .../CoreSystem/Reflection/Assembly.lua | 64 ++++++++++++++++--- 1 file changed, 54 insertions(+), 10 deletions(-) diff --git a/CSharp.lua/CoreSystem.Lua/CoreSystem/Reflection/Assembly.lua b/CSharp.lua/CoreSystem.Lua/CoreSystem/Reflection/Assembly.lua index ea922670..7810ce02 100644 --- a/CSharp.lua/CoreSystem.Lua/CoreSystem/Reflection/Assembly.lua +++ b/CSharp.lua/CoreSystem.Lua/CoreSystem/Reflection/Assembly.lua @@ -266,6 +266,25 @@ local FieldInfo = define("System.Reflection.FieldInfo", { fillMetadataCustomAttributes(t, metadata, index, attributeType) end return arrayFromTable(t, System.Attribute) + end, + GetCustomAttribute = function (this, attributeType, inherit) + if attributeType == nil then throw(ArgumentNullException()) end + local t = {} + local metadata = this.metadata + if metadata then + local index = 4 + if type(metadata[index]) == "string" then + index = 5 + end + fillMetadataCustomAttributes(t, metadata, index, attributeType) + end + local size = #t + if size == 0 then + return nil + elseif size > 1 then + throw(AmbiguousMatchException()) + end + return t[1] end }) @@ -400,7 +419,23 @@ local PropertyInfo = define("System.Reflection.PropertyInfo", { fillMetadataCustomAttributes(t, metadata, index, attributeType) end return arrayFromTable(t, System.Attribute) - end + end, + GetCustomAttribute = function (this, attributeType, inherit) + if attributeType == nil then throw(ArgumentNullException()) end + local t = {} + local metadata = this.metadata + if metadata then + local index = getPropertyAttributesIndex(metadata) + fillMetadataCustomAttributes(t, metadata, index, attributeType) + end + local size = #t + if size == 0 then + return nil + elseif size > 1 then + throw(AmbiguousMatchException()) + end + return t[1] + end }) local function hasPublicFlag(flags) @@ -533,7 +568,23 @@ local MethodInfo = define("System.Reflection.MethodInfo", { fillMetadataCustomAttributes(t, metadata, index, attributeType) end return arrayFromTable(t, System.Attribute) - end + end, + GetCustomAttribute = function (this, attributeType, inherit) + if attributeType == nil then throw(ArgumentNullException()) end + local t = {} + local metadata = this.metadata + if metadata then + local index = getMethodAttributesIndex(metadata) + fillMetadataCustomAttributes(t, metadata, index, attributeType) + end + local size = #t + if size == 0 then + return nil + elseif size > 1 then + throw(AmbiguousMatchException()) + end + return t[1] + end }) local function buildFieldInfo(cls, name, metadata) @@ -1099,14 +1150,7 @@ define("System.Reflection.CustomAttributeExtensions", { if getmetatable(attributeType) ~= Type then attributeType = typeof(attributeType) end - local attributes = element:GetCustomAttributes(attributeType, inherit) - local size = #attributes - if size == 0 then - return nil - elseif size > 1 then - throw(AmbiguousMatchException()) - end - return attributes:get(0) + return element:GetCustomAttribute(attributeType, inherit) end, IsDefined = function (element, attributeType, inherit) if element == nil then throw(ArgumentNullException("element")) end