小编Neb*_*ula的帖子

C#操作JSON数据

我有一个"简单"的场景:读取一些JSON文件,过滤或更改一些值并将结果写回json,而不更改原始格式.

所以例如改变这个:

{
  "type": "FeatureCollection",
  "crs": {
    "type": "EPSG",
    "properties": {
      "code": 28992
    }
  },
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              149886.192,
              374554.705
            ],
            [
              149728.583,
              374473.112
            ],
            [
              149725.476,
              374478.215
            ]
          ]
        ]
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

进入:

{
  "type": "FeatureCollection",
  "crs": {
    "type": "EPSG",
    "properties": {
      "code": 28992
    }
  },
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": 
            [
              149886.192,
              374554.705
            ]
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试过newtonsoft等JSON.Net,但我能找到的只有:

  • 读入对象
  • 将对象写入json …

c# json json.net

17
推荐指数
1
解决办法
2万
查看次数

在派生类中重载基方法

所以我正在玩C#以查看它是否与此帖子中的C++行为相符:http://herbsutter.com/2013/05/22/gotw-5-solution-overriding-virtual-functions/ 当我遇到这个非常奇怪的时候行为:

public class BaseClass
{
    public virtual void Foo(int i)
    {
        Console.WriteLine("Called Foo(int): " + i);
    }

    public void Foo(string i)
    {
        Console.WriteLine("Called Foo(string): " + i);
    }
}

public class DerivedClass : BaseClass
{
    public void Foo(double i)
    {
        Console.WriteLine("Called Foo(double): " + i);
    }
}

public class OverriddenDerivedClass : BaseClass
{
    public override void Foo(int i)
    {
        base.Foo(i);
    }

    public void Foo(double i)
    {
        Console.WriteLine("Called Foo(double): " + i);
    }
}

class Program
{ …
Run Code Online (Sandbox Code Playgroud)

c# inheritance overloading

12
推荐指数
1
解决办法
3146
查看次数

如何在 gitlab CI 中检测编译器警告

在我们的 gitlab 服务器上设置 CI 构建的步骤中,我似乎找不到有关如何设置编译器警告检测的信息。构建输出示例:

[100%] Building CXX object somefile.cpp.o
/home/gitlab-runner/builds/XXXXXXX/0/group/project/src/somefile.cpp:14:2: warning: #warning ("This is a warning to test gitlab") [-Wcpp]
 #warning("This is a warning to test gitlab")
 ^
Run Code Online (Sandbox Code Playgroud)

然而,构建结果是success相反的warning或类似的。理想情况下,结果在功能的合并请求中也可见(如果可能的话,阻止合并)。

我无法想象我是唯一一个试图实现这一目标的人,所以我可能正在寻找错误的方向。我发现的“最佳”解决方案是以某种方式手动解析构建输出并生成 JUnit 报告。

我将如何在不让构建作业失败的情况下执行此操作,因为我希望在发生编译器错误时作业失败。

更新

对于后来偶然发现这个问题的任何人,代替最佳实践,这就是我解决它的方法:

stages:
  - build
  - check-warnings

shellinspector:
  stage: build
  script:
    - cmake -Bcmake-build -S.
    - make -C cmake-build > >(tee make.output) 2> >(tee make.error)
  artifacts:
    paths:
      - make.output
      - make.error
    expire_in: 1 week

analyse build:
  stage: check-warnings
  script:
    - "if [[ …
Run Code Online (Sandbox Code Playgroud)

gcc compiler-warnings gitlab-ci

8
推荐指数
1
解决办法
5278
查看次数

使用不带Invoke的MethodInvoker

我现在正在编写GUI应用程序一段时间,我总是使用一个方法是使用MethodInvoker + lambda函数来进行跨线程访问.

从示例中我发现我总是看到这样的东西:

版本1

if (InvokeRequired)
{
    Invoke(new MethodInvoker(() => 
    {
        Label1.Text = "Foobar";
    });
}
else
{
    Label1.Text = "Foobar";
}
Run Code Online (Sandbox Code Playgroud)

然而,这会导致代码重复 - >对我来说是主要的坏人.

那有什么不对呢?

版本2

MethodInvoker updateText = new MethodInvoker(() => 
    {
        Label1.Text = "Foobar";
    });

if (InvokeRequired)
{
    Invoke(updateText);
}
else
{
    updateText();
}
Run Code Online (Sandbox Code Playgroud)

现在我将功能捆绑在一个变量中,并在适当时使用Invoke或函数指针调用它.版本2的性能更差吗?或者我使用匿名函数是不好的做法?

c# lambda user-interface multithreading

4
推荐指数
1
解决办法
5568
查看次数

C#方法返回lambda中的变量

所以我正在与一些LINQ(实体框架)lambda进行斗争,我最终通过将结果存储在变量中来解决这个问题.就像是:

IQueryable<object> DoStuff() { return /* some linq query */; }

void Main()
{
    var result = DoStuff();
    // (_database is DbContext) == true

    // Works fine
    _database.Table.Where(x => result.Contains(x.Id));

    // Throws "LINQ to Entities does not recognize the method '[...] DoStuff()' method, and this method cannot be translated into a store expression."
    _database.Table.Where(x => DoStuff().Contains(x.Id));
}
Run Code Online (Sandbox Code Playgroud)

我能够通过将查询存储在一个单独的变量中来解决我的问题(偶然),但我很好奇为什么这样做?这是延迟执行的事吗?在此之前,我一直认为两个可交换的方法(方法与中间变量用法)用于引用类型,但显然它们不是.

c# variables methods lambda

2
推荐指数
2
解决办法
217
查看次数