Skip to content
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

Fix ArgumentException caused by Enum constraint on method out parameter #658

Merged
merged 4 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading