Skip to content

Instantly share code, notes, and snippets.

@gowon
Last active August 12, 2022 07:06
Show Gist options
  • Save gowon/850b32c970afaa5972e5 to your computer and use it in GitHub Desktop.
Save gowon/850b32c970afaa5972e5 to your computer and use it in GitHub Desktop.
Create an Enumerable for an Anonymous Type
var list = Enumerable.Empty<object>()
.Select(r => new {A = 0, B = 0}) // prototype of anonymous type
.ToList();
list.Add(new { A = 4, B = 5 }); // adding actual values
Console.Write(list[0].A);
// http://stackoverflow.com/a/901936
public static class Extensions{
public static IEnumerable<T> CastByExample<T>(
this IEnumerable sequence,
T example) where T: class
{
foreach (Object o in sequence)
yield return o as T;
}
}
public static class AnonymousEnumerableFactory{
public static IEnumerable<T> CastByExample<T>(T example) where T: class
{
//http://stackoverflow.com/a/14702242
Func<object, T> func1 = (x) => example;
var list = Enumerable.Empty<object>()
.Select(func1); // prototype of anonymous type
return list;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment