473,397 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

C# cast to anonymous type

2
consider the following code:

Expand|Select|Wrap|Line Numbers
  1. var test1 = new { field1 = "hello" };
  2. ArrayList al = new ArrayList();
  3. al.Add(test1);
  4.  
  5. //compile error
  6. Console.WriteLine(al[0].field1);
  7.  
an anonymous type is created than placed into ArrayList.
al[0] obviously returns Object, how to cast this back to anonymous type to retrieve value of 'field1'?
Aug 22 '09 #1
4 9620
tlhintoq
3,525 Expert 2GB
just guessing but have you tried...

(var)al[0].field1

or

var Recovered = (var)al[0];
Console.WriteLine(Recovered.field1);
Aug 22 '09 #2
venil7
2
ok, I found the answer:

first we need an Extension class:

Expand|Select|Wrap|Line Numbers
  1. static class Extension
  2.     {
  3.         static public T CastTo<T>(this object o, T t)
  4.         {
  5.             return (T)o;
  6.         }
  7.     }
which will add CastTo method to all Objects, then

Expand|Select|Wrap|Line Numbers
  1. var v = al[0];
  2. Console.WriteLine(v.CastTo(new { field1=String.Empty}).field1);
the trick is to pass an anonymous object to CastTo which has same signature as the one that we put to ArrayList
Aug 22 '09 #3
Plater
7,872 Expert 4TB
Why did you need to do all that work instead of just type-casting?
Aug 25 '09 #4
Yeah, at that rate, introspection is straightforward enough.

You should be able to do something like:

f = a.GetType().GetProperty("field1").GetValue(a);
Oct 1 '14 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: James Mcguire | last post by:
Hi, I frequently do non-initialisation type structure assignment via casting: e.g. struct s{int i,j,k;} mys; .... mys=(struct s){3,4,5};
1
by: Ya1000 | last post by:
I create an object with type A dinamicly and when I obtain it I tryed cast it to A and this action cause an exception A a = (A)dynamicCreation(...); // this cause invalid cast exceptio Thanks...
3
by: Harry Keck | last post by:
I would imagine that this is a pretty trivial operation, but I can not figure out how to cast a string variable to a Decimal. I tried Decimal dVariable = (Decimal)strStringVariable, but it does not...
17
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
8
by: Charles | last post by:
I do not understand why I am getting a "Specified cast is not valid" error, since it has worked before. Something has changed and I am not really sure what it could be. I am looking for something...
9
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but...
16
by: Martin Jørgensen | last post by:
Hi, Short question: Any particular reason for why I'm getting a warning here: (cast from function call of type int to non-matching type double) xdouble = (double)rand()/(double)RAND_MAX;
20
by: raylopez99 | last post by:
Inspired by Chapter 8 of Albahari's excellent C#3.0 in a Nutshell (this book is amazing, you must get it if you have to buy but one C# book) as well as Appendix A of Jon Skeet's book, I am going...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.