Skip to content

Commit 7f955ca

Browse files
Avoid throwing in async void (dotnet#24780)
1 parent c6ca951 commit 7f955ca

2 files changed

Lines changed: 24 additions & 12 deletions

File tree

src/Controls/src/Core/Binding.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,17 @@ internal override void Apply(object context, BindableObject bindObj, BindablePro
137137

138138
if (Source is RelativeBindingSource relativeBindingSource)
139139
{
140-
ApplyRelativeSourceBinding(relativeBindingSource, bindObj, targetProperty, specificity);
140+
var relativeSourceTarget = RelativeSourceTargetOverride ?? bindObj as Element;
141+
if (relativeSourceTarget is not Element)
142+
{
143+
var message = bindObj is not null
144+
? $"Cannot apply relative binding to {bindObj.GetType().FullName} because it is not a superclass of Element."
145+
: "Cannot apply relative binding when the target object is null.";
146+
147+
throw new InvalidOperationException(message);
148+
}
149+
150+
ApplyRelativeSourceBinding(relativeBindingSource, relativeSourceTarget, bindObj, targetProperty, specificity);
141151
}
142152
else
143153
{
@@ -148,13 +158,9 @@ internal override void Apply(object context, BindableObject bindObj, BindablePro
148158
}
149159

150160
#pragma warning disable RECS0165 // Asynchronous methods should return a Task instead of void
151-
async void ApplyRelativeSourceBinding(RelativeBindingSource relativeSource, BindableObject targetObject, BindableProperty targetProperty, SetterSpecificity specificity)
161+
async void ApplyRelativeSourceBinding(RelativeBindingSource relativeSource, Element relativeSourceTarget, BindableObject targetObject, BindableProperty targetProperty, SetterSpecificity specificity)
152162
#pragma warning restore RECS0165 // Asynchronous methods should return a Task instead of void
153163
{
154-
var relativeSourceTarget = RelativeSourceTargetOverride ?? targetObject as Element;
155-
if (!(relativeSourceTarget is Element))
156-
throw new InvalidOperationException();
157-
158164
await relativeSource.Apply(_expression, relativeSourceTarget, targetObject, targetProperty, specificity);
159165
}
160166

src/Controls/src/Core/TypedBinding.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,17 @@ internal override void Apply(object context, BindableObject bindObj, BindablePro
182182

183183
if (Source is RelativeBindingSource relativeSource)
184184
{
185-
ApplyRelativeSourceBinding(relativeSource, bindObj, targetProperty, specificity);
185+
var relativeSourceTarget = RelativeSourceTargetOverride ?? bindObj as Element;
186+
if (relativeSourceTarget is not Element)
187+
{
188+
var message = bindObj is not null
189+
? $"Cannot apply relative binding to {bindObj.GetType().FullName} because it is not a superclass of Element."
190+
: "Cannot apply relative binding when the target object is null.";
191+
192+
throw new InvalidOperationException(message);
193+
}
194+
195+
ApplyRelativeSourceBinding(relativeSource, relativeSourceTarget, bindObj, targetProperty, specificity);
186196
}
187197
else
188198
{
@@ -192,13 +202,9 @@ internal override void Apply(object context, BindableObject bindObj, BindablePro
192202

193203
#pragma warning disable RECS0165 // Asynchronous methods should return a Task instead of void
194204
async void ApplyRelativeSourceBinding(
195-
RelativeBindingSource relativeSource, BindableObject targetObject, BindableProperty targetProperty, SetterSpecificity specificity)
205+
RelativeBindingSource relativeSource, Element relativeSourceTarget, BindableObject targetObject, BindableProperty targetProperty, SetterSpecificity specificity)
196206
#pragma warning restore RECS0165 // Asynchronous methods should return a Task instead of void
197207
{
198-
var relativeSourceTarget = RelativeSourceTargetOverride ?? targetObject as Element;
199-
if (!(relativeSourceTarget is Element))
200-
throw new InvalidOperationException();
201-
202208
await relativeSource.Apply(this, relativeSourceTarget, targetObject, targetProperty, specificity);
203209
}
204210

0 commit comments

Comments
 (0)