Skip to content

Support implicit operators in method argument matching#977

Merged
StefH merged 7 commits intomasterfrom
copilot/add-support-for-implicit-operators
Mar 29, 2026
Merged

Support implicit operators in method argument matching#977
StefH merged 7 commits intomasterfrom
copilot/add-support-for-implicit-operators

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 29, 2026

When a method parameter type differs from the passed argument type but an implicit operator exists between them, the parser failed with "No applicable method exists" — unlike plain C# which resolves such calls automatically.

Root cause

ExpressionPromoter.Promote (used by MethodFinder.IsApplicable to validate argument/parameter compatibility) had no knowledge of op_Implicit operators, so methods requiring implicit conversion were never considered applicable.

Changes

  • ExpressionPromoter.cs — After TypeHelper.IsCompatibleWith fails, scan op_Implicit methods on both the source and target types. If a matching operator is found (returnType → type), return Expression.Convert(sourceExpression, type) to wire up the conversion.

  • DynamicExpressionParserTests.cs — Added MyStructWithImplicitOperator (struct with string ↔ MyStruct implicit operators), MyMethodsWithImplicitOperatorSupport (a [DynamicLinqType] static class), and a test covering the scenario from the issue.

Example

[DynamicLinqType]
public static class MyMethods
{
    public static string UsesMyStruct(MyStruct myStruct) => myStruct.Value;
}

public readonly struct MyStruct
{
    public static implicit operator MyStruct(string value) => new(value);
    public static implicit operator string(MyStruct s) => s.Value;
    public string Value { get; }
}

// Previously threw: "No applicable method 'UsesMyStruct' exists in type 'MyMethods'"
var parser = new ExpressionParser([], "MyMethods.UsesMyStruct(\"Foo\")", [], ParsingConfig.Default);
var result = Expression.Lambda<Func<string>>(parser.Parse(typeof(string))).Compile()(); // "Foo"

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI linked an issue Mar 29, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Add support for implicit operators Support implicit operators in method argument matching Mar 29, 2026
Copilot AI requested a review from StefH March 29, 2026 06:34
return sourceExpression;
}

// Check for implicit conversion operators (op_Implicit) from returnType to type.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in d0cc881. Added TryFindImplicitConversionOperator(Type sourceType, Type targetType, out MethodInfo? implicitOperator) to TypeHelper.cs, updated ExpressionPromoter.Promote and ExpressionParser.HasImplicitConversion to use it.

@StefH StefH marked this pull request as ready for review March 29, 2026 06:56
@StefH StefH added the feature label Mar 29, 2026
@StefH StefH merged commit b939626 into master Mar 29, 2026
4 checks passed
@StefH StefH deleted the copilot/add-support-for-implicit-operators branch March 29, 2026 07:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

Support for implicit operators

2 participants