Bug 5001 - Object initializers are not handled properly
Summary: Object initializers are not handled properly
Alias: None
Product: Xamarin Studio
Classification: Desktop
Component: C# Binding ()
Version: Trunk
Hardware: PC Mac OS
: Low normal
Target Milestone: ---
Assignee: Mike Krüger
Depends on:
Reported: 2012-05-10 10:27 UTC by Alan McGovern
Modified: 2012-05-18 05:14 UTC (History)
1 user (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Alan McGovern 2012-05-10 10:27:20 UTC
var data = new Foo {

When i type this i would expect to only be offered properties in 'Foo' in the completion list. Instead I'm offered a list of all types *and* all properties in Foo. This makes it really hard to see which properties can be set.
Comment 1 Mike Krüger 2012-05-10 10:40:27 UTC
It's not that easy in this context it's not easy to decide if you are doing an object or array initializer. And in the latter case you need the full list.
Comment 2 Mikayla Hutchinson [MSFT] 2012-05-10 12:35:50 UTC
Isn't the problem with that test case that it's incorrect?

This would be an object initialiser:
var data = new Foo () {

If Foo is IEnumerable and has an Add method(s), then it could also be a collection initializer. MD would know the allowed types, so could filter subtypes in a category. Maybe it could also put the properties in a category.

This would be a collection initializer:
var data = new Foo () {
    { $

This would be an array initializer, and filtered subtypes (or implicitly convertable types) could be categorized:
var data = new Foo[] {

And so would this:
string[] data = new [] {

And this:
string[] data = {

See also http://stackoverflow.com/a/5678393/116899
Comment 3 Alan McGovern 2012-05-10 12:44:56 UTC
Would it not be possible to check to see if the class implements IEnumerable<T> and also has an Add (T) method. If that is *not* true, the it must be an object initializer. If it is true, then it is ambiguous and the current behaviour would be best.
Comment 4 Mike Krüger 2012-05-10 17:20:22 UTC

This is valid:
var data = new Foo () {
   element1, ..., elementn
- there doesn't need to be an object initializer in this context.

I thought I already check for the add methods in that case, I need to look at it.
Comment 5 Mike Krüger 2012-05-18 05:14:37 UTC