Comments (4)
I should also have pointed out that this throws the same exception:
var proxy = new Castle.DynamicProxy.ProxyGenerator().CreateInterfaceProxyWithoutTarget(typeof(ITestInterface1));
So I'm guessing this is a DynamicProxy issue -- either that or both Moq and I are calling the wrong method.
from core.
@abagonhishead, thanks for reporting, I'm going to take a look at it.
from core.
This also affects classes:
Example on dotnetfiddle
using System;
using Moq;
public class Program
{
public static void Main()
{
var mock1 = new Mock<TestClass1>();
var mock2 = new Mock<TestClass2>();
// Throws
var instance1 = mock1.Object;
// Does not throw
var instance2 = mock2.Object;
}
public class TestClass1
{
public virtual bool TestMethod<TEnum>(out TEnum? someValue)
where TEnum : Enum
{
someValue = default(TEnum?);
return false;
}
}
public class TestClass2
{
public virtual bool TestMethod<TEnum>(out TEnum? someValue)
{
someValue = default(TEnum?);
return false;
}
}
}
Stack trace:
Unhandled exception. System.ArgumentException: Cannot create an instance of TEnum because Type.ContainsGenericParameters is true.
at System.RuntimeType.CreateInstanceCheckThis()
at System.RuntimeType.ActivatorCache..ctor(RuntimeType rt)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)
at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
at System.Activator.CreateInstance(Type type)
at Castle.DynamicProxy.Generators.Emitters.OpCodeUtil.GetUnderlyingTypeOfEnum(Type enumType)
at Castle.DynamicProxy.Generators.Emitters.OpCodeUtil.EmitLoadIndirectOpCodeForType(ILGenerator gen, Type type)
at Castle.DynamicProxy.Generators.Emitters.SimpleAST.IndirectReference.LoadReference(ILGenerator gen)
at Castle.DynamicProxy.Generators.Emitters.SimpleAST.ReferencesToObjectArrayExpression.Emit(ILGenerator gen)
at Castle.DynamicProxy.Generators.Emitters.SimpleAST.NewInstanceExpression.Emit(ILGenerator gen)
at Castle.DynamicProxy.Generators.Emitters.SimpleAST.AssignStatement.Emit(ILGenerator gen)
at Castle.DynamicProxy.Generators.Emitters.CodeBuilder.Generate(ILGenerator il)
at Castle.DynamicProxy.Generators.Emitters.MethodEmitter.Generate()
at Castle.DynamicProxy.Generators.Emitters.AbstractTypeEmitter.EnsureBuildersAreInAValidState()
at Castle.DynamicProxy.Generators.Emitters.AbstractTypeEmitter.BuildType()
at Castle.DynamicProxy.Generators.BaseClassProxyGenerator.GenerateType(String name, INamingScope namingScope)
at Castle.DynamicProxy.Generators.BaseProxyGenerator.<>c__DisplayClass13_0.<GetProxyType>b__0(CacheKey cacheKey)
at Castle.Core.Internal.SynchronizedDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at Castle.DynamicProxy.Generators.BaseProxyGenerator.GetProxyType()
at Castle.DynamicProxy.DefaultProxyBuilder.CreateClassProxyType(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options)
at Castle.DynamicProxy.ProxyGenerator.CreateClassProxyType(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options)
at Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, Object[] constructorArguments, IInterceptor[] interceptors)
at Moq.CastleProxyFactory.CreateProxy(Type mockType, IInterceptor interceptor, Type[] interfaces, Object[] arguments) in /_/src/Moq/Interception/CastleProxyFactory.cs:line 110
at Moq.Mock`1.InitializeInstance() in /_/src/Moq/Mock`1.cs:line 502
at Moq.Mock`1.OnGetObject() in /_/src/Moq/Mock`1.cs:line 516
at Moq.Mock.get_Object() in /_/src/Moq/Mock.cs:line 180
at Moq.Mock`1.get_Object() in /_/src/Moq/Mock`1.cs:line 453
at Program.Main()
from core.
This is apparently caused by type checks inside OpCodeUtil.EmitLoadIndirectOpCodeForType
and OpCodeUtil.EmitStoreIndirectOpCodeForType
running in the wrong order: there is a check for type.IsEnum
which runs before type.IsGenericParameter
. If we switch the ordering of those type checks, the reported problem disappears.
from core.
Related Issues (20)
- 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`: "Could not find method overriding method" with overridden class method having generic by-ref parameter HOT 3
- 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
- .NET 9 compatibility: Signature of the body and declaration in a method implementation do not match HOT 1
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.