Skip to content

Commit 979eb75

Browse files
committed
Refactor "special case for handling arrays"
This is where the `NullReferenceException` happens. The pre-existing comment here didn't explain *why* this special case is needed at all, which only became apparent when I temporarily removed it. Let's explain the intent behind the special case better, and move it inside an `if` that triggers only when the right conditions are present (which now excludes `null` values).
1 parent 0f796e9 commit 979eb75

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/Castle.Core/DynamicProxy/Internal/AttributeUtil.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,18 @@ private static object[] GetArguments(IList<CustomAttributeTypedArgument> constru
6060
private static object ReadAttributeValue(CustomAttributeTypedArgument argument)
6161
{
6262
var value = argument.Value;
63-
if (argument.ArgumentType.IsArray == false)
63+
64+
if (argument.ArgumentType.IsArray && value is IList<CustomAttributeTypedArgument> values)
6465
{
65-
return value;
66+
// `CustomAttributeInfo` represents array values as `ReadOnlyCollection<CustomAttributeTypedArgument>`,
67+
// but `CustomAttributeBuilder` will require plain arrays, so we need a (recursive) conversion:
68+
var arguments = GetArguments(values);
69+
var array = new object[arguments.Length];
70+
arguments.CopyTo(array, 0);
71+
return array;
6672
}
67-
//special case for handling arrays in attributes
68-
var arguments = GetArguments((IList<CustomAttributeTypedArgument>)value);
69-
var array = new object[arguments.Length];
70-
arguments.CopyTo(array, 0);
71-
return array;
73+
74+
return value;
7275
}
7376

7477
private static void GetSettersAndFields(Type attributeType, IEnumerable<CustomAttributeNamedArgument> namedArguments,

0 commit comments

Comments
 (0)