Skip to content

Commit 3229340

Browse files
authored
Merge pull request #515 from Orden4/fix-getcustomattributes
Fix GetCustomAttribute(s) extension methods.
2 parents d605778 + 21ee097 commit 3229340

File tree

1 file changed

+69
-6
lines changed

1 file changed

+69
-6
lines changed

CSharp.lua/CoreSystem.Lua/CoreSystem/Reflection/Assembly.lua

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,25 @@ local FieldInfo = define("System.Reflection.FieldInfo", {
266266
fillMetadataCustomAttributes(t, metadata, index, attributeType)
267267
end
268268
return arrayFromTable(t, System.Attribute)
269+
end,
270+
GetCustomAttribute = function (this, attributeType, inherit)
271+
if attributeType == nil then throw(ArgumentNullException()) end
272+
local t = {}
273+
local metadata = this.metadata
274+
if metadata then
275+
local index = 4
276+
if type(metadata[index]) == "string" then
277+
index = 5
278+
end
279+
fillMetadataCustomAttributes(t, metadata, index, attributeType)
280+
end
281+
local size = #t
282+
if size == 0 then
283+
return nil
284+
elseif size > 1 then
285+
throw(AmbiguousMatchException())
286+
end
287+
return t[1]
269288
end
270289
})
271290

@@ -400,7 +419,23 @@ local PropertyInfo = define("System.Reflection.PropertyInfo", {
400419
fillMetadataCustomAttributes(t, metadata, index, attributeType)
401420
end
402421
return arrayFromTable(t, System.Attribute)
403-
end
422+
end,
423+
GetCustomAttribute = function (this, attributeType, inherit)
424+
if attributeType == nil then throw(ArgumentNullException()) end
425+
local t = {}
426+
local metadata = this.metadata
427+
if metadata then
428+
local index = getPropertyAttributesIndex(metadata)
429+
fillMetadataCustomAttributes(t, metadata, index, attributeType)
430+
end
431+
local size = #t
432+
if size == 0 then
433+
return nil
434+
elseif size > 1 then
435+
throw(AmbiguousMatchException())
436+
end
437+
return t[1]
438+
end
404439
})
405440

406441
local function hasPublicFlag(flags)
@@ -533,7 +568,23 @@ local MethodInfo = define("System.Reflection.MethodInfo", {
533568
fillMetadataCustomAttributes(t, metadata, index, attributeType)
534569
end
535570
return arrayFromTable(t, System.Attribute)
536-
end
571+
end,
572+
GetCustomAttribute = function (this, attributeType, inherit)
573+
if attributeType == nil then throw(ArgumentNullException()) end
574+
local t = {}
575+
local metadata = this.metadata
576+
if metadata then
577+
local index = getMethodAttributesIndex(metadata)
578+
fillMetadataCustomAttributes(t, metadata, index, attributeType)
579+
end
580+
local size = #t
581+
if size == 0 then
582+
return nil
583+
elseif size > 1 then
584+
throw(AmbiguousMatchException())
585+
end
586+
return t[1]
587+
end
537588
})
538589

539590
local function buildFieldInfo(cls, name, metadata)
@@ -971,7 +1022,7 @@ end
9711022
local Attribute = System.Attribute
9721023

9731024
function Attribute.GetCustomAttribute(element, attributeType, inherit)
974-
return element:GetCustomAttributes(attributeType, inherit)
1025+
return element:GetCustomAttribute(attributeType, inherit)
9751026
end
9761027

9771028
function Attribute.GetCustomAttributes(element, attributeType, inherit)
@@ -1078,17 +1129,29 @@ define("System.Activator", {
10781129
})
10791130

10801131
define("System.Reflection.CustomAttributeExtensions", {
1081-
GetCustomAttribute = function (element, attributeType, inherit)
1132+
GetCustomAttributes = function (element, attributeType, inherit)
10821133
if element == nil then throw(ArgumentNullException("element")) end
1083-
if attributeType == nil then throw(ArgumentNullException("attributeType")) end
10841134
if type(attributeType) == "boolean" then
1085-
attributeType, inherit = inherit, attributeType
1135+
attributeType, inherit = nil, attributeType
1136+
elseif inherit == nil then
1137+
inherit = true
1138+
end
1139+
if attributeType == nil then
1140+
return element:GetCustomAttributes(inherit)
10861141
end
10871142
if getmetatable(attributeType) ~= Type then
10881143
attributeType = typeof(attributeType)
10891144
end
10901145
return element:GetCustomAttributes(attributeType, inherit)
10911146
end,
1147+
GetCustomAttribute = function (element, attributeType, inherit)
1148+
if element == nil then throw(ArgumentNullException("element")) end
1149+
if attributeType == nil then throw(ArgumentNullException("attributeType")) end
1150+
if getmetatable(attributeType) ~= Type then
1151+
attributeType = typeof(attributeType)
1152+
end
1153+
return element:GetCustomAttribute(attributeType, inherit)
1154+
end,
10921155
IsDefined = function (element, attributeType, inherit)
10931156
if element == nil then throw(ArgumentNullException("element")) end
10941157
return element:IsDefined(attributeType, inherit)

0 commit comments

Comments
 (0)