Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested custom attribute types do not get replicated #638

Closed
stakx opened this issue Dec 12, 2022 · 0 comments · Fixed by #639
Closed

Nested custom attribute types do not get replicated #638

stakx opened this issue Dec 12, 2022 · 0 comments · Fixed by #639
Labels
Milestone

Comments

@stakx
Copy link
Member

stakx commented Dec 12, 2022

#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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant