Skip to content

Nested custom attribute types do not get replicated #638

@stakx

Description

@stakx

#637 incidentally revealed that custom attributes do not get replicated if their custom attribute type is a nested type. Given these two tests:

namespace Castle.DynamicProxy.Tests;

using System;

using NUnit.Framework;

[TestFixture]
public class Tests : BasePEVerifyTestCase
{
    [Test]
    public void Nested_non_inherited_attribute_is_replicated()
    {
        var proxy = generator.CreateInterfaceProxyWithoutTarget<IHasNestedNonInheritedAttribute>();
        var proxyType = proxy.GetType();
        var customAttributesOnProxyType = proxyType.GetCustomAttributes(typeof(NestedNonInheritedAttribute), inherit: false);
        CollectionAssert.IsNotEmpty(customAttributesOnProxyType);
    }

    [Test]
    public void Non_nested_non_inherited_attribute_is_replicated()
    {
        var proxy = generator.CreateInterfaceProxyWithoutTarget<IHasNonNestedNonInheritedAttribute>();
        var proxyType = proxy.GetType();
        var customAttributesOnProxyType = proxyType.GetCustomAttributes(typeof(NonNestedNonInheritedAttribute), inherit: false);
        CollectionAssert.IsNotEmpty(customAttributesOnProxyType);
    }

    [NestedNonInherited]
    public interface IHasNestedNonInheritedAttribute { }

    [NonNestedNonInherited]
    public interface IHasNonNestedNonInheritedAttribute { }

    [AttributeUsage(AttributeTargets.Interface, Inherited = false)]
    public sealed class NestedNonInheritedAttribute : Attribute { }
}

// NOTE: it is relevant to the above tests that the following type not be a nested type!

[AttributeUsage(AttributeTargets.Interface, Inherited = false)]
public sealed class NonNestedNonInheritedAttribute : Attribute { }

Only of of the tests fails, more specifically, Nested_non_inherited_attribute_is_replicated.

The problem is likely caused here:

if (attribute.IsPublic == false)

We should probably augment this test with an additional condition ... && attribute.IsNestedPublic == false.

... or was there a specific reason against replicating nested custom attribute types?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions