Acces to property hidden field
If yyou want to acces easily you can show this post
use Mono.Reflection as nuget https://github.com/jbevain/mono.reflection/
and use with
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<Window x:Class="HiddenFields.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:HiddenFields" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <Button Content="SET" HorizontalAlignment="Left" Height="75" Margin="10,10,0,0" VerticalAlignment="Top" Width="170" Click="Button_Click" /> <Button Content="UPDATE" HorizontalAlignment="Left" Height="75" Margin="10,99,0,0" VerticalAlignment="Top" Width="170" Click="Update_Click" /> <TextBlock HorizontalAlignment="Left" Height="23" Margin="10,179,0,0" TextWrapping="Wrap" Text="{Binding XName}" VerticalAlignment="Top" Width="170" /> </Grid> </Window> |
behind
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
using Mono.Reflection; namespace HiddenFields { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window, INotifyPropertyChanged { public event PropertyChangedEventHandler? PropertyChanged; public string XName { get; set; } = "alpha"; public MainWindow() { InitializeComponent(); this.DataContext = this; } private void Button_Click(object sender, RoutedEventArgs e) { var f = typeof(MainWindow) .GetProperty(nameof(MainWindow.XName)) .GetBackingField(); f.SetValue(this, "zouzou"); } private void Update_Click(object sender, RoutedEventArgs e) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(MainWindow.XName))); } } } |
see extension for sample
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
public static class BackingFieldResolver { class FieldPattern : ILPattern { public static object FieldKey = new object (); ILPattern pattern; public FieldPattern (ILPattern pattern) { this.pattern = pattern; } public override void Match (MatchContext context) { pattern.Match (context); if (!context.success) return; var match = GetLastMatchingInstruction (context); var field = (FieldInfo) match.Operand; context.AddData (FieldKey, field); } } static ILPattern Field (OpCode opcode) { return new FieldPattern (ILPattern.OpCode (opcode)); } static ILPattern GetterPattern = ILPattern.Sequence ( ILPattern.Optional (OpCodes.Nop), ILPattern.Either ( Field (OpCodes.Ldsfld), ILPattern.Sequence ( ILPattern.OpCode (OpCodes.Ldarg_0), Field (OpCodes.Ldfld))), ILPattern.Optional ( ILPattern.Sequence ( ILPattern.OpCode (OpCodes.Stloc_0), ILPattern.OpCode (OpCodes.Br_S), ILPattern.OpCode (OpCodes.Ldloc_0))), ILPattern.Optional(ILPattern.OpCode(OpCodes.Br_S)), ILPattern.OpCode (OpCodes.Ret)); static ILPattern SetterPattern = ILPattern.Sequence ( ILPattern.Optional (OpCodes.Nop), ILPattern.OpCode (OpCodes.Ldarg_0), ILPattern.Either ( Field (OpCodes.Stsfld), ILPattern.Sequence ( ILPattern.OpCode (OpCodes.Ldarg_1), Field (OpCodes.Stfld))), ILPattern.OpCode (OpCodes.Ret)); static FieldInfo GetBackingField (MethodInfo method, ILPattern pattern) { var result = ILPattern.Match (method, pattern); if (!result.success) throw new ArgumentException (); object value; if (!result.TryGetData (FieldPattern.FieldKey, out value)) throw new InvalidOperationException (); return (FieldInfo) value; } public static FieldInfo GetBackingField (this PropertyInfo self) { if (self == null) throw new ArgumentNullException ("self"); var getter = self.GetGetMethod (true); if (getter != null) return GetBackingField (getter, GetterPattern); var setter = self.GetSetMethod (true); if (setter != null) return GetBackingField (setter, SetterPattern); throw new ArgumentException (); } } |
Categories: Non classé
Commentaires récents