Skip to content

Instantly share code, notes, and snippets.

@oleksabor
Last active January 23, 2020 11:34
Show Gist options
  • Select an option

  • Save oleksabor/b1e186be0b80f5e6f64c585535315d8f to your computer and use it in GitHub Desktop.

Select an option

Save oleksabor/b1e186be0b80f5e6f64c585535315d8f to your computer and use it in GitHub Desktop.

Revisions

  1. oleksabor revised this gist Jan 23, 2020. 1 changed file with 24 additions and 23 deletions.
    47 changes: 24 additions & 23 deletions ReturnAnonymousType.cs
    Original file line number Diff line number Diff line change
    @@ -1,30 +1,31 @@
    public class MyController
    public class MyController
    {
    public object Get(string id)
    {
    public object Get(string id)
    {
    return new { Key = id, Value = "val:" + id };
    }
    return new { Key = id, Value = "val:" + id };
    }
    class Program
    }
    class Program
    {
    // Application entry-point
    static void Main()
    {
    // Application entry-point
    static void Main()
    {
    var sut = new MyController();
    var sut = new MyController();

    // Act.
    dynamic res = sut.Get("42");
    // Act.
    dynamic res = sut.Get("42");

    // Second attempt:
    var a = new { Key = "", Value = "" };
    a = Cast(a, res); // <- the code works
    // Second attempt:
    var a = new { Key = "", Value = "" };
    a = Cast(a, res); // <- the code works

    Console.WriteLine($"{a.Key} {a.Value}");
    }
    Console.WriteLine($"{a.Key} {a.Value}");
    }

    private static T Cast<T>(T typeHolder, object x)
    {
    // typeHolder above is just for compiler magic
    // to infer the type to cast x to
    return (T)x;
    }
    private static T Cast<T>(T typeHolder, object x)
    {
    // typeHolder above is just for compiler magic
    // to infer the type to cast x to
    return (T)x;
    }
    }
  2. oleksabor revised this gist Jan 23, 2020. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions ReturnAnonymousType.cs
    Original file line number Diff line number Diff line change
    @@ -15,11 +15,9 @@ static void Main()
    // Act.
    dynamic res = sut.Get("42");

    // Assert.

    // Second attempt:
    var a = new { Key = "", Value = "" };
    a = Cast(a, res); // <- the code fails already here
    a = Cast(a, res); // <- the code works

    Console.WriteLine($"{a.Key} {a.Value}");
    }
  3. oleksabor created this gist Jan 23, 2020.
    32 changes: 32 additions & 0 deletions ReturnAnonymousType.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    public class MyController
    {
    public object Get(string id)
    {
    return new { Key = id, Value = "val:" + id };
    }
    }
    class Program
    {
    // Application entry-point
    static void Main()
    {
    var sut = new MyController();

    // Act.
    dynamic res = sut.Get("42");

    // Assert.

    // Second attempt:
    var a = new { Key = "", Value = "" };
    a = Cast(a, res); // <- the code fails already here

    Console.WriteLine($"{a.Key} {a.Value}");
    }

    private static T Cast<T>(T typeHolder, object x)
    {
    // typeHolder above is just for compiler magic
    // to infer the type to cast x to
    return (T)x;
    }