SA1118 doesn't allow to write anonymous object (or object with property initialization) on multiple lines.
See original GitHub issueThis 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:
- Created 8 years ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
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.