Skip to content

Commit

Permalink
Merge pull request #515 from Orden4/fix-getcustomattributes
Browse files Browse the repository at this point in the history
Fix GetCustomAttribute(s) extension methods.
  • Loading branch information
yanghuan authored Sep 20, 2024
2 parents d605778 + 21ee097 commit 3229340
Showing 1 changed file with 69 additions and 6 deletions.
75 changes: 69 additions & 6 deletions CSharp.lua/CoreSystem.Lua/CoreSystem/Reflection/Assembly.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
})

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -971,7 +1022,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)
Expand Down Expand Up @@ -1078,17 +1129,29 @@ 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
return element:GetCustomAttribute(attributeType, inherit)
end,
IsDefined = function (element, attributeType, inherit)
if element == nil then throw(ArgumentNullException("element")) end
return element:IsDefined(attributeType, inherit)
Expand Down

0 comments on commit 3229340

Please sign in to comment.