Consider the following:
object myObject = new Object();
myObject = new { foo = "fooStr", bar = "barStr" };
How do I retrieve the value of foo from myObject? 6 1427
bill tie <bi*****@discussions.microsoft.comwrote:
Consider the following:
object myObject = new Object();
myObject = new { foo = "fooStr", bar = "barStr" };
How do I retrieve the value of foo from myObject?
You can't. Note that this is using anonymous types, not object
initializers or automatic properties.
Anonymous types are usually used either within a context where the type
is propagated (e.g. LINQ) or with implicitly typed local variables,
e.g.:
var myObject = new { foo = "fooStr", bar = "barStr" };
string x = myObject.foo;
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
To simplify the riddle I tried to emulate a class that I had perused in the
Reflector. The class MAY look like this:
public class FunkyClass
{
public object FunkyProperty
{
get;
set;
}
}
....
myList.Add(new FunkyClass
{
FunkyProperty = new { foo = "fooStr", bar = "barString" }
}
);
What gives?
bill tie <bi*****@discussions.microsoft.comwrote:
To simplify the riddle I tried to emulate a class that I had perused in the
Reflector. The class MAY look like this:
public class FunkyClass
{
public object FunkyProperty
{
get;
set;
}
}
Right: *That* is an automatically implemented property.
myList.Add(new FunkyClass
{
FunkyProperty = new { foo = "fooStr", bar = "barString" }
}
);
And *that* is using an object initializer.
What gives?
I don't see what you mean. Previously you were using one particular
feature, now you're using two different ones. What's confusing you?
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Actually, it is using all of auto-properties (get;set;}, an object
initializer ({FunkyInitialize=...}) and an anonymous type (new { foo =
"fooStr", bar = "barString" }).
Since the auto-property is typed as object there is no friendly way of
getting the properties, except for reflection / component-model:
var item = myList[0].FunkyProperty;
// (item is typed as "object" due to FunkyProperty)
foreach(PropertyDescriptor prop in
TypeDescriptor.GetProperties(item)) {
Console.WriteLine("{0}={1}", prop.Name,
prop.GetValue(item));
}
Marc
One other thought; you might want to verify the syntax isn't actually
more like:
var myList = new Dictionary<int, string{
{1,"abc"}
};
This is the more general form of a collection-initializer, meaning
call .Add(1,"abc")
Just a thought...
Marc This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: A |
last post by:
Hi,
I have always been taught to use an inialization list for initialising data
members of a class. I realize that initialsizing primitives and pointers use
an inialization list is exactly the...
|
by: Peter Ammon |
last post by:
A friend was asked "How many ways are there to create an object in C++?"
in an interview. Apparently, the "right" answer was eleven. Does
anyone know how the interviewer arrived at that number? ...
|
by: Marty M |
last post by:
The following causes the "invalid initializer" message during gcc
compile time...
char firstword = word(question,1);
the "word" function is...
char * word(char *phrase, int what)
{...body...
|
by: Andrew Au |
last post by:
Dear all,
I am trying to write a piece of software that use Object Oriented design
and implement it with C, I did the following
== In Object.h ==
typedef struct ObjectStructure* Object;
...
|
by: Jim Hammond |
last post by:
I am passing the whole object instead or parameters in my select and update
methods.
I can get the updated object if I set UpdateMethod, let ASP.NET autogenerate
an update button, and then press...
|
by: Jake Barnes |
last post by:
I was just reading this article on Ajaxian:
http://ajaxian.com/archives/show-love-to-the-object-literal
This is a newbie question, but what is the object literal? I thought it
was like an...
|
by: Andrew Poulos |
last post by:
With the following code I can't understand why this.num keeps
incrementing each time I create a new instance of Foo. For each instance
I'm expecting this.num to alert as 1 but keeps incrementing.
...
|
by: sarathy |
last post by:
Hi,
I read the following points in K&R "Section A8.7 Initialization".
Seems like i have a problem with them.
* All expressions in the initialization of constant object/array must
be constant...
|
by: Jess |
last post by:
Hello,
I tried several books to find out the details of object
initialization. Unfortunately, I'm still confused by two specific
concepts, namely default-initialization and...
|
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
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
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,...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |