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

Support generic methods in the ResourceProxyBuilder #343

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
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
30 changes: 28 additions & 2 deletions src/Moryx.Resources.Management/Resources/ResourceProxyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,33 @@ private static void DefineMethod(TypeBuilder typeBuilder, Type baseType, MethodI
var argumentTypes = parameters.Select(p => p.ParameterType).ToArray();

const MethodAttributes methodAttributes = MethodAttributes.Private | MethodAttributes.Final | MethodAttributes.Virtual | MethodAttributes.NewSlot;
var methodBuilder = typeBuilder.DefineMethod(ExplicitMemberName(method), methodAttributes, method.ReturnType, argumentTypes);
MethodBuilder methodBuilder;
if (method.IsGenericMethodDefinition)
{
var genericArguments = method.GetGenericArguments();

methodBuilder = typeBuilder.DefineMethod(ExplicitMemberName(method), methodAttributes);
var genericDefinitions = methodBuilder.DefineGenericParameters(genericArguments.Select(ga => ga.Name).ToArray());

if (method.ReturnType.IsGenericParameter)
{
methodBuilder.SetReturnType(genericDefinitions[method.ReturnType.GenericParameterPosition]);
}
else if (method.ReturnType.IsGenericType && method.GetGenericArguments()[0].IsGenericParameter)
{
var genericReturn = method.ReturnType.GetGenericTypeDefinition();
genericReturn = genericReturn.MakeGenericType(genericDefinitions[method.ReturnType.GetGenericArguments()[0].GenericParameterPosition]);
methodBuilder.SetReturnType(genericReturn);
}
else
{
methodBuilder.SetReturnType(method.ReturnType);
}
}
else
{
methodBuilder = typeBuilder.DefineMethod(ExplicitMemberName(method), methodAttributes, method.ReturnType, argumentTypes);
}

var isResourceReference = IsResourceReference(method.ReturnType);

Expand Down Expand Up @@ -265,7 +291,7 @@ private static void DefineMethod(TypeBuilder typeBuilder, Type baseType, MethodI
/// Create a unique name for each explicit member implementation
/// </summary>
private static string ExplicitMemberName(MemberInfo member) => $"{member.DeclaringType.Name}_{member.Name}";


/// <summary>
/// Determine if the return type indicates a resource reference
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Moryx.AbstractionLayer.Drivers;
using Moryx.AbstractionLayer.Drivers.InOut;
using Moryx.AbstractionLayer.Drivers.Message;
using Moryx.AbstractionLayer.Resources;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Moryx.Resources.Management.Tests
{
public interface IGenericMethodCall<T> : IResource
{
/// <summary>
/// Get channel using specialized API
/// </summary>
IList<TChannel> GenericMethod<TChannel>(string identifier);
}

public class ResourceWithGenericMethod : Driver, IGenericMethodCall<object>
{
public IList<TChannel> GenericMethod<TChannel>(string identifier)
{
throw new NotImplementedException();
}
}
}
13 changes: 13 additions & 0 deletions src/Tests/Moryx.Resources.Management.Tests/TypeControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,19 @@ public void AfterDisposeTheProxyIsDetached()
Assert.Throws<ProxyDetachedException>(() => proxy.MultiplyFoo(2));
}

[Test]
public void ProxyForGenericMethod()
{
// Arrange
var driver = new ResourceWithGenericMethod { Id = 2, Name = "Some other Resource" };

// Act
var proxy = (IGenericMethodCall<object>)_typeController.GetProxy(driver);

// Assert
Assert.IsNotNull(proxy);
}

[Test]
public void ReplaceWithProxy()
{
Expand Down