fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Reflection;
  6. using System.Windows.Forms;
  7.  
  8. namespace WindowsFormsApplication1
  9. {
  10. public partial class Form1 : Form
  11. {
  12. public Form1()
  13. {
  14. InitializeComponent();
  15.  
  16. this.Load += Form1_Load;
  17. }
  18.  
  19. void Form1_Load(object sender, EventArgs e)
  20. {
  21. var list = GetSource().ToList();
  22.  
  23. this.comboBox1.SetDataSource(
  24. list,
  25. _ => _.Key,
  26. _ => _.Value);
  27. }
  28.  
  29. Dictionary<string, Assembly> GetSource()
  30. {
  31. return AppDomain.CurrentDomain.GetAssemblies()
  32. .ToDictionary(x => x.FullName, x => x);
  33. }
  34. }
  35.  
  36. public static class Ex
  37. {
  38. public static void SetDataSource<T, TDisplayMember, TValueMember>(
  39. this ComboBox @this, IList<T> dataSource,
  40. Expression<Func<T, TDisplayMember>> displayMember,
  41. Expression<Func<T, TValueMember>> valueMember)
  42. {
  43.  
  44. // _ => _.Key
  45. // ^^^^^ この部分を取得
  46. var displayMemberBody = displayMember.Body as MemberExpression;
  47. if (displayMemberBody == null) throw new ArgumentException();
  48.  
  49. // _ => _.Value
  50. // ^^^^^^^ 同様にこの部分を取得
  51. var valueMemberBody = valueMember.Body as MemberExpression;
  52. if (valueMemberBody == null) throw new ArgumentException();
  53.  
  54. @this.DataSource = dataSource;
  55. @this.DisplayMember = displayMemberBody.Member.Name;
  56. @this.ValueMember = valueMemberBody.Member.Name;
  57. }
  58. }
  59. }
  60.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty