@@ -266,6 +266,25 @@ local FieldInfo = define("System.Reflection.FieldInfo", {
266
266
fillMetadataCustomAttributes (t , metadata , index , attributeType )
267
267
end
268
268
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 ]
269
288
end
270
289
})
271
290
@@ -400,7 +419,23 @@ local PropertyInfo = define("System.Reflection.PropertyInfo", {
400
419
fillMetadataCustomAttributes (t , metadata , index , attributeType )
401
420
end
402
421
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
404
439
})
405
440
406
441
local function hasPublicFlag (flags )
@@ -533,7 +568,23 @@ local MethodInfo = define("System.Reflection.MethodInfo", {
533
568
fillMetadataCustomAttributes (t , metadata , index , attributeType )
534
569
end
535
570
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
537
588
})
538
589
539
590
local function buildFieldInfo (cls , name , metadata )
971
1022
local Attribute = System .Attribute
972
1023
973
1024
function Attribute .GetCustomAttribute (element , attributeType , inherit )
974
- return element :GetCustomAttributes (attributeType , inherit )
1025
+ return element :GetCustomAttribute (attributeType , inherit )
975
1026
end
976
1027
977
1028
function Attribute .GetCustomAttributes (element , attributeType , inherit )
@@ -1078,17 +1129,29 @@ define("System.Activator", {
1078
1129
})
1079
1130
1080
1131
define (" System.Reflection.CustomAttributeExtensions" , {
1081
- GetCustomAttribute = function (element , attributeType , inherit )
1132
+ GetCustomAttributes = function (element , attributeType , inherit )
1082
1133
if element == nil then throw (ArgumentNullException (" element" )) end
1083
- if attributeType == nil then throw (ArgumentNullException (" attributeType" )) end
1084
1134
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 )
1086
1141
end
1087
1142
if getmetatable (attributeType ) ~= Type then
1088
1143
attributeType = typeof (attributeType )
1089
1144
end
1090
1145
return element :GetCustomAttributes (attributeType , inherit )
1091
1146
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 ,
1092
1155
IsDefined = function (element , attributeType , inherit )
1093
1156
if element == nil then throw (ArgumentNullException (" element" )) end
1094
1157
return element :IsDefined (attributeType , inherit )
0 commit comments