小编Flo*_*ian的帖子

如何将字典值作为通用列表

我只想从字典值中获取一个列表,但它并不像看起来那么简单!

这里的代码:

Dictionary<string, List<MyType>> myDico = GetDictionary();
List<MyType> items = ???
Run Code Online (Sandbox Code Playgroud)

我尝试:

List<MyType> items = new List<MyType>(myDico.values)
Run Code Online (Sandbox Code Playgroud)

但它不起作用:-(

c# dictionary list

54
推荐指数
5
解决办法
11万
查看次数

Moq中的SetupSequence

我想模拟第一次返回0,然后在调用方法的任何时候返回1.问题是,如果方法被调用4次,我应该写:

mock.SetupSequence(x => x.GetNumber())
    .Returns(0)
    .Returns(1)
    .Returns(1)
    .Returns(1);
Run Code Online (Sandbox Code Playgroud)

否则该方法返回null.

有没有办法写下次在第一次调用该方法后,该方法返回1?谢谢

为SetupSequence设置更多"运营商"是否合适?如果您认为是,您可以投票:http: //moq.uservoice.com/forums/11304-general/suggestions/2973521-setupsequence-more-operators

c# unit-testing moq mocking

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

Parallel.Foreach +收益率回报?

我想用这样的并行循环处理一些东西:

public void FillLogs(IEnumerable<IComputer> computers)
{
    Parallel.ForEach(computers, cpt=>
    {
        cpt.Logs = cpt.GetRawLogs().ToList();
    });

}
Run Code Online (Sandbox Code Playgroud)

好的,它工作正常.但是如果我想让FillLogs方法返回一个IEnumerable怎么办?

public IEnumerable<IComputer> FillLogs(IEnumerable<IComputer> computers)
{
    Parallel.ForEach(computers, cpt=>
    {
        cpt.Logs = cpt.GetRawLogs().ToList();
        yield return cpt // KO, don't work
    });

}
Run Code Online (Sandbox Code Playgroud)

编辑

似乎不可能......但我使用这样的东西:

public IEnumerable<IComputer> FillLogs(IEnumerable<IComputer> computers)
{
    return computers.AsParallel().Select(cpt => cpt);
}
Run Code Online (Sandbox Code Playgroud)

但是我把cpt.Logs = cpt.GetRawLogs().ToList();指示放在哪里

c# yield-return parallel.foreach

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

如何获得1年的Timespan?

我希望得到一个代表C#一年的Timespan结构.

c# timespan

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

如何制作IEnumerable方法的并行方法

在这篇文章之后,我想要并行化这个方法:

    public IEnumerable<string> GetAllLogs(IEnumerable<IComputer> computers)
    {
        foreach (var cpt in computers)
        {
            foreach (var log in cpt.GetLogs())
            {
                yield return log;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

当其中一个方法GetLogs完成时,我希望方法"yield returns"成为日志.如果我有4台电脑返回:

  • 计算机01:"a","b","c","d","e"
  • 计算机02:"1","2","3","4","5"
  • 计算机03:"alpha","beta","gamma","delta","epsilon"
  • 计算机04:"我","II","III","IV","V"

使用"顺序方法",输出为:

a
b
c
d
e
1
2
3
4
5
alpha
beta
gamma
delta
epsilon
I
II
III
IV
V
Run Code Online (Sandbox Code Playgroud)

这些方法在20秒内运行.有一个Thread.Sleep(1000)GetLogs方法.

我希望输出看起来像这样:

III
a
4
gamma
b
c
IV
5
d
II
beta
e
1
2
delta
alpha
3
epsilon
I
Run Code Online (Sandbox Code Playgroud)

并在几秒钟内运行.

我想方法返回一个IEnumerable

c# parallel-processing nested-loops

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

如何区分Windows 7和Windows 2008 R2

我需要区分Windows 7和Windows 2008 R2,但我不知道如何.OS Version属性返回相同的数字"6.1.7600.0"

问候,

弗洛里安

c#

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

使用Reflection订阅活动

我有一个非常简单的课程:

class Trace
{
    void WriteTrace()
    {
        Console.WriteLine("Trace !");
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望这个类订阅一个事件,例如表单控件的load事件.控件和事件是动态定义的,所以我想使用反射来做到这一点我正在尝试这样的事情:

在我的类Trace我有这个方法:

public void SubscribeEvent (Control control)
{
        if (type.IsAssignableFrom(control.GetType()))
        {
            Trace test = this;
            MethodInfo method = typeof(Trace).GetMethod("WriteTrace");

            // Subscribe to the event
            EventInfo eventInfo = control.GetType().GetEvent("Load"); // We suppose control is a form 
            Delegate handler = Delegate.CreateDelegate(eventInfo.EventHandlerType, control, method); // ERROR : Error binding to target method

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

最后一行出错:绑定到目标方法时出错.我的片段有什么问题?

谢谢 !

编辑:好的,没有更多的错误但是当从Form中引发事件"Load"时,方法WriteTrace没有被调用(我已经放置了一个断点,但没有到达).为什么?

对不起编辑,它工作得很好:)

.net reflection events

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

循环中匿名方法的不同"执行上下文"的问题

我在循环中有一个匿名方法的问题.

以下代码只是为了说明我的问题:

private void Form1_Load(object sender, EventArgs e)
{
    List<string> bassists = new List<string>(){
        "Jaco Pastorius", 
        "Marcus Miller", 
        "Flea", 
        "Vicor Wooten"
    };

    foreach (string item in bassists)
    {
        this.button1.Click += (s, ea) => Output(s, ea, item); 
    }
}

private void Output(object s, EventArgs e, string item)
{
    this.listBox1.Items.Add(item);
}
Run Code Online (Sandbox Code Playgroud)

当我点击按钮时,输出是:

Victor Wooten
Victor Wooten
Victor Wooten
Victor Wooten

代替:

Jaco Pastorius
Marcus Miller
Flea
Vicor Wooten

我的问题的主要问题是不同的执行上下文.我知道我的例子是愚蠢的.

.net c# anonymous-methods

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

使用Enumerable.Union方法合并字典<TKey,TValue>

我正在测试UNION方法以合并到字典(类型为Dictionary).它适用于TValue类型是字符串或int甚至对象.但是如果TValue类型是一个集合(使用List和object []测试),则抛出异常:"ArgumentException:已经添加了具有相同键的项目."

这是我的代码:

Dictionary<int,string> _dico1 = new Dictionary<int, string>()
{
    {0, "zero"},
    {1, "one"}
};

Dictionary<int,string> _dico2 = new Dictionary<int,string>()
{
    {1 , "one"},
    {2 , "two"},
    {3 , "three"},
    {4 , "four"},
    {5 , "five"},
    {6 , "six"}
};

Dictionary<int, List<string>> _dico3 = new Dictionary<int, List<string>>()
{
    {0, new List<string>{"zero"}},
    {1, new List<string>{"one"}}
};

Dictionary<int, List<string>> _dico4 = new Dictionary<int, List<string>>()
{
    {1, new List<string>{"one"}},
    {2, new List<string>{"two"}},
    {3, new List<string>{"three"}},
    {4, new List<string>{"four"}},
    {5, new …
Run Code Online (Sandbox Code Playgroud)

c# union merge dictionary

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

单元测试......如何改进它

我想测试一段返回对象的代码.

我使用NUnit并在测试类中编写一个方法来测试我的方法是否正常工作...

[Test]
public void GetMyObjectFromLog()
{
     string _xmlFilePath = @"C:\XmlFile.xml";
     MyObjectParser _myObjectParser = new MyObjectParser();
     MyObject _mockMyObject = new MyObject
                              {
                                   Title = "obj",
                                   Name = "objName"
                              }
     MyObject _myObject = _myObjectParser.GetMyObjectFromLog(_xmlFilePath);

     Assert.AreEqual(_mockMyObject , _myObject);
}
Run Code Online (Sandbox Code Playgroud)

此测试不起作用,因为MyObject不覆盖Equals方法,我不想Equals仅为测试目的覆盖方法.

所以我重写了这样的测试:

[Test]
public void GetMyObjectFromLog()
{
     string _xmlFilePath = @"C:\XmlFile.xml";
     MyObjectParser _myObjectParser = new MyObjectParser();
     MyObject _myObject = _myObjectParser.GetMyObjectFromLog(_xmlFilePath);

     Assert.AreEqual("obj", _myObject.Title);
     Assert.AreEqual("objName", _myObject.Name);
}
Run Code Online (Sandbox Code Playgroud)

好吧,它有效......但这个测试是否相关?而且,对文件有依赖性.

使用模拟框架而不是相关吗?以及如何使用它?

谢谢 !

c# nunit unit-testing

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