小编Art*_*ode的帖子

为什么这个字符串扩展方法不会抛出异常?

我有一个C#字符串扩展方法,它应返回IEnumerable<int>字符串中子字符串的所有索引.它完美地用于其预期目的,并返回预期的结果(由我的一个测试证明,虽然不是下面的一个),但另一个单元测试发现它有一个问题:它不能处理空参数.

这是我正在测试的扩展方法:

public static IEnumerable<int> AllIndexesOf(this string str, string searchText)
{
    if (searchText == null)
    {
        throw new ArgumentNullException("searchText");
    }
    for (int index = 0; ; index += searchText.Length)
    {
        index = str.IndexOf(searchText, index);
        if (index == -1)
            break;
        yield return index;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是标记问题的测试:

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void Extensions_AllIndexesOf_HandlesNullArguments()
{
    string test = "a.b.c.d.e";
    test.AllIndexesOf(null);
}
Run Code Online (Sandbox Code Playgroud)

当测试针对我的扩展方法运行时,它会失败,标准错误消息表明该方法"没有抛出异常".

这很令人困惑:我已明确传入null函数,但由于某种原因,比较null == null正在返回false.因此,不会抛出异常并且代码会继续.

我已经确认这不是测试的错误:在我的主项目中通过调用Console.WriteLinenull比较if块运行该方法时,控制台上没有显示任何内容,并且catch我添加的任何块都没有捕获到任何异常.而且,使用string.IsNullOrEmpty而不是== null …

c# comparison ienumerable null argumentnullexception

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

Visual Diagnostic已禁用或当前应用程序不支持

使用VS 2015 Enterprise,我会在调试时始终关注解决方案:XAML Visual Diagnostic已禁用或当前应用程序不支持

工具 - 选项 - 调试 - 常规:

  • 启用了为XAML启用UI调试工具
  • 已启用在实时可视树中预览选定元素

我试过了:

  • VS重启
  • 清洁解决方案/重建
  • 新鲜的结帐

好奇:如果我在没有调试的情况下启动解决方案并且我将调试器附加到进程,它可以工作......

更新: 如果我在项目设置中选择"启用本机代码调试" - 调试,它可以工作!

xaml visual-studio-2015

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

请求超出了10个内部重定向的限制

我的网站在最近几天放慢了速度.我查看了我的错误日志,发现了很多这些:

[Mon Sep 30 00:09:53 2013] [error] [client 66.249.66.205] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
[Mon Sep 30 00:09:53 2013] [debug] core.c(3120): [client 66.249.66.205] r->uri = /home/mysitecom/domains/mysite.com/public_html/index.php
[Mon Sep 30 00:09:53 2013] [debug] core.c(3126): [client 66.249.66.205] redirected from r->uri = /home/mysitecom/domains/mysite.com/public_html/index.php
[Mon Sep 30 00:09:53 2013] [debug] core.c(3126): [client 66.249.66.205] redirected from r->uri = /home/mysitecom/domains/mysite.com/public_html/index.php
[Mon Sep 30 …
Run Code Online (Sandbox Code Playgroud)

php apache error-handling .htaccess codeigniter

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

如何在动态链接中使用HAML?

我正在尝试使用看起来像这样的HAML创建一个链接

=link_to("Last updated on<%=@last_data.date_from.month %>",'/member/abc/def?month={Time.now.month}&range=xyz&year={Time.now.year}')
Run Code Online (Sandbox Code Playgroud)

它没有采用Ruby代码,而是将其显示为字符串

上次更新于<%= @ last_data.date_from.month%>

并在URL中也没有采取功能Time.now.monthTime.now.year.

如何在URL和字符串中传递Ruby代码?

ruby haml ruby-on-rails ruby-on-rails-3

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

如果没有解释C#,那么为什么需要VM呢?

我已经阅读了很多关于C#的争议,有些人说它被解释,有些人说它不是.我知道它被编译到MSIL中,然后在运行时进行JITed,具体取决于处理器等......但它是否仍然以它需要运行VM(.NET)的方式解释?

c# programming-languages

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

Bootstrap optgroup选择

我使用bootstrap选择插件与bootstrap 3.0.2,它满足我的大部分要求.
我想要的唯一附加功能是能够optgroup通过单击选择它.

例如,我想像任何其他选项一样选择Picnic(下图), 在此输入图像描述

Bootsrap选择页面

javascript twitter-bootstrap twitter-bootstrap-3

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

在.net世界中将JSON转换为BSON的最简单方法是什么?

我刚开始研究MongoDB.从我的JavaScript客户端我发送一个JSON字符串到ASP.NET WEB API项目.是否可以直接使用此JSON字符串并将其保存到MongoDB中?我也想知道这种方法是否有意义?

我正在考虑JSON从客户端传递并在服务器端读取stringas

 [System.Web.Mvc.HttpPost]
    public dynamic SaveData([FromBody] string data)
    {

        System.Web.HttpContext.Current.Request.Form[0]
        return null;
    }
Run Code Online (Sandbox Code Playgroud)

.net c# mongodb bson

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

JavaScript显示/隐藏边框/线条

我创建了一个交互式地图,当选择区域时,相关内容应该显示/隐藏,这是正常的.

链接到预览

我遇到的问题实际上是将区域/点的线路连接到相关的内容框.

截图

我将如何使用我已有的JavaScript来解决这个问题:

 <style>
/* Fonts */
@font-face {
    font-family: 'Newbaskn';
    src: url('fonts/Newbaskn.eot');
    src: url('fonts/Newbaskn.eot') format('embedded-opentype'),
         url('fonts/Newbaskn.woff2') format('woff2'),
         url('fonts/Newbaskn.woff') format('woff'),
         url('fonts/Newbaskn.ttf') format('truetype'),
         url('fonts/Newbaskn.svg#Newbaskn') format('svg');
}
@font-face {
    font-family: 'OpenSansLight';
    src: url('fonts/OpenSansLight.eot');
    src: url('fonts/OpenSansLight.eot') format('embedded-opentype'),
         url('fonts/OpenSansLight.woff2') format('woff2'),
         url('fonts/OpenSansLight.woff') format('woff'),
         url('fonts/OpenSansLight.ttf') format('truetype'),
         url('fonts/OpenSansLight.svg#OpenSansLight') format('svg');
}
.map h3{font-family: 'Newbaskn';text-transform:uppercase;color:#b3772c;font-size:13px;font-weight:normal;line-height:0px;}

.map table{font-family: 'OpenSansLight';font-size:13px;font-weight:normal;padding: 15px;}
p{text-align:left;font-family: 'OpenSansLight';}
</style>
<div class="map" align="center">
<img src="mandela-map.jpg" alt="map" width="500" height="431"  border="0">

<button title="University of Cape Town &#013;University of Western Cape" name="uct-btn" id="uct-btn" style=" cursor:pointer; position: relative; left: -420px; …
Run Code Online (Sandbox Code Playgroud)

html javascript css jquery

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

如何优雅地终止 BLOCKED 线程?

有很多地方可以优雅地终止 C# 线程。然而,它们依赖于循环或循环内执行的 if 条件,这假设该语句将被频繁执行;因此,当stop设置了 bool 标志时,线程会快速退出。

如果我有一个线程的情况不正确怎么办?就我而言,这是一个设置为从服务器接收数据的线程,该线程经常阻塞从输入流读取数据的调用,而尚未提供任何数据,因此它会等待。

这是问题循环中的线程:

    while (true)
        {
            if (EndThread || Commands.EndRcvThread)
            {
                Console.WriteLine("Ending thread.");
                return;
            }

            data = "";
            received = new byte[4096];

            int bytesRead = 0;
            try
            {
                bytesRead = stream.Read(received, 0, 4096);
            }
            catch (Exception e)
            {
                Output.Message(ConsoleColor.DarkRed, "Could not get a response from the server.");
                if (e.GetType() == Type.GetType("System.IO.IOException"))
                {
                    Output.Message(ConsoleColor.DarkRed, "It is likely that the server has shut down.");
                }
            }

            if (bytesRead == 0)
            {
                break;
            }

            int …
Run Code Online (Sandbox Code Playgroud)

c# multithreading

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

为什么这个添加的UI按钮的事件没有被调用?

在开发城市模式:Skylines时,我遇到了一个问题.

据我所知,我的大多数代码都可以正常工作 - 但到目前为止,我还没有对它进行测试.这是因为当我添加到UI的按钮被点击时,它应该按顺序被调用.此按钮上的单击事件不会调用我已分配给它的处理程序.

这是我创建按钮的地方:

public class LoadingExtension : LoadingExtensionBase
{
    public override void OnLevelLoaded(LoadMode mode)
    {
        Debug.LogDebugMessage("Adding 'Generate City Report' button to UI");

        // Get the UIView object. This seems to be the top-level object for most
        // of the UI.
        var uiView = UIView.GetAView();

        // Add a new button to the view.
        var button = (UIButton)uiView.AddUIComponent(typeof(UIButton));

        // Set the text to show on the button.
        button.text = "Generate City Report";

        // Set the button dimensions.
        button.width = 250; …
Run Code Online (Sandbox Code Playgroud)

c# unity-game-engine cities-skylines-api

5
推荐指数
0
解决办法
304
查看次数