Skip to content

Commit

Permalink
Merge pull request #633 from shoaibshakeel381/feature/alwaysreplicate…
Browse files Browse the repository at this point in the history
…attribute

Support replication of attributes defined in AttributesToAlwaysReplicate list
  • Loading branch information
stakx committed Apr 9, 2023
2 parents e18613e + af9a8ab commit dca4ed0
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

Enhancements:
- Two new generic method overloads `proxyGenerator.CreateClassProxy<TClass>([options], constructorArguments, interceptors)` (@backstromjoel, #636)
- 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)

## 5.1.1 (2022-12-30)

Expand Down
6 changes: 6 additions & 0 deletions ref/Castle.Core-net462.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2768,6 +2768,12 @@ public class StandardInterceptor : Castle.DynamicProxy.IInterceptor
}
namespace Castle.DynamicProxy.Generators
{
public static class AttributesToAlwaysReplicate
{
public static void Add(System.Type attribute) { }
public static void Add<T>() { }
public static bool Contains(System.Type attribute) { }
}
public static class AttributesToAvoidReplicating
{
public static void Add(System.Type attribute) { }
Expand Down
6 changes: 6 additions & 0 deletions ref/Castle.Core-net6.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2723,6 +2723,12 @@ public class StandardInterceptor : Castle.DynamicProxy.IInterceptor
}
namespace Castle.DynamicProxy.Generators
{
public static class AttributesToAlwaysReplicate
{
public static void Add(System.Type attribute) { }
public static void Add<T>() { }
public static bool Contains(System.Type attribute) { }
}
public static class AttributesToAvoidReplicating
{
public static void Add(System.Type attribute) { }
Expand Down
6 changes: 6 additions & 0 deletions ref/Castle.Core-netstandard2.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2721,6 +2721,12 @@ public class StandardInterceptor : Castle.DynamicProxy.IInterceptor
}
namespace Castle.DynamicProxy.Generators
{
public static class AttributesToAlwaysReplicate
{
public static void Add(System.Type attribute) { }
public static void Add<T>() { }
public static bool Contains(System.Type attribute) { }
}
public static class AttributesToAvoidReplicating
{
public static void Add(System.Type attribute) { }
Expand Down
6 changes: 6 additions & 0 deletions ref/Castle.Core-netstandard2.1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2721,6 +2721,12 @@ public class StandardInterceptor : Castle.DynamicProxy.IInterceptor
}
namespace Castle.DynamicProxy.Generators
{
public static class AttributesToAlwaysReplicate
{
public static void Add(System.Type attribute) { }
public static void Add<T>() { }
public static bool Contains(System.Type attribute) { }
}
public static class AttributesToAvoidReplicating
{
public static void Add(System.Type attribute) { }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2004-2021 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.Generators
{
using System;
using System.Collections.Generic;
using System.Linq;

public static class AttributesToAlwaysReplicate
{
private static readonly object lockObject = new object();

private static IList<Type> attributes;

static AttributesToAlwaysReplicate()
{
attributes = new List<Type>()
{
typeof(ParamArrayAttribute)
};
}

public static void Add(Type attribute)
{
// note: this class is made thread-safe by replacing the backing list rather than adding to it
lock (lockObject)
{
attributes = new List<Type>(attributes) { attribute };
}
}

public static void Add<T>()
{
Add(typeof(T));
}

public static bool Contains(Type attribute)
{
return attributes.Contains(attribute);
}

internal static bool ShouldAdd(Type attribute)
{
return attributes.Any(attr => attr.IsAssignableFrom(attribute));
}
}
}
6 changes: 1 addition & 5 deletions src/Castle.Core/DynamicProxy/Internal/AttributeUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,7 @@ private static bool ShouldSkipAttributeReplication(Type attribute, bool ignoreIn
return true;
}

// Later, there might be more special cases requiring attribute replication,
// which might justify creating a `SpecialCaseAttributeThatShouldBeReplicated`
// method and an `AttributesToAlwaysReplicate` class. For the moment, `Param-
// ArrayAttribute` is the only special case, so keep it simple for now:
if (attribute == typeof(ParamArrayAttribute))
if (AttributesToAlwaysReplicate.ShouldAdd(attribute))
{
return false;
}
Expand Down

0 comments on commit dca4ed0

Please sign in to comment.