Skip to content

Commit

Permalink
Enabling instance function methods
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiocav committed Dec 13, 2018
1 parent 00e62f4 commit 57a3ae0
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ public FunctionSignature GetEntryPointSignature(IFunctionEntryPointResolver entr
throw new InvalidOperationException($"The function type name '{typeName}' is invalid.");
}

MethodInfo method = functionType.GetMethod(methodName, BindingFlags.Static | BindingFlags.Public);
// To preserve legacy behavior, we attempt to bind to public static methods first, then fallback to instance
// methods if a match is not found.
MethodInfo method = functionType.GetMethod(methodName, BindingFlags.Static | BindingFlags.Public)
?? functionType.GetMethod(methodName, BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public);
if (method == null)
{
throw new InvalidOperationException($"The method '{methodName}' cannot be found.");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Azure.WebJobs.Script.Description;
using Xunit;

namespace Microsoft.Azure.WebJobs.Script.Tests.Description.DotNet
{
public class RawAssemblyCompilationTests
{
[Theory]
[MemberData(nameof(GetTargetNames))]
public void GetEntryPointSignature_BindsToExpectedMethod(string entryPointName, string methodName)
{
var testAssembly = typeof(TestFunction1).Assembly;
string assemblyPath = new Uri(testAssembly.CodeBase, UriKind.Absolute).LocalPath;
var compilation = new RawAssemblyCompilation(assemblyPath, entryPointName);

FunctionSignature signature = compilation.GetEntryPointSignature(new FunctionEntryPointResolver(), testAssembly);

Assert.NotNull(signature);
Assert.Equal(methodName, signature.MethodName);
}

[Fact]
public void GetEntryPointSignature_PrefersStaticMethod()
{
var testAssembly = typeof(TestFunction1).Assembly;
string assemblyPath = new Uri(testAssembly.CodeBase, UriKind.Absolute).LocalPath;
var compilation = new RawAssemblyCompilation(assemblyPath, $"{typeof(TestFunction3).FullName}.{nameof(TestFunction3.Run)}");

FunctionSignature signature = compilation.GetEntryPointSignature(new FunctionEntryPointResolver(), testAssembly);

Assert.NotNull(signature);
Assert.Equal(nameof(TestFunction3.Run), signature.MethodName);
}

public static IEnumerable<object[]> GetTargetNames()
{
return new[]
{
new[] { $"{typeof(TestFunction1).FullName}.{nameof(TestFunction1.Run)}", nameof(TestFunction1.Run) },
new[] { $"{typeof(TestFunction2).FullName}.{nameof(TestFunction2.Run)}", nameof(TestFunction2.Run) }
};
}
}

public class TestFunction1
{
public void Run() { }
}

public class TestFunction2
{
public static void Run() { }
}

public class TestFunction3
{
public void Run() { }

public static void Run(string test) { }
}
}

0 comments on commit 57a3ae0

Please sign in to comment.