-
Notifications
You must be signed in to change notification settings - Fork 225
Improve out arguments assignability, for both validation and expression compilation (STUD-76892) #376
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
Merged
aoltean16
merged 1 commit into
develop
from
fix/validate_and_compile_safe_cast_arguments
Oct 13, 2025
Merged
Improve out arguments assignability, for both validation and expression compilation (STUD-76892) #376
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
src/Test/TestCases.Workflows/TestXamls/ImproveAssignabilityOutArgumentActivity.xaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Activity x:Class="TestWorkflow" mc:Ignorable="sap sap2010" | ||
xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:sap="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation" | ||
xmlns:sap2010="http://schemas.microsoft.com/netfx/2010/xaml/activities/presentation" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:local="clr-namespace:TestCases.Workflows.WF4Samples;assembly=TestCases.Workflows" | ||
xmlns:scg="clr-namespace:System.Collections.Generic;assembly=System.Private.CoreLib" | ||
xmlns:sco="clr-namespace:System.Collections.ObjectModel;assembly=System.Private.CoreLib" | ||
xmlns:s="clr-namespace:System;assembly=System.Private.CoreLib"> | ||
<x:Members> | ||
<x:Property Name="myEnumerable" Type="OutArgument(scg:IEnumerable(x:String))" /> | ||
<x:Property Name="dateTimeOutArgument" Type="OutArgument(s:DateTime)" /> | ||
<x:Property Name="nullableDateTimeOutArgument" Type="OutArgument(s:Nullable(s:DateTime))" /> | ||
</x:Members> | ||
<sap2010:ExpressionActivityEditor.ExpressionActivityEditor>C#</sap2010:ExpressionActivityEditor.ExpressionActivityEditor> | ||
<Sequence DisplayName="RootSequence"> | ||
<Sequence.Variables> | ||
<Variable x:TypeArguments="x:String" Name="big" /> | ||
</Sequence.Variables> | ||
|
||
<!-- Use your custom activity --> | ||
<local:ImproveAssignabilityOutArgumentActivity> | ||
|
||
<local:ImproveAssignabilityOutArgumentActivity.Result> | ||
<OutArgument x:TypeArguments="scg:List(x:String)"> | ||
<CSharpReference x:TypeArguments="scg:List(x:String)"> | ||
myEnumerable | ||
</CSharpReference> | ||
</OutArgument> | ||
</local:ImproveAssignabilityOutArgumentActivity.Result> | ||
|
||
<local:ImproveAssignabilityOutArgumentActivity.DateTimeOutArgument> | ||
<OutArgument x:TypeArguments="s:DateTime"> | ||
<CSharpReference x:TypeArguments="s:DateTime"> | ||
dateTimeOutArgument | ||
</CSharpReference> | ||
</OutArgument> | ||
</local:ImproveAssignabilityOutArgumentActivity.DateTimeOutArgument> | ||
|
||
<local:ImproveAssignabilityOutArgumentActivity.NullableDateTimeOutArgument> | ||
<OutArgument x:TypeArguments="s:Nullable(s:DateTime)"> | ||
<CSharpReference x:TypeArguments="s:Nullable(s:DateTime)"> | ||
nullableDateTimeOutArgument | ||
</CSharpReference> | ||
</OutArgument> | ||
</local:ImproveAssignabilityOutArgumentActivity.NullableDateTimeOutArgument> | ||
|
||
</local:ImproveAssignabilityOutArgumentActivity> | ||
|
||
<Assign sap2010:WorkflowViewState.IdRef="Assign_1"> | ||
<Assign.To> | ||
<OutArgument x:TypeArguments="x:String"> | ||
<CSharpReference x:TypeArguments="x:String" sap2010:WorkflowViewState.IdRef="CSharpReference`1_1">big</CSharpReference> | ||
</OutArgument> | ||
</Assign.To> | ||
<Assign.Value> | ||
<InArgument x:TypeArguments="x:String"> | ||
<CSharpValue x:TypeArguments="x:String" sap2010:WorkflowViewState.IdRef="CSharpValue`1_13">new string('A', 100) ;</CSharpValue> | ||
</InArgument> | ||
</Assign.Value> | ||
</Assign> | ||
</Sequence> | ||
</Activity> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/Test/TestCases.Workflows/WF4Samples/ImproveAssignabilityOutArgumentActivity.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Activities; | ||
using System.Collections.Generic; | ||
|
||
namespace TestCases.Workflows.WF4Samples; | ||
|
||
public class ImproveAssignabilityOutArgumentActivity : NativeActivity | ||
{ | ||
// OutArgument of type List<string> | ||
public OutArgument<List<string>> Result { get; set; } | ||
|
||
// Additional OutArguments | ||
public OutArgument<DateTime> DateTimeOutArgument { get; set; } | ||
public OutArgument<DateTime?> NullableDateTimeOutArgument { get; set; } | ||
|
||
public ImproveAssignabilityOutArgumentActivity() : base() | ||
{ | ||
} | ||
|
||
protected override void CacheMetadata(NativeActivityMetadata metadata) | ||
{ | ||
// Result | ||
var resultArg = new RuntimeArgument( | ||
"Result", | ||
typeof(List<string>), | ||
ArgumentDirection.Out); | ||
metadata.Bind(this.Result, resultArg); | ||
metadata.AddArgument(resultArg); | ||
|
||
// DateTimeOutArgument | ||
var dateTimeArg = new RuntimeArgument( | ||
"DateTimeOutArgument", | ||
typeof(DateTime), | ||
ArgumentDirection.Out); | ||
metadata.Bind(this.DateTimeOutArgument, dateTimeArg); | ||
metadata.AddArgument(dateTimeArg); | ||
|
||
// NullableDateTimeOutArgument | ||
var nullableDateTimeArg = new RuntimeArgument( | ||
"NullableDateTimeOutArgument", | ||
typeof(DateTime?), | ||
ArgumentDirection.Out); | ||
metadata.Bind(this.NullableDateTimeOutArgument, nullableDateTimeArg); | ||
metadata.AddArgument(nullableDateTimeArg); | ||
} | ||
|
||
protected override void Execute(NativeActivityContext context) | ||
{ | ||
var result = new List<string> | ||
{ | ||
"aaa", | ||
"bbb", | ||
"ccc" | ||
}; | ||
|
||
Result.Set(context, result); | ||
DateTimeOutArgument.Set(context, DateTime.Now); | ||
NullableDateTimeOutArgument.Set(context, DateTime.Now); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Test/TestCases.Workflows/WF4Samples/ImproveAssignabilityOutArgumentTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using Shouldly; | ||
using System; | ||
using System.Activities; | ||
using System.Activities.XamlIntegration; | ||
using System.Collections.Generic; | ||
using Xunit; | ||
|
||
namespace TestCases.Workflows.WF4Samples; | ||
|
||
public class ImproveAssignabilityOutArgumentTests : ExpressionsBaseCommon | ||
{ | ||
protected override bool CompileExpressions => true; | ||
|
||
[Fact] | ||
public void CompileImproveAssignabilityOutArgumentActivity() | ||
{ | ||
Activity activity = null; | ||
|
||
// Assert that ActivityXamlServices.Load does not throw | ||
Should.NotThrow(() => | ||
{ | ||
activity = ActivityXamlServices.Load(TestHelper.GetXamlStream(TestXamls.ImproveAssignabilityOutArgumentActivity), | ||
new ActivityXamlServicesSettings | ||
{ | ||
CompileExpressions = true | ||
}); | ||
}); | ||
|
||
var invoker = new WorkflowInvoker(activity); | ||
var result = invoker.Invoke(); | ||
result["myEnumerable"].ShouldBe(new List<string> { "aaa", "bbb", "ccc" }); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading; | ||
namespace System.Activities; | ||
|
||
internal static class CSharpValidatorCommon | ||
{ | ||
private static int crt = 0; | ||
|
||
// This is used in case the expression does not properly close (e.g. missing quotes, or multiline comment not closed) | ||
private const string _expressionEnder = "// */ // \""; | ||
|
||
private const string _valueValidationTemplate = "public static System.Linq.Expressions.Expression<System.Func<{0}>> CreateExpression{1}()//activityId:{4}\n => ({2}) => {3}; {5}"; | ||
private const string _delegateValueValidationTemplate = "{0}\npublic static System.Linq.Expressions.Expression<{1}<{2}>> CreateExpression{3}()//activityId:{6}\n => ({4}) => {5}; {7}"; | ||
private const string _referenceValidationTemplate = "public static {0} IsLocation{1}()//activityId:{5}\n => ({2}) => {3} = default({4}); {6}"; | ||
|
||
internal static string CreateReferenceCode(string types, string returnType, string names, string code, string activityId, int index) | ||
{ | ||
var actionDefinition = !string.IsNullOrWhiteSpace(types) | ||
? $"System.Action<{string.Join(CompilerHelper.Comma, types)}>" | ||
: "System.Action"; | ||
return string.Format(_referenceValidationTemplate, actionDefinition, index, names, code, returnType, activityId, _expressionEnder); | ||
} | ||
|
||
internal static string CreateValueCode(IEnumerable<string> types, string names, string code, string activityId, int index) | ||
{ | ||
var serializedArgumentTypes = string.Join(CompilerHelper.Comma, types); | ||
if (types.Count() <= 16) // .net defines Func<TResult>...Func<T1,...T16,TResult) | ||
return string.Format(_valueValidationTemplate, serializedArgumentTypes, index, names, code, activityId, _expressionEnder); | ||
|
||
var (myDelegate, name) = DefineDelegateCommon(types.Count() - 1); | ||
return string.Format(_delegateValueValidationTemplate, myDelegate, name, serializedArgumentTypes, index, names, code, activityId, _expressionEnder); | ||
} | ||
|
||
internal static (string, string) DefineDelegateCommon(int argumentsCount) | ||
{ | ||
var crtValue = Interlocked.Add(ref crt, 1); | ||
|
||
var part1 = new StringBuilder(); | ||
var part2 = new StringBuilder(); | ||
for (var i = 0; i < argumentsCount; i++) | ||
{ | ||
part1.Append($"in T{i}, "); | ||
part2.Append($" T{i} arg{i},"); | ||
} | ||
part2.Remove(part2.Length - 1, 1); | ||
var name = $"Func{crtValue}"; | ||
return ($"public delegate TResult {name}<{part1} out TResult>({part2});", name); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment has a typo in 'Funct' which should be 'Func'.
Copilot uses AI. Check for mistakes.