Skip to content

Commit 375d614

Browse files
Merge pull request #12 from servicetitan/fix-constraints
Fix constraints
2 parents ee77c78 + 956019c commit 375d614

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

LazyProxy.Tests/LazyProxyBuilderTests.cs

+18-6
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ public class LazyProxyBuilderTests
99
{
1010
public interface IBaseArgument { }
1111

12-
public abstract class BaseArgument : IBaseArgument { }
12+
public interface IOtherBaseArgument { }
13+
14+
public abstract class BaseArgument : IBaseArgument, IOtherBaseArgument { }
1315

1416
public abstract class BaseArgument2 { }
1517

16-
public struct TestArgument : IBaseArgument { }
18+
public struct TestArgument : IBaseArgument, IOtherBaseArgument { }
1719

1820
// ReSharper disable once MemberCanBePrivate.Global
1921
public class TestArgument2 : BaseArgument { }
@@ -44,6 +46,10 @@ public interface ITestService : IParentTestService
4446
string MethodWithDefaultValue(string arg = "arg");
4547
string MethodWithOutValue(out string arg);
4648
string MethodWithRefValue(ref TestArgument arg);
49+
50+
void GenericMethod<T>()
51+
where T : IBaseArgument, IOtherBaseArgument { }
52+
4753
string GenericMethod<T1, T2, T3>(string arg)
4854
where T1 : class, IBaseArgument, new()
4955
where T2 : struct
@@ -52,7 +58,7 @@ string GenericMethod<T1, T2, T3>(string arg)
5258

5359
// ReSharper disable once MemberCanBePrivate.Global
5460
public interface IGenericTestService<T, in TIn, out TOut>
55-
where T : class, IBaseArgument, new()
61+
where T : class, IBaseArgument, IOtherBaseArgument, new()
5662
where TIn : struct
5763
where TOut : BaseArgument2, IBaseArgument
5864
{
@@ -76,7 +82,7 @@ public void ProxyMustImplementInterface()
7682
public void ExceptionMustBeThrownForBuildingProxyByClass()
7783
{
7884
Assert.Throws<NotSupportedException>(
79-
() => LazyProxyBuilder.GetType<AbstractTestService>());
85+
LazyProxyBuilder.GetType<AbstractTestService>);
8086
}
8187

8288
[Fact]
@@ -402,10 +408,16 @@ public void GenericInterfaceWithDifferentTypeParametersMustBeCreatedWithoutExcep
402408
LazyProxyBuilder.GetType(typeof(IGenericTestService<,,>));
403409
LazyProxyBuilder.GetType<IGenericTestService<TestArgument2, TestArgument, TestArgument4>>();
404410
LazyProxyBuilder.GetType<IGenericTestService<TestArgument3, TestArgument, TestArgument4>>();
405-
406411
});
407412

408413
Assert.Null(exception);
409414
}
415+
416+
[Fact]
417+
public void GenericMethodWithMultipleInterfaceConstraintsMustBeProxied()
418+
{
419+
var proxy = LazyProxyBuilder.CreateInstance(Mock.Of<ITestService>);
420+
proxy.GenericMethod<TestArgument>();
421+
}
410422
}
411-
}
423+
}

LazyProxy/LazyProxyBuilder.cs

+12-1
Original file line numberDiff line numberDiff line change
@@ -312,18 +312,29 @@ private static void AddGenericParameters(
312312
definedGenericParameter.SetGenericParameterAttributes(genericParameterAttributes);
313313

314314
var genericParameterConstraints = genericParameter.GetGenericParameterConstraints();
315+
if (!genericParameterConstraints.Any())
316+
{
317+
return;
318+
}
319+
320+
var interfaceConstraints = new List<Type>(genericParameterConstraints.Length);
315321

316322
foreach (var constraint in genericParameterConstraints)
317323
{
318324
if (constraint.IsInterface)
319325
{
320-
definedGenericParameter.SetInterfaceConstraints(constraint);
326+
interfaceConstraints.Add(constraint);
321327
}
322328
else
323329
{
324330
definedGenericParameter.SetBaseTypeConstraint(constraint);
325331
}
326332
}
333+
334+
if (interfaceConstraints.Any())
335+
{
336+
definedGenericParameter.SetInterfaceConstraints(interfaceConstraints.ToArray());
337+
}
327338
}
328339
}
329340
}

0 commit comments

Comments
 (0)