Browse Source

Fix static property getter/setter

pull/9460/head
Max Katz 4 years ago
parent
commit
c111b235f7
  1. 21
      src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/XamlIlClrPropertyInfoHelper.cs
  2. 25
      tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/CompiledBindingExtensionTests.cs

21
src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/XamlIlClrPropertyInfoHelper.cs

@ -59,14 +59,17 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions
var field = _builder.DefineField(types.IPropertyInfo, name + "!Field", false, true); var field = _builder.DefineField(types.IPropertyInfo, name + "!Field", false, true);
void Load(IXamlMethod m, IXamlILEmitter cg) void Load(IXamlMethod m, IXamlILEmitter cg, bool passThis)
{ {
cg if (passThis)
.Ldarg_0(); {
if (m.DeclaringType.IsValueType) cg
cg.Unbox(m.DeclaringType); .Ldarg_0();
else if (m.DeclaringType.IsValueType)
cg.Castclass(m.DeclaringType); cg.Unbox(m.DeclaringType);
else
cg.Castclass(m.DeclaringType);
}
foreach (var indexerArg in indexerArguments) foreach (var indexerArg in indexerArguments)
{ {
@ -80,7 +83,7 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions
new[] {types.XamlIlTypes.Object}, name + "!Getter", false, true, false); new[] {types.XamlIlTypes.Object}, name + "!Getter", false, true, false);
if (getter != null) if (getter != null)
{ {
Load(property.Getter, getter.Generator); Load(property.Getter, getter.Generator, !property.Getter.IsStatic);
getter.Generator.EmitCall(property.Getter); getter.Generator.EmitCall(property.Getter);
if (property.Getter.ReturnType.IsValueType) if (property.Getter.ReturnType.IsValueType)
@ -95,7 +98,7 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions
name + "!Setter", false, true, false); name + "!Setter", false, true, false);
if (setter != null) if (setter != null)
{ {
Load(property.Setter, setter.Generator); Load(property.Setter, setter.Generator, !property.Getter.IsStatic);
setter.Generator.Ldarg(1); setter.Generator.Ldarg(1);
if (property.Setter.Parameters[0].IsValueType) if (property.Setter.Parameters[0].IsValueType)

25
tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/CompiledBindingExtensionTests.cs

@ -139,6 +139,27 @@ namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions
} }
} }
[Fact]
public void ResolvesStaticClrPropertyBased()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
var xaml = @"
<Window xmlns='https://github.com/avaloniaui'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.MarkupExtensions;assembly=Avalonia.Markup.Xaml.UnitTests'
x:DataType='local:TestDataContext'>
<TextBlock Text='{CompiledBinding StaticProperty}' Name='textBlock' />
</Window>";
var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
var textBlock = window.FindControl<TextBlock>("textBlock");
textBlock.DataContext = new TestDataContext();
Assert.Equal(TestDataContext.StaticProperty, textBlock.Text);
}
}
[Fact] [Fact]
public void ResolvesDataTypeFromBindingProperty() public void ResolvesDataTypeFromBindingProperty()
{ {
@ -1716,7 +1737,9 @@ namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions
string IHasExplicitProperty.ExplicitProperty => "Hello"; string IHasExplicitProperty.ExplicitProperty => "Hello";
public string ExplicitProperty => "Bye"; public string ExplicitProperty => "Bye";
public static string StaticProperty => "World";
public class NonIntegerIndexer : NotifyingBase, INonIntegerIndexerDerived public class NonIntegerIndexer : NotifyingBase, INonIntegerIndexerDerived
{ {

Loading…
Cancel
Save