浏览代码

Fix static property getter/setter

Max Katz 2 年之前
父节点
当前提交
c111b235f7

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

+ 24 - 1
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]
         public void ResolvesDataTypeFromBindingProperty()
         {
@@ -1716,7 +1737,9 @@ namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions
 
         string IHasExplicitProperty.ExplicitProperty => "Hello"; 
 
-        public string ExplicitProperty => "Bye"; 
+        public string ExplicitProperty => "Bye";
+
+        public static string StaticProperty => "World"; 
 
         public class NonIntegerIndexer : NotifyingBase, INonIntegerIndexerDerived
         {