小编Pep*_*epo的帖子

Visual Studio 中的圈复杂度

我使用 Visual Studio Code Metrics 做了一些测试。由于我可以计算圈复杂度,因此每个if, while, for- 运算符都会将复杂度增加 1。我有下一个简单的方法:

static bool ContainsNegative(int a, int b, int c, int d)
    {
        if (a < 0 || b < 0 || c < 0 || d < 0) return false;
        return true;
    }
Run Code Online (Sandbox Code Playgroud)

但对于它来说,圈复杂度是 5,而不是 2(1 表示方法 + 1 表示if)。我的问题是 - 这是因为代码指标将if运算符中的每个条件计算为不同的if?即我的方法相当于:

static bool ContainsNegative(int a, int b, int c, int d)
    {
        if (a < 0) return false;
        if (b …
Run Code Online (Sandbox Code Playgroud)

c# cyclomatic-complexity code-metrics visual-studio

6
推荐指数
1
解决办法
6456
查看次数

Json.NET 不同的 json 结构,基于枚举值

我需要将我的类转换为 JSON 并使用 Json.NET。但我可以有不同的 JSON 结构,例如:

{
    name: "Name",
    type: "simple1",
    value: 100
};
Run Code Online (Sandbox Code Playgroud)

或者

{
    name: "Name",
    type: {
        optional1: {
            setting1: "s1",
            setting2: "s2",
            ///etc.
    },
    value: 100
};
Run Code Online (Sandbox Code Playgroud)

我的 C# 代码是:

public class Configuration
{
    [JsonProperty(PropertyName = "name")]
    public string Name{ get; set; }

    [JsonProperty(PropertyName = "type")]
    public MyEnumTypes Type { get; set; }

    public OptionalType TypeAdditionalData { get; set; }

    [JsonProperty(PropertyName = "value")]
    public int Value { get; set; }
    public bool ShouldSerializeType()
    {
        OptionalSettingsAttribute optionalSettingsAttr …
Run Code Online (Sandbox Code Playgroud)

.net c# serialization json json.net

5
推荐指数
1
解决办法
938
查看次数