小编dri*_*ter的帖子

C#:将CurrentCulture与InvariantCulture区分开来的单元测试

我有一个采用StringComparison参数的方法,它确实需要一个单元测试来证明它正在执行正确的比较类型.

这基本上是我到目前为止所做的.它通过比较"oe"和"œ"(参见System.String)将Ordinal与其余部分区分开来.但是,我没有找到区分CurrentCulture和InvariantCulture的方法.

using System;
using NUnit.Framework;
using System.Threading;
using System.Globalization;

namespace NUnitTests
{
    [TestFixture]
    public class IndexOfTests
    {
        [Test]
        public void IndexOf_uses_specified_comparison_type()
        {
            StringComparison comparisonTypePerformed;

            result = TestComparisonType(StringComparison.CurrentCulture);
            Assert.AreEqual(StringComparison.CurrentCulture, comparisonTypePerformed);

            result = TestComparisonType(StringComparison.InvariantCulture);
            Assert.AreEqual(StringComparison.CurrentCulture, comparisonTypePerformed);

            result = TestComparisonType(StringComparison.Ordinal);
            Assert.AreEqual(StringComparison.CurrentCulture, comparisonTypePerformed);
        }

        bool TestComparisonType(StringComparison comparisonType)
        {
            int result;

            // Ensure the current culture is consistent for test
            var prevCulture = Thread.CurrentThread.CurrentCulture;
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", false);

            try
            {
                result = IndexOf("oe", "œ", comparisonType);
                if (result == 0)
                {
                    // …
Run Code Online (Sandbox Code Playgroud)

c# culture unit-testing

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

Bash脚本:这是什么意思?"完成</ dev/null&disown"

你能否详细解释一下最后一行是什么,为什么需要它?

while true; do
    /usr/bin/ssh -R 55555:localhost:22 -i ~/.ssh/tunnel-id user@server.com
    sleep 1
done < /dev/null & disown
Run Code Online (Sandbox Code Playgroud)

这是整个脚本,其目的是创建到中继服务器的SSH隧道.我是Bash的新手,但看起来它会不断尝试保持连接活着,但我不明白最后一行的语法.

此脚本是在防火墙后面使用SSH的过程的一部分,或者在我的情况下是NAT:http: //martin.piware.de/ssh/index.html

syntax bash

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

C#Winforms线程:调用Closed Form

以下代码说明了我的困境.代码创建一个处理内容的后台线程,然后使用结果调用UI线程.

如果后台线程在窗体关闭后调用窗体上的Invoke,它可能会抛出异常.它在调用Invoke之前检查IsHandleCreated,但是在检查之后表单可能会关闭.

void MyMethod()
{
    // Define background thread
    Action action = new Action(
        () =>
        {
            // Process something
            var data = BackgroundProcess();

            // Try to ensure the form still exists and hope
            // that doesn't change before Invoke is called
            if (!IsHandleCreated)
                return;

            // Send data to UI thread for processing
            Invoke(new MethodInvoker(
                () =>
                {
                    UpdateUI(data);
                }));
        });

    // Queue background thread for execution
    action.BeginInvoke();
}
Run Code Online (Sandbox Code Playgroud)

一种解决方案可能是同步FormClosing和每次调用Invoke,但这听起来不是很优雅.有没有更简单的方法?

c# invoke thread-safety winforms

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

Python:通过完成对任务进行排序后获取对原始任务的引用

问题:asyncio.as_completed 产生结果后,如何获取原始任务的引用?

基本上与这个 C# 问题相同,除了在 Python:按完成排序任务后获取对原始任务的引用?

示例问题:

# Takes a list of WebClient objects,
# calls each one simultaneously,
# and yields the results immediately as they arrive
# to a synchronous caller.

def yieldThingsAsTheyArrive(webClients):

    tasks = []
    for webClient in webClients:
        # This is what we want to get a reference to later:
        task = webClient.fetch_thing()  # start long-running request asynchronously
        tasks.append(task)

    loop = asyncio.get_event_loop()
    for future in asyncio.as_completed(tasks):
        thing = loop.run_until_complete(future)  # since our caller is synchronous, …
Run Code Online (Sandbox Code Playgroud)

python-asyncio

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

返回无构造函数的对象作为输出参数

我想从函数返回一个对象作为输出参数,但该对象没有默认构造函数,所以我不能这样做:

bool FindFlaggedObject(MyObject& myObject)
{
    std::vector<MyObject> myObjects = GetSomeObjectList();
    for (UINT i = 0; i < myObjects.size(); i++)
    {
        if (myObjects[i].Flag) {
            myObject = myObjects[i];
            return true;
        }
    }
    return false;
}

void main()
{
    MyObject myObject;  // NOT ALLOWED - OBJECT HAS NO DEFAULT CONSTRUCTOR
    if (FindFlaggedObject(myObject))
    {
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

所以,看起来我应该在堆上返回它并使用shared_ptr实例进行管理,如下所示:

bool FindFlaggedObject(MyObject& myObject)
{
    std::vector<MyObject> myObjects = GetSomeObjectList();
    for (UINT i = 0; i < myObjects.size(); i++)
    {
        if (myObjects[i].Flag) {
            myObject = new MyObject(myObjects[i]);
            return true; …
Run Code Online (Sandbox Code Playgroud)

c++

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

什么是C#.NET的优秀博客网站

我希望在.NET上发布关于单元测试命名约定的博客,并希望将来更多地了解C#.NET或更多一般开发主题.

我知道有几个流行的博客网站,并且在很大程度上取决于个人偏好,但有人可能从哪个最佳设计或最受欢迎的博客网站开始?

.net blogs web

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

标签 统计

c# ×2

.net ×1

bash ×1

blogs ×1

c++ ×1

culture ×1

invoke ×1

python-asyncio ×1

syntax ×1

thread-safety ×1

unit-testing ×1

web ×1

winforms ×1