473,407 Members | 2,546 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,407 software developers and data experts.

object initializer and automatic properties

Consider the following:

object myObject = new Object();
myObject = new { foo = "fooStr", bar = "barStr" };

How do I retrieve the value of foo from myObject?

Dec 13 '07 #1
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
Dec 13 '07 #2
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?
Dec 13 '07 #3
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
Dec 13 '07 #4
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
Dec 14 '07 #5
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
Dec 14 '07 #6
Marc Gravell <ma**********@gmail.comwrote:
Actually, it is using all of auto-properties (get;set;}, an object
initializer ({FunkyInitialize=...}) and an anonymous type (new { foo =
"fooStr", bar = "barString" }).
Whoops - hadn't seen the anonymous type part. Doh!

--
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
Dec 14 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

106
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...
22
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? ...
6
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...
9
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; ...
12
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...
6
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...
12
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. ...
8
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...
4
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...
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
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...
0
marktang
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,...
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
tracyyun
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...
0
agi2029
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,...
0
isladogs
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...

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.