code_junkie

C# feature request: implement interfaces on anonymous types

Nov 14th, 2011
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. using System;
  2.  
  3. class Program
  4. {
  5. static void Main()
  6. {
  7. var f = new IFoo {
  8. Foo = "foo",
  9. Print = () => Console.WriteLine(Foo)
  10. };
  11. }
  12. }
  13.  
  14. interface IFoo
  15. {
  16. String Foo { get; set; }
  17. void Print();
  18. }
  19.  
  20. internal sealed class <>f__AnonymousType0<<Foo>j__TPar> : IFoo
  21. {
  22. readonly <Foo>j__TPar <Foo>i__Field;
  23.  
  24. public <>f__AnonymousType0(<Foo>j__TPar Foo)
  25. {
  26. this.<Foo>i__Field = Foo;
  27. }
  28.  
  29. public <Foo>j__TPar Foo
  30. {
  31. get { return this.<Foo>i__Field; }
  32. }
  33.  
  34. public void Print()
  35. {
  36. Console.WriteLine(this.Foo);
  37. }
  38. }
  39.  
  40. Create<IFoo>
  41. (
  42. new
  43. {
  44. Foo = "foo",
  45. Print = (Action)(() => Console.WriteLine(Foo))
  46. }
  47. );
  48.  
  49. T Create<T> (object o)
  50. {
  51. //...
  52. }
  53.  
  54. Create
  55. {
  56. new Members<IFoo>
  57. {
  58. {"Print", ((IFoo @this)=>Console.WriteLine(Foo))},
  59. {"Foo", "foo"}
  60. }
  61. }
  62.  
  63. using ImpromptuInterface.Dynamic;
  64.  
  65. var f = ImpromptuGet.Create<IFoo>(new{
  66. Foo = "foo",
  67. Print = ReturnVoid.Arguments(() => Console.WriteLine(Foo))
  68. });
  69.  
  70. void Main() {
  71. var foo = new Foo();
  72. foo.Bar = "bar";
  73. foo.Print = () => Console.WriteLine(foo.Bar);
  74. foo.Print();
  75. }
  76.  
  77.  
  78. class Foo : IFoo {
  79. public String Bar { get; set; }
  80. public Action Print {get;set;}
  81. }
  82.  
  83. using System;
  84.  
  85. class Program {
  86. static void Main() {
  87. var f = new IFoo() {
  88. public String Foo { get { return "foo"; } }
  89. public void Print() { Console.WriteLine(Foo); }
  90. };
  91. }
  92. }
  93.  
  94. interface IFoo {
  95. String Foo { get; set; }
  96. void Print();
  97. }
Add Comment
Please, Sign In to add comment