@ -62,25 +62,40 @@ internal class TypedBindingExpression<TSource, TValue> : BindingExpressionBase,
AvaloniaProperty targetProperty ,
AvaloniaProperty targetProperty ,
BindingPriority priority )
BindingPriority priority )
{
{
if ( _ sink is not null )
// The validation is factored out into a non-generic static method so that its code (most
// notably the exception string formatting) is shared across all generic instantiations
// instead of being duplicated for each one, which is a meaningful NativeAOT size saving.
var element = ValidateAttach ( _ sink is not null , TargetProperty , typeof ( TValue ) , target , targetProperty ) ;
_ sink = sink ;
_f rame = frame ;
_ target = new ( element ) ;
TargetProperty = targetProperty ;
Priority = priority ;
}
private static StyledElement ValidateAttach (
bool alreadyAttached ,
AvaloniaProperty ? currentTargetProperty ,
Type valueType ,
AvaloniaObject target ,
AvaloniaProperty targetProperty )
{
if ( alreadyAttached )
throw new InvalidOperationException ( "TypedBindingExpression was already attached." ) ;
throw new InvalidOperationException ( "TypedBindingExpression was already attached." ) ;
if ( target is not StyledElement element )
if ( target is not StyledElement element )
throw new InvalidOperationException ( "TypedBindingExpression may only target StyledElements" ) ;
throw new InvalidOperationException ( "TypedBindingExpression may only target StyledElements" ) ;
if ( TargetProperty is not null & & TargetProperty ! = targetProperty )
if ( current TargetProperty is not null & & current TargetProperty ! = targetProperty )
throw new InvalidOperationException ( "TypedBindingExpression was already attached to a different property." ) ;
throw new InvalidOperationException ( "TypedBindingExpression was already attached to a different property." ) ;
if ( ! typeof ( TValue ) . IsAssignableTo ( targetProperty . PropertyType ) )
if ( ! valueType . IsAssignableTo ( targetProperty . PropertyType ) )
{
{
throw new InvalidOperationException (
throw new InvalidOperationException (
$"TypedBindingExpression of type '{typeof(TValue)}' cannot be bound " +
$"TypedBindingExpression of type '{valueType }' cannot be bound " +
$"to a property of type '{targetProperty.PropertyType}'." ) ;
$"to a property of type '{targetProperty.PropertyType}'." ) ;
}
}
_ sink = sink ;
return element ;
_f rame = frame ;
_ target = new ( element ) ;
TargetProperty = targetProperty ;
Priority = priority ;
}
}
public override void Dispose ( )
public override void Dispose ( )