-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from AArnott/nbmsgpack003
Add NBMsgPack003 analyzer: `[Key]` index must be unique
- Loading branch information
Showing
7 changed files
with
162 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# NBMsgPack003: `[Key]` index must be unique | ||
|
||
@Nerdbank.MessagePack.KeyAttribute must have unique indexes provided across all members of a type, including base types. | ||
|
||
This is because for a given object, the indexes determine the array index that will contain the value for a property. | ||
|
||
## Example violations | ||
|
||
Non-unique within a class: | ||
|
||
```cs | ||
class MyType | ||
{ | ||
[Key(0)] | ||
public int Property1 { get; set; } | ||
|
||
[Key(0)] | ||
public int Property2 { get; set; } | ||
} | ||
``` | ||
|
||
Non-unique across a type hierarchy: | ||
|
||
```cs | ||
class BaseType | ||
{ | ||
[Key(0)] | ||
public int BaseProperty { get; set; } | ||
} | ||
|
||
class DerivedType : BaseType | ||
{ | ||
[Key(0)] // This must be a different index than BaseType.BaseProperty | ||
public int DerivedProperty { get; set; } | ||
} | ||
``` | ||
|
||
## Resolution | ||
|
||
Reassign indexes so that each member has a unique value assigned. | ||
When `[Key]` is used across a type hierarchy, lower indexes are typically assigned to base types and higher indexes to derived types. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,5 @@ | |
href: NBMsgPack001.md | ||
- name: NBMsgPack002 | ||
href: NBMsgPack002.md | ||
- name: NBMsgPack003 | ||
href: NBMsgPack003.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters