Different logic is implemented by pressing a different button
But with the same code:
1 usingSystem.Reflection;2 3 namespaceValuablebook4 {5 /// <summary>6 ///Interaction logic for Item.xaml7 /// </summary>8 Public Partial classItem:window9 {Ten PublicItem () One { A InitializeComponent (); - } - the Private voidButton_Click (Objectsender, RoutedEventArgs e) - { -Button cmd = E.originalsource asButton; -Type type = This. GetType (); +Assembly Assembly =type. Assembly; -Window win = (window) assembly. CreateInstance (type. Namespace +"."+cmd. TAG); + win. ShowDialog (); A at } - } -}
button at the front desk
1 <stackpanel margin="5" button.click="button_click"> 2 <button content="MainWindow" tag="MainWindow" ></Button>3 </StackPanel>
An example on MSDN:
1 usingSystem;2 usingSystem.Reflection;3 usingcontoso.libraries;4 5 namespacecontoso.libraries6 {7 Public class Person8 {9 Private string_name;Ten One PublicPerson () A { } - - PublicPerson (stringname) the { - This. _name =name; - } - + Public stringName -{Get{return This. _name;} + Set{ This. _name =value;} } A at Public Override stringToString () - { - return This. _name; - } - } - } in - Public classExample to { + Public Static voidMain () - { theAssembly Assem =typeof(person). Assembly; *Person P = (person) Assem. CreateInstance ("Contoso.Libraries.Person"); $ if(! (p = =NULL)) {Panax NotoginsengP.name ="John"; -Console.WriteLine ("instantiated a {0} object whose value is ' {1} '", the P.gettype (). Name, p); + } A Else { theConsole.WriteLine ("Unable to instantiate a person object."); + } - } $ } $ //The example displays the following output: - //instantiated a person object whose value is ' John '
Implementing the dynamic response of a button