小编Van*_*s S的帖子

C#通过添加属性来扩展类

在C#中是否可以扩展一个类,而不仅仅是添加函数而是属性.例如:我有一个我依赖的标准DLL库,供应商不想修改它.

在整个代码中我已经广泛使用了DataCell类,现在才意识到我需要为它添加一个额外的属性,因为创建一个继承自这个类的新扩展类看起来不会起作用+大量的重写.

DataCell [元数据]

public class DataCell : Message
{
public int Field1;
public int Field2;
public DataCell()
{
 ..
} 
..
}
Run Code Online (Sandbox Code Playgroud)

基本上我想添加一个public int Flags; 到这堂课.所以我现在可以做而不重写任何东西,(新的DataCell).Flags = 0x10;

c# oop

10
推荐指数
3
解决办法
5万
查看次数

找到坐标2d数组的中心点c#

是否有公式来平均所有x,y坐标并找到它们死点的位置.

我有100x100的正方形,里面是1x1红色和黑色点的大块,我想确定一个位于中间的红点.

我查看了最合适的公式,但我不确定这是否是我需要的.

有时所有的红色都在一侧或另一侧.我想基本上绘制一条线然后找到该线的中心点,或者只找到红色方块的中心点.基于100x100网格.

c# algorithm math center

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

Visual Studio 2010 C++/w Google协议缓冲区.找不到60个外部.无法编译

我尽我所能遵循指示.下载的GPBuffers 5.0rc1将src/google文件夹放入我的项目目录中.添加了设置> C/C++>"其他包含目录"的路径.

但我认为我需要在某处放置-lprotobuf编译器选项.我尝试将它添加到设置> xxx>附加命令行参数中的C/C++和链接器.

但没有运气.

似乎缺少libprotobuf.lib,libprotobuf-lite.lib和libprotoc.lib.也许它没有正确构建..

我有我的serverThread.cpp,它看起来像:

#include "serverThread.h"
#include "sendStruct.h"

#include <fstream>
#include <string>
#include "messages.pb.h"
Run Code Online (Sandbox Code Playgroud)

该日志转载如下.

它似乎缺少对某些库的引用.因为大多数依赖都在那里.

Error   62  error LNK1120: 60 unresolved externals  C:\work\freetimeC+\ServerUpdater\Debug\ServerUpdater.exe
Error   37  error LNK2001: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const google::protobuf::internal::kEmptyString" (?kEmptyString@internal@protobuf@google@@3V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@B)   C:\work\freetimeC+\ServerUpdater\ServerUpdater\messages.pb.obj
Error   34  error LNK2001: unresolved external symbol "private: virtual void __thiscall google::protobuf::Message::SetCachedSize(int)const " (?SetCachedSize@Message@protobuf@google@@EBEXH@Z)  C:\work\freetimeC+\ServerUpdater\ServerUpdater\messages.pb.obj
Error   27  error LNK2001: unresolved external symbol "public: virtual bool __thiscall google::protobuf::Message::IsInitialized(void)const " (?IsInitialized@Message@protobuf@google@@UBE_NXZ)  C:\work\freetimeC+\ServerUpdater\ServerUpdater\messages.pb.obj
Error   28  error LNK2001: unresolved external …
Run Code Online (Sandbox Code Playgroud)

c++

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

Array.Resize(..)线程安全吗?

在一个用作SAEA线程安全缓冲区的数组上调用Array.Resize?不同的线程都写入它们自己分配的数组部分,我只想在连接用户增加时初始化大小耗尽后使数组更大而不锁定.

byte[] buffer; //Accessed
object expand_Lock = new object();

public void AsyncAccept()
{
    //Lock here so we don't resize the buffer twice at once
    lock(expand_Lock)
    {
        if (bufferFull)
        {
            Array.Resize(buffer, buffer.Length + 2048 * 100); //Add space for 100 more args
            //Is Array.Resize threadsafe if buffer can be read/wrote to at anytime?
            AssignMoreSAEA(2048, 100); //irrelevant to question what this does
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c#

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

线程问题> 30个线程.CPU非线性缩放

我的C#应用​​程序遇到了一些问题.

我确保线程不会访问自己以外的任何资源.

现在我有一个线程池线程,它创建一个tcp连接,创建线程对象并运行,1线程性能很好.有50个线程似乎相同,可能慢5-10%,CPU 10-20%.使用100个线程时,CPU使用率从10-20%增加到70-99%.

我们的一位开发人员表示,与Linux线程相比,Windows线程很糟糕,而且上下文切换会导致巨大的惩罚.他建议使用运行所有实例的4-8个核心线程创建多路复用.

但是我认为一旦你有1000多个线程,这样的问题就会开始发生.任何人都可以用一些好的资源来评论更多关于这个主题,以及关于线程/ CPU性能和正确的做法?


编辑:好的许多答案似乎有点偏离,因为正在做一些假设,所以我将添加一些额外的点:

 Running 3 applications with 50 threads at 10-20% cpu usage  makes them all use that much. 30-60% CPU usage total.
 Running 1 application with 150 threads makes it cap cpu at 70-99%.  
Run Code Online (Sandbox Code Playgroud)

这就是我所说的线程不缩放.

c# multithreading

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

Proto Buffers不以可读格式存储数据

我将我的protobuf消息保存到文件,格式全部搞砸了.我已经看到它在protobug消息以与.proto文件几乎相同的格式保存到磁盘之前完成.我这样做:

using (Stream output = File.OpenWrite(@"logs\listings.txt"))
{          
    listingBook.AddClisting(_listing);
    listingBook.Build().WriteTo(output);
}
Run Code Online (Sandbox Code Playgroud)

但我得到的是一个错误的文件,似乎ENTER与奇怪的标签分开.将它保存到磁盘时我想要的样子就像示例:

# Textual representation of a protocol buffer.
# This is *not* the binary format used on the wire.
person {
  name: "John Doe"
  email: "jdoe@example.com"
}
Run Code Online (Sandbox Code Playgroud)

c# protocol-buffers

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

Nodejs传递res.end()作为回调不起作用

您好我正在使用连接并具有:

var app = connect()
 .use(connect.static(directory))
 .use(function(req,res) {
  switch (req.url)
  {
   case '/query':
    exportFunc.query("myquery", res.end);
    break;
  }
}

-- exportFunc.js --
exports.query= function(query, cb) {
 queryServer(query, cb);
}

var queryServer = function(query, cb) {
 cb("MyQueryResult");
}
Run Code Online (Sandbox Code Playgroud)

但我得到一个错误,cb无法确定它是什么对象.我需要将此对象转换为正确的类型吗?

connect node.js

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

找到2个坐标的区域

如何找到由2个二维向量形成的矩形区域.例如:p1 =(20,40); p2 =(30,60);

我需要找到这种形式的矩形.有一个常用的配方吗?

c# points

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

C++从文件中读取int和字符串

我有一个看起来像这样的文件:

4
Sam Stone
2000
Freida Flass
100500
Tammy Flass
5000
Rich Raptor
55000
Run Code Online (Sandbox Code Playgroud)

我试图从中读取,但是while循环中的第一个getline总是不返回任何内容.int 4被正确读取.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <string>

using namespace std;

const int SIZE = 60;


struct person
{
    string name;
    double money;
};

int main()
{
    char filename[SIZE];
    string input;
    char inputs [50];
    int value;
    int count = 0;
    vector<person> Members;

    ifstream inFile;
    inFile.open("carinfo.txt");
    if (!inFile.is_open()){ cout << "Could not open fle"; }

    inFile >> value;
    Members.resize(value);

    while (inFile.good())
    {
        inFile.getline(inputs, …
Run Code Online (Sandbox Code Playgroud)

c++ string

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

Delphi到ASM - 如何在ASM中创建对象?

我在delphi中有一个对象,看起来像:

  object DXDraw: TDXDraw
    Left = 0
    Top = 0
    Width = 640
    Height = 360
    AutoInitialize = True
    AutoSize = False
    Display.BitCount = 16
    Display.FixedBitCount = True
    Display.FixedRatio = True
    Display.FixedSize = False
    Options = [doFullScreen, doAllowReboot, doWaitVBlank, doHardware, 
        doRetainedMode, doDrawPrimitive, doSelectDriver, doTexture, 
        doRGB, doMono]
    SurfaceHeight = 360
    OnInitialize = DXDrawInitialize
    TabOrder = 1
    Visible = False
    OnClick = LbBackScreenClick
    OnDblClick = DXDrawDblClick
    OnDragDrop = FormDragDrop
    OnDragOver = FormDragOver
    OnKeyPress = EdFocusKeyPress
    OnMouseDown = LbBackScreenMouseDown
    OnMouseMove = LbBackScreenMouseMove
    OnMouseUp …
Run Code Online (Sandbox Code Playgroud)

delphi assembly reverse-engineering

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