fork download
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using Microsoft.CSharp.RuntimeBinder; // Requires a reference to Microsoft.CSharp
  4.  
  5. public class ImplicitTest
  6. {
  7. public double Val { get; set; }
  8.  
  9. public ImplicitTest(double Val)
  10. {
  11. this.Val = Val;
  12. }
  13.  
  14. public static implicit operator int(ImplicitTest d)
  15. {
  16. return (int)d.Val;
  17. }
  18. }
  19.  
  20. public class TestClass
  21. {
  22. public int Val { get; set; }
  23.  
  24. public TestClass(int Val)
  25. {
  26. this.Val = Val;
  27. }
  28. }
  29.  
  30. public class Test
  31. {
  32. static void Main(string[] args)
  33. {
  34. var call = CallSite<Func<CallSite, Type, ImplicitTest, TestClass>>.Create(
  35. Binder.InvokeConstructor(CSharpBinderFlags.None, typeof(Test),
  36. new CSharpArgumentInfo[] {
  37. CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.IsStaticType | CSharpArgumentInfoFlags.UseCompileTimeType, null),
  38. CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
  39. }));
  40.  
  41. TestClass tc = call.Target.Invoke(call, typeof(TestClass), new ImplicitTest(15.5));
  42.  
  43. Console.WriteLine(tc.Val);
  44. }
  45. }
Success #stdin #stdout 0.41s 29944KB
stdin
Standard input is empty
stdout
15