Etude in C#: Create a strongly typed delegate that returns an anonymous type

Hi, Habr.

Want a simple etude in C#?

Let's say we have an anonymous type new{ Name = "aaa", Number = 123 }

How can we make the delegate getAnonimusInstancethat returns an object of an anonymous type?
I.e. that compiled and worked
<font color="black"><ol> <p><font color="#0000ff">var</font> a = getAnonimusInstance();</li> <p><font color="#2B91AF">Console</font>.WriteLine(a.Name);</li> <p><font color="#0000ff">var</font> b = getAnonimusInstance();</li> <p><font color="#2B91AF">Console</font>.WriteLine(b.Number == a.Number); //where a and b are of an anonymous type, as described above.</li> </ol></font><font color="gray">* This source code was highlighted with <a href="http://virtser.net/blog/post/source-code-highlighter.aspx"><font color="gray">Source Code Highlighter</font></a>.</font>
October 10th 19 at 03:42
2 answers
October 10th 19 at 03:44

dynamic GetAnonimusInstance()
{
 return new {Name = "aaa", Number = 123};
}

Isn't it?
No. Typification of the duck will be. Reflection is also not the solution. To clarify — the problem can be solved using .net 3.5 - Gennaro.Altenwer commented on October 10th 19 at 03:47
October 10th 19 at 03:46
<font color="black"><font color="#0000ff">class</font> Program<br> {<br> <font color="#0000ff">static</font> <font color="#0000ff">object</font> ReturnAnonymous()<br> {<br> <font color="#0000ff">return</font> <font color="#0000ff">new</font> { Name = <font color="#A31515">"aaa"</font>, Number = 123 };<br> }<br> <br / > <font color="#0000ff">static</font> Func<t> Cast<t>(<font color="#0000ff">object</font> obj, T type)<br> {<br> <font color="#0000ff">return</font> () => (T)obj;<br> }<br> <br / > <font color="#0000ff">static</font> <font color="#0000ff">void</font> Main(<font color="#0000ff">string</font>[] args)<br> {<br> <font color="#0000ff">object</font> o = ReturnAnonymous();<br> <font color="#0000ff">var</font> getAnonimusInstance = Cast(o, <font color="#0000ff">new</font> { Name = <font color="#A31515">""</font> Number = 0 });<br> <br / > <font color="#0000ff">var</font> a = getAnonimusInstance();<br> <font color="#2B91AF">Console</font>.WriteLine(a.Name);<br> <font color="#0000ff">var</font> b = getAnonimusInstance();<br> <font color="#2B91AF">Console</font>.WriteLine(b.Number == a.Number); <font color="#008000">//where a and b are of an anonymous type, as described above.</font><br> }<br> }</t></t></font><br> <br / > <font color="gray">* This source code was highlighted with <a href="http://virtser.net/blog/post/source-code-highlighter.aspx"><font color="gray">Source Code Highlighter</font></a>.</font>


I wrote to clarify about 3.5, adapt laziness.
Well the whole idea is correct =) the Only thing that you can do without caste.

<font color="black"><ol> <p><font color="#0000ff">public</font> <font color="#0000ff">static</font> Func<t > > MakeFunc<t>(Func<t> selector)</t></t></t></li> <p>{</li> <p> <font color="#0000ff">return</font> selector;</li> the <li>}</li> <p> </li> <p> </li> <p><font color="#0000ff">var</font> getAnonimusInstance = MakeFunc(()=> <font color="#0000ff">new</font>{ Name = <font color="#A31515">"aaa"</font>, Number = 123 }); </li> <p> </li> <p><font color="#0000ff">var</font> a = getAnonimusInstance();</li> <p><font color="#2B91AF">Console</font>.WriteLine(a.Name);</li> <p><font color="#0000ff">var</font> b = getAnonimusInstance();</li> <p><font color="#2B91AF">Console</font>.WriteLine(b.Number == a.Number); //where a and b are of an anonymous type, as described above.</li> </ol></font><font color="gray">* This source code was highlighted with <a href="http://virtser.net/blog/post/source-code-highlighter.aspx"><font color="gray">Source Code Highlighter</font></a>.</font>
- Gennaro.Altenwer commented on October 10th 19 at 03:49
Remained the main question — why? - Gennaro.Altenwer commented on October 10th 19 at 03:52

Find more questions by tags .NETC#