-
Notifications
You must be signed in to change notification settings - Fork 263
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
Can't create substitute for delegate from some namespaces 🤯 #631
Comments
@zvirja @alexandrnikitin I'm trying to reproduce this with DynamicProxy alone (taking NSubstitute out of the equation). I'm probably doing something inanely silly, but I can't get this to work: using System;
using Xunit;
using NSubstitute;
namespace Na { public delegate void DoNothing(); }
namespace Ns { public delegate void DoNothing(); }
namespace Nt { public delegate void DoNothing(); }
namespace NSubWorkshop
{
public class Issue631_NamespaceDelegate
{
private class TestInterceptor : Castle.DynamicProxy.IInterceptor
{
public void Intercept(Castle.DynamicProxy.IInvocation invocation) { invocation.Proceed(); }
}
public static object Proxy<T>() where T : class {
var proxyType = typeof(T);
var proxyGenerationOptions = new Castle.DynamicProxy.ProxyGenerationOptions();
var g = new Castle.DynamicProxy.ProxyGenerator();
return g.CreateClassProxy(
proxyType,
new Type[0],
proxyGenerationOptions,
new object[0],
new Castle.DynamicProxy.IInterceptor[] { new TestInterceptor() });
}
[Fact] public void Na() { Substitute.For<Na.DoNothing>(); }
[Fact] public void Ns() { Substitute.For<Ns.DoNothing>(); }
[Fact] public void Nt() { Substitute.For<Nt.DoNothing>(); }
[Fact] public void DynamicProxyAction() { Proxy<Action>(); }
[Fact] public void DynamicProxyNa() { Proxy<Na.DoNothing>(); }
[Fact] public void DynamicProxyNt() { Proxy<Nt.DoNothing>(); }
}
} The
If you have time could you please have a quick look and point out what I'm doing wrong? 🤦 |
@dtchepak It's because you always need a touch of magic when you work with delegates 😊 proxyGenerationOptions.AddDelegateTypeMixin(typeof(T));
var obj = g.CreateClassProxy(
classToProxy: typeof(object),
additionalInterfacesToProxy: Array.Empty<Type>(),
proxyGenerationOptions,
constructorArguments: Array.Empty<object>(),
new TestInterceptor());
return (T)obj.GetType().GetMethod("Invoke").CreateDelegate(typeof(T), obj); We don't pass delegate as a type, otherwise Castle will try to generate a proxy for a subtype of our delegate type and that will fail. @kislovs This issue seems to be fixed in the most recent version of <PackageReference Include="Castle.Core" Version="4.4.1" /> Let me know if the issue is reproducible after that. If it helps we'll just bump the version of the |
Anyway, I'm not sure how it's related to castleproject/Core#469... Because even such test fails without Test.csproj
Tests.cs
Error:
I just don't understand error |
@kislovs It's indeed some weird stuff 😆 To troubleshoot why it happens we should debug I will create a PR soon to pin |
I'm not so interesting into such mystery. I just found some weird thing during usual test writing and report it 🙂 Nice that it was fixed without long researching. |
Closes nsubstitute#631. Misc changes: - ignore .ionide files - helper to run test using only dotnet core (without .NET framework).
Closes #631. Misc changes: - ignore .ionide files - helper to run test using only dotnet core (without .NET framework).
Describe the bug
In short, I can't use
Substitute.For<{delegate}>();
if this delegate from some namespaces. I discovered that namespaces started withN[a-s]
led to exception, others work fine (I didn't tested any namespaces though). Also I found that it doesn't work for version 4.1.0 and later (I think it relates to #537), 4.0.0 works fine. Bug is permanently reproducable with 4.1.0+ version on different machines and operations systems. Though it works fine withFunc<...>
stuff.To Reproduce
Test.csproj
Tests.cs
It should results like that:
Full exception stacktrace:
Expected behaviour
Substitute should be created without any problems.
Environment:
Additional context
I have no idea why it doen't works... It sounds mind-blowing if something doesn't work because of namespace 🤯
The text was updated successfully, but these errors were encountered: