Skip to content

Commit

Permalink
Merge pull request #658 from stakx/bug/generic-enum-constraint
Browse files Browse the repository at this point in the history
Fix `ArgumentException` caused by `Enum` constraint on method `out` parameter
  • Loading branch information
stakx committed Aug 28, 2023
2 parents 2537a23 + 1bc7f18 commit 57e666d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@ Enhancements:

Bugfixes:
- `ArgumentException`: "Could not find method overriding method" with overridden class method having generic by-ref parameter (@stakx, #657)
- `ArgumentException`: "Cannot create an instance of `TEnum` because `Type.ContainsGenericParameters` is true" caused by `Enum` constraint on method `out` parameter (@stakx, #658)

## 5.1.1 (2022-12-30)

Expand Down
@@ -0,0 +1,23 @@
// Copyright 2004-2023 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.Tests.GenInterfaces
{
using System;

public interface IHaveGenericMethodWithEnumConstraint
{
void Method<TEnum>(out TEnum result) where TEnum : Enum;
}
}
Expand Up @@ -33,6 +33,12 @@ public void Generic_type_generic_method_with_struct_base_Method_base_Type_constr
CreateProxyFor<IConstraint_Method1IsTypeStructAndMethod2<object>>();
}

[Test]
public void Non_generic_type_generic_method_with_by_ref_parameter_type_constrained_to_Enum()
{
CreateProxyFor<IHaveGenericMethodWithEnumConstraint>();
}


private T CreateProxyFor<T>(params IInterceptor[] interceptors) where T : class
{
Expand Down
38 changes: 20 additions & 18 deletions src/Castle.Core/DynamicProxy/Generators/Emitters/OpCodeUtil.cs
Expand Up @@ -27,12 +27,6 @@ internal abstract class OpCodeUtil
/// </summary>
public static void EmitLoadIndirectOpCodeForType(ILGenerator gen, Type type)
{
if (type.IsEnum)
{
EmitLoadIndirectOpCodeForType(gen, GetUnderlyingTypeOfEnum(type));
return;
}

if (type.IsByRef)
{
throw new NotSupportedException("Cannot load ByRef values");
Expand All @@ -48,13 +42,20 @@ public static void EmitLoadIndirectOpCodeForType(ILGenerator gen, Type type)

gen.Emit(opCode);
}
else if (type.IsValueType)
else if (type.IsGenericParameter)
{
gen.Emit(OpCodes.Ldobj, type);
}
else if (type.IsGenericParameter)
else if (type.IsValueType)
{
gen.Emit(OpCodes.Ldobj, type);
if (type.IsEnum)
{
EmitLoadIndirectOpCodeForType(gen, GetUnderlyingTypeOfEnum(type));
}
else
{
gen.Emit(OpCodes.Ldobj, type);
}
}
else
{
Expand Down Expand Up @@ -107,12 +108,6 @@ public static void EmitLoadOpCodeForDefaultValueOfType(ILGenerator gen, Type typ
/// </summary>
public static void EmitStoreIndirectOpCodeForType(ILGenerator gen, Type type)
{
if (type.IsEnum)
{
EmitStoreIndirectOpCodeForType(gen, GetUnderlyingTypeOfEnum(type));
return;
}

if (type.IsByRef)
{
throw new NotSupportedException("Cannot store ByRef values");
Expand All @@ -128,13 +123,20 @@ public static void EmitStoreIndirectOpCodeForType(ILGenerator gen, Type type)

gen.Emit(opCode);
}
else if (type.IsValueType)
else if (type.IsGenericParameter)
{
gen.Emit(OpCodes.Stobj, type);
}
else if (type.IsGenericParameter)
else if (type.IsValueType)
{
gen.Emit(OpCodes.Stobj, type);
if (type.IsEnum)
{
EmitStoreIndirectOpCodeForType(gen, GetUnderlyingTypeOfEnum(type));
}
else
{
gen.Emit(OpCodes.Stobj, type);
}
}
else
{
Expand Down

0 comments on commit 57e666d

Please sign in to comment.