Comments (3)
Thanks for reporting this @rbeurskens. Don't worry about your repro code example making use of NSubstitute for now, I expect it shouldn't be too difficult figuring out the underlying calls and deriving DynamicProxy-only repro code.
from core.
OK, it's taken me a while, but here is a corresponding DynamicProxy-only repro code example:
var generator = new ProxyGenerator();
var factory = generator.CreateClassProxy<DerivedFactory>(new QueryMethodInvocationTargetInterceptor());
factory.Create<object>(out _);
class QueryMethodInvocationTargetInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
_ = invocation.MethodInvocationTarget;
}
}
public interface IFactory // NOTE: the issue is also reproducible without this interface
{
void Create<T>(out T result);
}
public class Factory : IFactory
{
public virtual void Create<T>(out T result) => result = default;
}
public class DerivedFactory : Factory
{
public override void Create<T>(out T result) => result = default;
}
It seems that the error is caused in this method:
Core/src/Castle.Core/DynamicProxy/Generators/MethodSignatureComparer.cs
Lines 104 to 153 in dca4ed0
When given two matching generic types (or type parameters) by-ref (in our repro code: the out T result
parameters' types), this method claims that they do not match. This could possibly be fixed by introducing some new logic that erases by-ref-ness for further comparisons:
private bool EqualSignatureTypes(Type x, Type y)
{
if (x.IsGenericParameter != y.IsGenericParameter)
{
return false;
}
else if (x.IsGenericType != y.IsGenericType)
{
return false;
}
+ if (x.IsByRef != y.IsByRef)
+ {
+ return false;
+ }
+
+ if (x.IsByRef)
+ {
+ x = x.GetElementType();
+ y = y.GetElementType();
+ }
+
if (x.IsGenericParameter)
...
But I haven't checked yet whether this code breaks any existing test cases.
The suggested code addition does not appear to break any existing test cases, but we should still double-check its validity, and whether it can possibly be added in a more optimal place (from a run-time performance standpoint).
from core.
Scratch the code fix suggested above, the following would be more correct (because it doesn't skip the first two checks):
private bool EqualSignatureTypes(Type x, Type y)
{
+ if (x.IsByRef != y.IsByRef)
+ {
+ return false;
+ }
+ else if (x.IsByRef)
+ {
+ return EqualSignatureTypes(x.GetElementType(), y.GetElementType());
+ }
+
if (x.IsGenericParameter != y.IsGenericParameter)
...
from core.
Related Issues (20)
- Calls to other methods within the same class are not being intercepted HOT 4
- Not Intercepted Method is invoked on target, not on proxy. HOT 5
- Random VerificationException/TypeLoadException HOT 5
- it's very slow to create proxy HOT 2
- Log4Net not listed in dependencies HOT 4
- `InvalidProgramException` when proxying `MemoryStream` with .NET 7 HOT 15
- Proxy created with CreateClassProxyWithTarget returns false for Equals on itself HOT 3
- Why TransactionInterceptor.Intercept run at the client HOT 2
- System.InvalidOperationException: Operations that change non-concurrent collections must have exclusive access. HOT 1
- `ArgumentException`: "Cannot create an instance of `TEnum` because `Type.ContainsGenericParameters` is true" caused by `Enum` constraint on method `out` parameter HOT 4
- Proxy fail to create for create substitute that returns a derived class from abstract that satisfies interface implicitly HOT 5
- Release 5.2.0 HOT 1
- Support by-ref-like (`ref struct`) parameter types such as `Span<T>` and `ReadOnlySpan<T>` HOT 10
- Support "with" for record proxies HOT 4
- CreateInterfaceProxyWithoutTarget and BaseTypeForInterfaceProxy-member
- DictionaryAdapter Nesting Dictionaries JSON like
- Allow to intercept IAsyncEnumerable HOT 1
- ProxyGenerator leaking HOT 2
- MissingMethodException when trying to call a proxy target from different project
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from core.