Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

Bugfixes:
- Failure proxying type that has a non-inheritable custom attribute type applied where `null` argument is given for array parameter (@stakx, #637)
- Nested custom attribute types do not get replicated (@stakx, #638)

## 5.1.0 (2022-08-02)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace Castle.DynamicProxy.Tests
{
using System;
using System.IO;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -178,5 +179,22 @@ public void Can_proxy_type_with_non_inheritable_attribute_depending_on_array_of_
.Cast<NonInheritableWithArray2Attribute>().Single();
CollectionAssert.AreEqual(attribute.Values, new[] { "1", "2", "3" });
}

[TestCase(typeof(HasAttributeWithNullAsPositionalArgument))]
[TestCase(typeof(HasAttributeWithEmptyArrayAsPositionalArgument))]
[TestCase(typeof(HasAttributeWithNullInArrayAsPositionalArgument))]
public void Can_proxy_type_with_attribute_with_positional_array_parameter(Type classType)
{
_ = generator.CreateClassProxy(classType);
}

[NonInheritableAttributeWithPositionalArrayParameter(null)]
public class HasAttributeWithNullAsPositionalArgument{ }

[NonInheritableAttributeWithPositionalArrayParameter(new object[0])]
public class HasAttributeWithEmptyArrayAsPositionalArgument { }

[NonInheritableAttributeWithPositionalArrayParameter(new object[1] { null })]
public class HasAttributeWithNullInArrayAsPositionalArgument { }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2004-2022 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Castle.DynamicProxy.Tests.Classes
{
using System;

#if FEATURE_SERIALIZATION
[Serializable]
#endif
[AttributeUsage(AttributeTargets.All, Inherited = false)]
public class NonInheritableAttributeWithPositionalArrayParameterAttribute : Attribute
{
public NonInheritableAttributeWithPositionalArrayParameterAttribute(object[] arg)
{
}
}
}
38 changes: 12 additions & 26 deletions src/Castle.Core/DynamicProxy/Internal/AttributeUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ public static CustomAttributeInfo CreateInfo(CustomAttributeData attribute)
{
Debug.Assert(attribute != null, "attribute != null");

// .NET Core does not provide CustomAttributeData.Constructor, so we'll implement it
// by finding a constructor ourselves
Type[] constructorArgTypes;
object[] constructorArgs;
GetArguments(attribute.ConstructorArguments, out constructorArgTypes, out constructorArgs);
var constructor = attribute.AttributeType.GetConstructor(constructorArgTypes);
object[] constructorArgs = GetArguments(attribute.ConstructorArguments);

PropertyInfo[] properties;
object[] propertyValues;
Expand All @@ -43,26 +38,14 @@ public static CustomAttributeInfo CreateInfo(CustomAttributeData attribute)
attribute.AttributeType,
attribute.NamedArguments, out properties, out propertyValues, out fields, out fieldValues);

return new CustomAttributeInfo(constructor,
return new CustomAttributeInfo(attribute.Constructor,
constructorArgs,
properties,
propertyValues,
fields,
fieldValues);
}

private static void GetArguments(IList<CustomAttributeTypedArgument> constructorArguments,
out Type[] constructorArgTypes, out object[] constructorArgs)
{
constructorArgTypes = new Type[constructorArguments.Count];
constructorArgs = new object[constructorArguments.Count];
for (var i = 0; i < constructorArguments.Count; i++)
{
constructorArgTypes[i] = constructorArguments[i].ArgumentType;
constructorArgs[i] = ReadAttributeValue(constructorArguments[i]);
}
}

private static object[] GetArguments(IList<CustomAttributeTypedArgument> constructorArguments)
{
var arguments = new object[constructorArguments.Count];
Expand All @@ -77,15 +60,18 @@ private static object[] GetArguments(IList<CustomAttributeTypedArgument> constru
private static object ReadAttributeValue(CustomAttributeTypedArgument argument)
{
var value = argument.Value;
if (argument.ArgumentType.IsArray == false)

if (argument.ArgumentType.IsArray && value is IList<CustomAttributeTypedArgument> values)
{
return value;
// `CustomAttributeInfo` represents array values as `ReadOnlyCollection<CustomAttributeTypedArgument>`,
// but `CustomAttributeBuilder` will require plain arrays, so we need a (recursive) conversion:
var arguments = GetArguments(values);
var array = new object[arguments.Length];
arguments.CopyTo(array, 0);
return array;
}
//special case for handling arrays in attributes
var arguments = GetArguments((IList<CustomAttributeTypedArgument>)value);
var array = new object[arguments.Length];
arguments.CopyTo(array, 0);
return array;

return value;
}

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