Skip to content

Commit 8147509

Browse files
authored
Add support for multi parameter constructors in attributes (#177)
1 parent 7a505c1 commit 8147509

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

nanoFramework.CoreLibrary/System/Reflection/CustomAttributesHelpers.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,32 @@ public static object[] GetCustomAttributesInternal(object[] rawAttributes)
4141
// get constructor
4242
ConstructorInfo ctor = objectType.GetConstructor(new Type[] { paramType });
4343

44+
// get params
45+
object[] ctorParams = new object[] { rawAttributes[i + 1] };
46+
47+
if (ctor is null)
48+
{
49+
// give it another try, this time with an array of the parameters types
50+
// rebuild params list
51+
ctorParams = (object[])rawAttributes[i + 1];
52+
53+
Type[] paramsTypes = new Type[ctorParams.Length];
54+
55+
for (int p = 0; p < ctorParams.Length; p++)
56+
{
57+
paramsTypes[p] = ctorParams[p].GetType();
58+
}
59+
60+
ctor = objectType.GetConstructor(paramsTypes);
61+
62+
if (ctor is null)
63+
{
64+
throw new InvalidOperationException();
65+
}
66+
}
67+
4468
// invoke constructor with the parameter
45-
attributes[i / 2] = ctor.Invoke(new object[] { rawAttributes[i + 1] });
69+
attributes[i / 2] = ctor.Invoke(ctorParams);
4670
}
4771
}
4872

0 commit comments

Comments
 (0)