Skip to content

Commit

Permalink
Add support for multi parameter constructors in attributes (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
josesimoes authored Mar 14, 2022
1 parent 7a505c1 commit 8147509
Showing 1 changed file with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,32 @@ public static object[] GetCustomAttributesInternal(object[] rawAttributes)
// get constructor
ConstructorInfo ctor = objectType.GetConstructor(new Type[] { paramType });

// get params
object[] ctorParams = new object[] { rawAttributes[i + 1] };

if (ctor is null)
{
// give it another try, this time with an array of the parameters types
// rebuild params list
ctorParams = (object[])rawAttributes[i + 1];

Type[] paramsTypes = new Type[ctorParams.Length];

for (int p = 0; p < ctorParams.Length; p++)
{
paramsTypes[p] = ctorParams[p].GetType();
}

ctor = objectType.GetConstructor(paramsTypes);

if (ctor is null)
{
throw new InvalidOperationException();
}
}

// invoke constructor with the parameter
attributes[i / 2] = ctor.Invoke(new object[] { rawAttributes[i + 1] });
attributes[i / 2] = ctor.Invoke(ctorParams);
}
}

Expand Down

0 comments on commit 8147509

Please sign in to comment.