question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SA1118 doesn't allow to write anonymous object (or object with property initialization) on multiple lines.

See original GitHub issue

This code:

          Test(
                "as",
                new
                {
                    foo = 1, bar = 2
                });

and this:

          Test(
                "as",
                new MyObject
                {
                    Foo = 1
                });

for this method:

        public void Test(string test, object obj)
        {
        }

gives SA1118 warnig when in StyleCop Classic it didn’t. I’m migrating from classic StyleCop and had to remove this rule because of that. I think it should be fixed.

BTW Thanks for working on this projects! It’s great that we can use StyleCop rules and use C#6 😃

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
sharwellcommented, Sep 13, 2017

Considering that object initializers would need to be indented when they span multiple lines, accepting them as arguments should work well with our existing handling of parameters.

0reactions
paulomorgadocommented, Jan 17, 2017

The rule was created before the feature was added to the language and doesn’t make sense if taken literally.

I think the correct implementation should be taking the whole object initialization as if it was only one line.

The documentation does not cover this case or nested method calls.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does C# allow to assign anonymous object to class ...
The object name becomes redundant as it is already known with the use of the object initializer syntax. Share. Share a link to...
Read more >
Instantiating anonymous object using C# object initializer ...
csc.exe creates a class with private fields and a read/write property with the type inferred from context. All uses of the object are,...
Read more >
Object Initializers: Named and Anonymous Types
For anonymousCust , the compiler defines a new class that has one property, a string called Name , and creates a new instance...
Read more >
Anonymous Types
You create anonymous types by using the new operator together with an object initializer. For more information about object initializers, see ...
Read more >
Projection Initializers | 2,000 Things You Should Know About C#
A projection initializer is a declaration for a member of an anonymous type that does not include a property name. Here's an example,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found