Last active
August 12, 2022 07:06
-
-
Save gowon/850b32c970afaa5972e5 to your computer and use it in GitHub Desktop.
Create an Enumerable for an Anonymous Type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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