Skip to content

Commit f9139b8

Browse files
author
Shoaib Shakeel
committed
Support replication of attributss defined in AttributesToAlwaysReplicate
- This is complementary to AttributesToAvoidReplicating - This will allow consumers to specify attributes which should always be copied to generated proxy. It is especially useful where we do not own target interfeces and/or cannot override attributes, e.g. attributes used by various frameworks. This was mentioned in castleproject#593 - Similar effect could by achieved in Castle.Core Version 4.4.1 by simply extending various generator classes but in 5.0 all those classes were marked internal, without this fix we now have to override half the library, So this fix offers an alternative path. - Fixes castleproject#607
1 parent e18613e commit f9139b8

File tree

6 files changed

+85
-6
lines changed

6 files changed

+85
-6
lines changed

ref/Castle.Core-net462.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2768,6 +2768,12 @@ protected virtual void PreProceed(Castle.DynamicProxy.IInvocation invocation) {
27682768
}
27692769
namespace Castle.DynamicProxy.Generators
27702770
{
2771+
public static class AttributesToAlwaysReplicate
2772+
{
2773+
public static void Add(System.Type attribute) { }
2774+
public static void Add<T>() { }
2775+
public static bool Contains(System.Type attribute) { }
2776+
}
27712777
public static class AttributesToAvoidReplicating
27722778
{
27732779
public static void Add(System.Type attribute) { }

ref/Castle.Core-net6.0.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,6 +2723,12 @@ protected virtual void PreProceed(Castle.DynamicProxy.IInvocation invocation) {
27232723
}
27242724
namespace Castle.DynamicProxy.Generators
27252725
{
2726+
public static class AttributesToAlwaysReplicate
2727+
{
2728+
public static void Add(System.Type attribute) { }
2729+
public static void Add<T>() { }
2730+
public static bool Contains(System.Type attribute) { }
2731+
}
27262732
public static class AttributesToAvoidReplicating
27272733
{
27282734
public static void Add(System.Type attribute) { }

ref/Castle.Core-netstandard2.0.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2721,6 +2721,12 @@ protected virtual void PreProceed(Castle.DynamicProxy.IInvocation invocation) {
27212721
}
27222722
namespace Castle.DynamicProxy.Generators
27232723
{
2724+
public static class AttributesToAlwaysReplicate
2725+
{
2726+
public static void Add(System.Type attribute) { }
2727+
public static void Add<T>() { }
2728+
public static bool Contains(System.Type attribute) { }
2729+
}
27242730
public static class AttributesToAvoidReplicating
27252731
{
27262732
public static void Add(System.Type attribute) { }

ref/Castle.Core-netstandard2.1.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2721,6 +2721,12 @@ protected virtual void PreProceed(Castle.DynamicProxy.IInvocation invocation) {
27212721
}
27222722
namespace Castle.DynamicProxy.Generators
27232723
{
2724+
public static class AttributesToAlwaysReplicate
2725+
{
2726+
public static void Add(System.Type attribute) { }
2727+
public static void Add<T>() { }
2728+
public static bool Contains(System.Type attribute) { }
2729+
}
27242730
public static class AttributesToAvoidReplicating
27252731
{
27262732
public static void Add(System.Type attribute) { }
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
namespace Castle.DynamicProxy.Generators
16+
{
17+
using System;
18+
using System.Collections.Generic;
19+
using System.Linq;
20+
21+
public static class AttributesToAlwaysReplicate
22+
{
23+
private static readonly object lockObject = new object();
24+
25+
private static IList<Type> attributes;
26+
27+
static AttributesToAlwaysReplicate()
28+
{
29+
attributes = new List<Type>()
30+
{
31+
typeof(ParamArrayAttribute)
32+
};
33+
}
34+
35+
public static void Add(Type attribute)
36+
{
37+
// note: this class is made thread-safe by replacing the backing list rather than adding to it
38+
lock (lockObject)
39+
{
40+
attributes = new List<Type>(attributes) { attribute };
41+
}
42+
}
43+
44+
public static void Add<T>()
45+
{
46+
Add(typeof(T));
47+
}
48+
49+
public static bool Contains(Type attribute)
50+
{
51+
return attributes.Contains(attribute);
52+
}
53+
54+
internal static bool ShouldAdd(Type attribute)
55+
{
56+
return attributes.Any(attr => attr == attribute);
57+
}
58+
}
59+
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,11 @@ private static bool ShouldSkipAttributeReplication(Type attribute, bool ignoreIn
181181
return true;
182182
}
183183

184-
// Later, there might be more special cases requiring attribute replication,
185-
// which might justify creating a `SpecialCaseAttributeThatShouldBeReplicated`
186-
// method and an `AttributesToAlwaysReplicate` class. For the moment, `Param-
187-
// ArrayAttribute` is the only special case, so keep it simple for now:
188-
if (attribute == typeof(ParamArrayAttribute))
184+
if (AttributesToAlwaysReplicate.ShouldAdd(attribute))
189185
{
190186
return false;
191187
}
192-
188+
193189
if (!ignoreInheritance)
194190
{
195191
var attrs = attribute.GetCustomAttributes<AttributeUsageAttribute>(true).ToArray();

0 commit comments

Comments
 (0)