小编Dav*_*don的帖子

c#重构两个几乎相同的方法

重构是好的,但有时候解决如何重构并确实是否可以实际重构某些内容并不容易!

我有许多几乎相同的方法 - 我可以重构它们,但重构的一部分超出了我的逻辑.

这是两个未重构的方法:

 private void projectToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (projectToolStripMenuItem.Checked)
        {
            projectToolStripMenuItem.Checked = false;
            if (!projectForm.IsDisposed) projectForm.Hide();
        }
        else
        {
            if (projectForm.IsDisposed)
                projectForm = new frmProject();
            projectForm.Show(dockPanel, DockState.DockRight);
            projectToolStripMenuItem.Checked = true;
        }

    }

    private void logginToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (logginToolStripMenuItem.Checked)
        {
            logginToolStripMenuItem.Checked = false;
            if (!outputForm.IsDisposed) outputForm.Hide();
        }
        else
        {
            if (outputForm.IsDisposed)
                outputForm = new frmOutput();
            outputForm.Show(dockPanel, DockState.DockBottom);
            logginToolStripMenuItem.Checked = true;
        }
    }
Run Code Online (Sandbox Code Playgroud)

使用Refactoring,我会得到一个这样的方法,以前的未重构方法会调用

private void refactoredMethod(TooStripMenuItem menuItem, DockContent frmName)
{

        if (menuItem.Checked)
        { …
Run Code Online (Sandbox Code Playgroud)

c# refactoring

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

WebRole和WorkerRole模板在VS 2015中丢失

我已经远远地看了解答案.在Visual Studio 2013中,我可以轻松地创建WebRole和WorkerRole解决方案,但在VS 2015中,我似乎只能创建一个网站和WebJob.

WebRole和WorkerRole已被删除吗?如果没有,你可以向我解释它在哪里 - 或者为什么它会丢失?

azure-worker-roles azure-web-roles azure-webjobs visual-studio-2015

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

C#空传播 - 魔法在哪里发生?

空传播是一个非常好的功能 - 但实际魔法在哪里以及如何发生?哪里frm?.Close()得到改变if(frm != null) frm.Close();-它是否真正得到改变那种代码呢?

c# null propagation

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

node-restful 不将文档“内容”保存到 MongoDb

我正在尝试使用 NodeJS 将文档保存到 MongoDb。我正在使用 Express、Mongoose、Node-Restful 为我完成这些艰苦的工作。但是,尽管它确实在 MongoDb 中创建了文档,但它们并不是实际内容。我只得到

[ { "_id": "5460ab83c9e8f9481063df82", "__v": 0 }, { "_id": "5460ac00c9e8f9481063df83", "__v": 0 }, { "_id": "5460ae, "df06,df80c"{0} "__v": 0 }, { "_id": "5460afb6be7938bc1469ae97", "__v": 0 }, { "_id": "5460fe9088d68e2516cdf572", "__v": 0 }, { "_id": "3945bcf"5946 ": 0 }, { "_id": "5460fed5d47cba3916fba0ac", "__v": 0 }, { "_id":"5460fed8d47cba3916fba0ad", "__v": 0 }, { "_id": "5460ff079b60285016dfd0da", "__v": 0 } ]

如您所见,实际内容并未被保存,出于测试目的,这只是一个名称。

我的架构 (models/users.js)

var restful = require('node-restful');
var mongoose = restful.mongoose;

var userSchema = new mongoose.Schema({ …
Run Code Online (Sandbox Code Playgroud)

javascript rest mongoose mongodb node.js

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

Chrome扩展程序无法读取属性在contextMenus.create中创建未定义

这是我的contextMenus.create函数,它在未定义的错误中抛出了 create的无法读取属性.

chrome.contextMenus.create({
   "title": "Buzz This",
   "contexts": ["page", "selection", "image", "link"],
   "onclick" : clickHandler
   });
Run Code Online (Sandbox Code Playgroud)

我也在相同的内容脚本中有这个:

chrome.contextMenus.onClicked.addListener(onClickHandler);

// The onClicked callback function.
function onClickHandler(info, tab) {
     window.alert(info.srcUrl);    
};
Run Code Online (Sandbox Code Playgroud)

这是我的manifest.json

{
  "name": "ReportIt",
  "version": "0.0.1",
  "manifest_version": 2,

  "default_locale": "en",
  "description": "Immediately Remove and Report",
  "icons": {
   "16": "images/icon-128.png",
   "128": "images/icon-128.png"
   },


   "content_scripts": [{
    "matches": ["<all_urls>"],
    "js": ["scripts/contentscript.js"],
    "run_at": "document_end",
    "all_frames": false
    }],

      "permissions": [
      "http://*/*",
      "https://*/*",
      "contextMenus"
      ],

      "content_security_policy": "script-src 'self'; object-src 'self'",
      "web_accessible_resources": 
      [
      "bower_components/angular/*",
      "scripts/background.js"
      ] …
Run Code Online (Sandbox Code Playgroud)

javascript google-chrome-extension

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

c#有更好的方法来验证用户输入

我有以下代码:

private void btnOK_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(tbVendorName.Text))
        {
            VendorName = tbVendorName.Text;
            if (!string.IsNullOrEmpty(rtbVendorAddress.Text))
            {
                VendorAddress = rtbVendorAddress.Text;
                if (!string.IsNullOrEmpty(tbVendorEmail.Text))
                {
                    VendorEmail = tbVendorEmail.Text;
                    if (!string.IsNullOrEmpty(tbVendorWebsite.Text))
                    {
                        VendorWebsite = tbVendorWebsite.Text;
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("Vendor Website Required");
                    }
                }
                else
                {
                    MessageBox.Show("Vendor email is required");
                }
            }
            else
            {
                MessageBox.Show("Vendor address is required");
            }
        }
        else
        {
            MessageBox.Show("Vendor name is required");
        }
    }
Run Code Online (Sandbox Code Playgroud)

但它看起来很可怕.有没有更好的办法?或者甚至是一种使代码更具可读性的替代方法?

c# validation if-statement

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

返回myVar!= null实际上是什么意思?

Resharper是一个很棒的工具,但它有时让我对建议的代码真正含义感到困惑.我有这个代码:

private bool DoesUserExists()
{
    var user = De.Users.FirstOrDefault(u => u.Username == CurrentUser.Username);
    return user != null;
}
Run Code Online (Sandbox Code Playgroud)

我最初有:

if(user == null)
    return false;
else
    return true;
Run Code Online (Sandbox Code Playgroud)

但是Resharper建议使用顶级代码.但是,如果它不是null,那我认为是返回用户.但该方法只接受bool返回而不是类.

那么返回user!= null实际上返回的是null,当它不是?

c# resharper refactoring return-type

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

我在哪种方法?

我的目标是在我的方法中添加一些#if DEBUG但是我不想编辑我复制的代码并粘贴到每个方法中.

是否有像这样的通用代码:

void DoSomething()
            {
#if Debug
            Log("Now In " + MethodName);
#endif
            }
Run Code Online (Sandbox Code Playgroud)

在哪里填充MethodName等于DoSomething,或者哪个方法称为日志?

c# debugging methods winforms

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

任务中的计时器未触发

从任务调用以下代码 t = new task(() => StartScanAsync(path)); 但计时器事件没有触发,计时器在 StartTimer 中启动。

private void StartScanAsync(string path)
{
    StartTimer();
    foreach (string fsPath in Alphaleonis.Win32.Filesystem.Directory.EnumerateFileSystemEntries(path, "*", SearchOption.AllDirectories, true))
    {
        Alphaleonis.Win32.Filesystem.FileInfo finfo = new Alphaleonis.Win32.Filesystem.FileInfo(fsPath);
        fileClass.AddFile(finfo);
        //  UpdateStatus(fsPath);
        UpdateTotalFilesScanned();
        TotalFileSize += finfo.Length;
        UpdateTotalFileSize(TotalFileSize);
        IdentifyFileType.FileType ft = FileType.LoadPathRet(fsPath);
        //  Application.DoEvents();
    }
    UpdateStatus("Done Scanning.");
    StopTimer();
}
Run Code Online (Sandbox Code Playgroud)

我也尝试了 Parallel.ForEach 并遇到了同样的问题。

以下代码应该启动一个计时器,在一个大集合上运行Parrallel.ForEach,然后停止计时器。但是,计时器事件没有被触发...有什么想法以及如何解决这个问题吗?

StartTimer();
Parallel.ForEach(Alphaleonis.Win32.Filesystem.Directory.EnumerateFileSystemEntries(path, "*", SearchOption.AllDirectories), fsPath =>
{
    Alphaleonis.Win32.Filesystem.FileInfo finfo = new Alphaleonis.Win32.Filesystem.FileInfo(fsPath);
    fileClass.AddFile(finfo);
    UpdateStatus(fsPath);
    UpdateTotalFilesScanned();
    TotalFileSize += finfo.Length;
    UpdateTotalFileSize(TotalFileSize);
    // IdentifyFileType.FileType ft = FileType.LoadPathRet(fsPath);
    Application.DoEvents();
});
StopTimer(); …
Run Code Online (Sandbox Code Playgroud)

c# timer task parallel.foreach

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

dd 标签中的 Bootstrap 缩进没有按预期工作

我正在为一个慈善机构设计(希望我是一名设计师)一个网页 - 章程基本上是我决定在标签中做的标题和信息的列表。但是,我似乎无法让元素按照我希望它们的方式运行,而子项的缩进比它们的父元素更远。

不正确的缩进

如您所见,第 4 项很好,但第 4.1 项应进一步缩进,并且该子项 (4.1.1) 中的任何子项 (4.1.1) 仍应进一步缩进。

我尝试使用 css 并创建两个 text-indent-n 样式,如下所示

.text-indent-4{
    text-indent: 50px;
}

.text-indent-8{
    text-indent: 100px;
}
Run Code Online (Sandbox Code Playgroud)

像这样应用时没有区别:

<dt data-toggle="tooltip" data-placement="top" title="Register of members">Register of members</dt>
  <dd>17. The board must keep a register of members, setting out </dd>
  <dd  class="text-indent-4">17.1   for each current member:</dd>
  <dd  class="text-indent-8">17.1.1 their full name and address; and</dd>
  <dd  class="text-indent-8">17.1.2 the date on which they were registered as a member of the organisation; </dd>
  <dd  class="text-indent-4">17.2   for each …
Run Code Online (Sandbox Code Playgroud)

html css dd indentation twitter-bootstrap

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