Skip to content

Commit dca4ed0

Browse files
authored
Merge pull request castleproject#633 from shoaibshakeel381/feature/alwaysreplicateattribute
Support replication of attributes defined in AttributesToAlwaysReplicate list
2 parents e18613e + af9a8ab commit dca4ed0

File tree

7 files changed

+85
-5
lines changed

7 files changed

+85
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
Enhancements:
66
- Two new generic method overloads `proxyGenerator.CreateClassProxy<TClass>([options], constructorArguments, interceptors)` (@backstromjoel, #636)
7+
- Allow specifying which attributes should always be copied to proxy class by adding attribute type to `AttributesToAlwaysReplicate`. Previously only non-inherited, with `Inherited=false`, attributes were copied. (@shoaibshakeel381, #633)
78

89
## 5.1.1 (2022-12-30)
910

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.IsAssignableFrom(attribute));
57+
}
58+
}
59+
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,7 @@ 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
}

0 commit comments

Comments
 (0)