public class Region { public string Name { get ; set ; } public int Index { get ; set ; } public IEnumerable <Region> SubRegions { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
public class Region { public string Name { get ; set ; } public int Index { get ; set ; } public IEnumerable <Region> SubRegions { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
public class Region { public string Name { get ; set ; } public int Index { get ; set ; } public IEnumerable <Region> SubRegions { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
public class Region { public string Name { get ; set ; } public int Index { get ; set ; } public IEnumerable <Region> SubRegions { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
public class Region { public string Name { get ; set ; } public int Index { get ; set ; } public IEnumerable <Region> SubRegions { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
public class Region { public string Name { get ; set ; } public int Index { get ; set ; } public IEnumerable <Region> SubRegions { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
public class Region { public string Name { get ; set ; } public int Index { get ; set ; } public IEnumerable <Region> SubRegions { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
public class Region { public string Name { get ; set ; } public int Index { get ; set ; } public IEnumerable <Region> SubRegions { get ; set ; } } * This source code was highlighted with Source Code Highlighter .
* This source code was highlighted with Source Code Highlighter .
- public class RegionViewModel
- {
- public Region Value { get ; set ; }
- public IEnumerable <RegionViewModel> SubRegions { get { /*...*/ }}
- }
* This source code was highlighted with Source Code Highlighter .
- public override bool TryInvokeMember (InvokeMemberBinder binder, object [] args, out object result)
- {
- result = _methods.ContainsKey (binder.Name)? _methods [binder.Name] .DynamicInvoke ( new [] { this } .Concat (args) .ToArray ()): InvokeNativeMethod (binder.Name, args);
- result = GetResult (result);
- return true ;
- }
* This source code was highlighted with Source Code Highlighter .
- private class MyClass
- {
- MyClass _i;
- public string Name { get ; set ; }
- public MyClass Foo ()
- {
- return _i ?? (_i = new MyClass {Name = "_sdfdsfsdfsfd" });
- }
- public IEnumerable <MyClass> GetChilds ()
- {
- yield return new MyClass ();
- yield return new MyClass ();
- }
- }
- [TestMethod]
- public void TestAddProperties ()
- {
- var a = new MyClass {Name = "123" };
- Assert.AreEqual ( "123" , a.Name);
- dynamic proxy = DynamicProxy.Create (a) .AddProperty < bool > ( "IsSelected" )
- .AddProperty ( "X" , _ => x, (_, value ) => x = value )
- .AddProperty ( "LastName" , "FFFF" )
- proxy.Name = "567" ;
- proxy.IsSelected = true ;
- proxy.X = 42;
- Assert.AreEqual ( "567" , a.Name);
- Assert.IsTrue (proxy.IsSelected);
- Assert.AreEqual (42, x);
- proxy.IsSelected = false ;
- Assert.IsFalse (proxy.IsSelected);
- }
* This source code was highlighted with Source Code Highlighter .
- [TestMethod]
- public void TestChilds ()
- {
- var a = new MyClass {Name = "123" };
- Assert.AreEqual ( "123" , a.Name);
- Assert.AreEqual ( "_sdfdsfsdfsfd" , a.Foo (). Name);
- var x = 0;
- dynamic proxy = DynamicProxy.Create (a) .AddProperty < bool > ( "IsSelected" )
- .AddProperty ( "X" , _ => x, (_, value ) => x = value )
- .AddProperty ( "LastName" , "FFFF" )
- .AddMethod ( "Boo" , new Func <DynamicProxy <MyClass>, int , string > ((m, i) => ((MyClass) m) .Name + i.ToString ())));
- proxy.Name = "567" ;
- proxy.IsSelected = true ;
- proxy.X = 42;
- var b = proxy.Foo ();
- b.IsSelected = true ;
- Assert.AreEqual ( "567" , a.Name);
- Assert.AreEqual ( "5674" , proxy.Boo (4));
- Assert.IsTrue (proxy.IsSelected);
- Assert.AreEqual (42, x);
- Assert.IsTrue (b.IsSelected);
- b.IsSelected = false ;
- Assert.IsTrue (proxy.IsSelected);
- Assert.IsFalse (b.IsSelected);
- proxy.LastName = "890" ;
- var d = proxy.Foo ();
- Assert.AreEqual ( "FFFF" , d.LastName);
- var d2 = proxy.Foo ();
- d2.LastName = "RRRRR" ;
- Assert.AreEqual ( "567" , Foo (proxy));
- Assert.AreEqual (d.LastName, d2.LastName);
- // It is possible
to rob the cowsto do an impassable caste- var c = (MyClass) proxy;
- Assert.AreEqual ( "567" , c.Name);
- foreach ( var child in proxy.GetChilds ())
- {
- child.IsSelected = true ;
- Assert.IsTrue (child.IsSelected);
- }
- }
* This source code was highlighted with Source Code Highlighter .
- [TestMethod]
- public void TestPropertyChange ()
- {
- var myClass = new MyClass ();
- var propertyName = string .Empty;
- dynamic proxy = DynamicProxy.Create (myClass);
- ((INotifyPropertyChanged) proxy). PropertyChanged + = (s, a) => propertyName = a.PropertyName;
- proxy.Name = "aaaa" ;
- Assert.AreEqual ( "Name" , propertyName);
- }
* This source code was highlighted with Source Code Highlighter .
- public MainWindow ()
- {
- Initializecomponent ();
- DataContext = this ;
- Items = new [] {DynamicProxy.Create (CreateRegions (). First ()). AddProperty < bool > ( "IsSelected" )};
- }
- IEnumerable <Region> GetSelectedItems ( IEnumerable <dynamic> items)
- {
- Return items.Where (x => x.IsSelected) .Concat (items.SelectMany (x => GetSelectedItems (( IEnumerable <dynamic>) x.SubRegions))). Select (x => (Region) x);
- }
- private void ButtonClick ( object sender, RoutedEventArgs e)
- {
- var res = GetSelectedItems (Items) .Take (10) .ToList ();
- SelectedItems = res;
- }
* This source code was highlighted with Source Code Highlighter .
- < Window x: Class = "WpfApplication1.MainWindow"
- xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns: WpfApplication1 = "clr-namespace: WpfApplication1"
- Title = "MainWindow" Height = "350" Width = "525" >
- < Window.Resources >
- < DataTemplate DataType = "{x: Type WpfApplication1: Region}" >
- < WrapPanel >
- < TextBlock Text = "{Binding Path = Name, StringFormat = '{} {0},'}" />
- < TextBlock Text = "{Binding Path = Index}" />
- </ WrapPanel >
- </ DataTemplate >
- </ Window.Resources >
- < Grid >
- < Grid.ColumnDefinitions >
- < ColumnDefinition Width = "*" />
- < ColumnDefinition Width = "auto" />
- < ColumnDefinition Width = "*" />
- </ Grid.ColumnDefinitions >
- < TreeView Grid . Column = "0" ItemsSource = "{Binding Items}" BorderThickness = "0" >
- < TreeView.ItemTemplate >
- < HierarchicalDataTemplate ItemsSource = "{Binding SubRegions}" >
- < CheckBox IsChecked = "{Binding IsSelected, Mode = TwoWay}" Content = "{Binding Value}" />
- </ HierarchicalDataTemplate >
- </ TreeView.ItemTemplate >
- </ TreeView >
- < Button Content = "Show selected" VerticalAlignment = "Center" Grid . Column = "1" Click = "ButtonClick" />
- < ListBox Grid . Column = "2" ItemsSource = "{Binding SelectedItems}" BorderThickness = "0" />
- </ Grid >
- </ Window >
Source: https://habr.com/ru/post/124078/
All Articles