小编Fro*_*art的帖子

如何将C#泛型类型传递给另一个类

有没有办法在C#中编写以下代码?

public T GetRandomRecord<T>(DbSet<T> set)
{
    return set.OrderBy(r => Guid.NewGuid()).FirstOrDefault();
}
Run Code Online (Sandbox Code Playgroud)

它给了我以下错误:

错误CS0452类型'T'必须是引用类型才能在泛型类型或方法'DbSet'中将其用作参数'TEntity'

DbSet是的Microsoft.EntityFrameworkCore.DbSet.

我该如何解决?

c# generics

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

如何“加入”字典元素

假设我有以下字典:

var d = {
  'foo': 0,
  'bar': 1
};
Run Code Online (Sandbox Code Playgroud)

我如何轻松地将其转换为这样的字符串?

foo=0&bar=1
Run Code Online (Sandbox Code Playgroud)

有内置的方法可以帮助我吗?

javascript

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

缩小时对象消失

有没有办法防止Fabric.js对象在缩小时消失?

看看我刚刚创建的以下JSFiddle - https://jsfiddle.net/t3jkrmn6/.只需滚动画布,你就会看到我在说什么.

<div id="canvas-wrapper">
  <canvas id="c" class="canvas"></canvas>
</div>

.canvas {
  border: 1px solid #999;
}

var canvas = new fabric.Canvas('c');
var line = new fabric.Line([100, 50, 100, 100], {
    stroke: 'black',
  strokeWidth: 1
});
canvas.add(line);

$('#canvas-wrapper').mousewheel(function(e) {
  if (e.originalEvent.deltaY < 0) {
    canvas.setZoom(canvas.getZoom() * 1.1);
  } else {
    canvas.setZoom(canvas.getZoom() / 1.1);
  }
  console.log(canvas.getZoom());
});
Run Code Online (Sandbox Code Playgroud)

无论用户缩小画布多少,我都希望我的一些对象保持1个宽度.

javascript fabricjs

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

从通过std :: async启动的函数抛出的异常会发生什么

从通过启动的函数抛出的异常会发生什么std::async

#include <future>
#include <iostream>
#include <stdexcept>

void foo()
{
  std::cout << "foo()" << std::endl;
  throw std::runtime_error("Error");
}

int main()
{
  try
  {
    std::cout << "1" << std::endl;
    auto f = std::async(std::launch::async, foo);
    f.get();
    std::cout << "2" << std::endl;
  }
  catch (const std::exception& ex)
  {
    std::cerr << ex.what() << std::endl;
  }
}
Run Code Online (Sandbox Code Playgroud)

MSVC给我以下输出:

1
foo()
Error
Run Code Online (Sandbox Code Playgroud)

是否通过调用get函数将这些异常捕获到函数外部?

c++ multithreading c++11

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

C99和C11中的const关键字和常量表达式

我们真的可以在C99和C11中使用const关键字来构建这样的常量表达式吗?标准说的是什么?

const int n = 5;

int main()
{
   int arr[n];
}
Run Code Online (Sandbox Code Playgroud)

在C89/C90我们不能.

c

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

字符串文字中的反斜杠字符

为什么以下程序的输出:

str                 str
Run Code Online (Sandbox Code Playgroud)

而不是这个:

str str
Run Code Online (Sandbox Code Playgroud)

这是该计划:

#include <iostream>

int main()
{
   std::cout << "str \
                str \n";
}
Run Code Online (Sandbox Code Playgroud)

我尝试了gcc 4.7.2和MSVC-11.0.

c c++

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

MtGox API和websocketpp

我无法通过名为websocketpp的 WebSockets C++库从MtGox API获取信息:

#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>

#include <iostream>

typedef websocketpp::client<websocketpp::config::asio_client> client;

using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;

typedef websocketpp::config::asio_client::message_type::ptr message_ptr;

void on_open(websocketpp::connection_hdl hdl)
{
    std::cout << "on_open \n";
}

void on_close(websocketpp::connection_hdl hdl)
{
    std::cout << "on_close \n";
}

void on_message(client* c, websocketpp::connection_hdl hdl, message_ptr msg)
{
    std::cout << msg->get_payload() << '\n';
}

int main()
{
    client c;

    try
    {
        c.init_asio();

        c.set_open_handler(on_open);
        c.set_close_handler(on_close);
        c.set_message_handler(bind(&on_message, &c, ::_1, ::_2));

        websocketpp::lib::error_code ec;
        client::connection_ptr con = c.get_connection("ws://websocket.mtgox.com:80/mtgox?Currency=EUR", ec);
        c.connect(con);

        c.run();
    }
    catch …
Run Code Online (Sandbox Code Playgroud)

c++ websocket websocket++

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

我应该选择期权还是隐式解包期权

假设您需要编写将闭包作为参数之一的函数来将其称为回调.该函数的用户应该能够传递nil而不是关闭.在这种情况下,您会使用期权还是隐式解包期权?

提前致谢.

optional ios swift

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

指定事务级别的Postgres dblink存储过程调用

我想以这种方式在PL / pgSQL存储过程中使用dblink:

PERFORM dblink_exec('myconn', 'BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE');
PERFORM dblink_exec('myconn', 'SELECT another_stored_procedure()');
PERFORM dblink_exec('myconn', 'COMMIT');
Run Code Online (Sandbox Code Playgroud)

但是在运行时出现错误:

ERROR:  statement returning results not allowed
CONTEXT:  SQL statement "SELECT dblink_exec('myconn', 'select another_stored_procedure()')"
Run Code Online (Sandbox Code Playgroud)

因此执行失败,尽管我尝试以其他方式获得所需的结果。

更新1:

我知道Postgresql中的存储过程是事务性的。我将dblink用于自治事务功能以在同一服务器上使用它。

问题是服务器上的默认事务级别是“读已提交”,但有时我需要从另一个级别开始事务,例如“可序列化”。

因此,我需要在具有明确事务级别指定的自主事务中执行存储过程。

据我所知dblink允许这样做,但是我找不到适合我情况的有关dblink或dblink_exec函数的任何有用信息。

postgresql dblink plpgsql

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

事件处理程序是否保证在 UI 线程上运行

事件句柄是这样的吗

notifyIcon.BalloonTipClosed += new EventHandler(delegate(Object sender, EventArgs e)
{
     // ...
});
Run Code Online (Sandbox Code Playgroud)

保证在 UI 线程上运行,所以我不需要调用 InvokeBeginInvoke方法来更新控件?

提前致谢。

c# winforms

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