我在Visual Studio 2017 RC中创建了一个项目,以检查我是否可以在.NET Framework 4.5项目中使用新的C#7.0语言功能.在我看来,在引用System.ValueTupleNuGet之后,新的元组工作得很好.还有什么我应该考虑的,或者这会起作用吗?
检查System.ValueTupleNuGet依赖项后,看起来不支持.NET Framework 4.0.是这种情况,还是有一些方法可以让新语言在这个运行时工作?
从一个选项列表到一个只包含Some的元素的列表,我出乎意料地遇到了一些麻烦.
我最初的尝试是:
let ga = List.filter (fun xx ->
match xx with
| Some(g) -> true
| None -> false) gao
Run Code Online (Sandbox Code Playgroud)
但是,当然,这种结果类型仍然是一个选项列表.我不知道如何使用List.map来压缩它,因为你必须处理匹配语句中的所有情况.我有一个丑陋的解决方案,但我想知道是否有更好的东西.
丑陋:
let rec gOptRemove gdec gacc =
match gdec with
| head :: tail ->
match head with
| Some(a) -> gOptRemove tail (a :: gacc)
| None -> gOptRemove tail gacc
| [] -> gacc
Run Code Online (Sandbox Code Playgroud)
我更愿意找到一个非递归的解决方案或找出这种事情的标准方法.
到目前为止,这是我的XAML.
<ScrollViewer Grid.Column="1" Grid.RowSpan="2">
<ListBox Background="Black" ItemsSource="{Binding Path=ActiveLog}" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Background="Black">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0" Foreground="White">
<TextBlock >Date:</TextBlock>
<TextBlock Text="{Binding Path=LogDate}"/>
</TextBlock>
<TextBlock Grid.Column="1" Grid.Row="0" Foreground="White">
<TextBlock >Severity:</TextBlock>
<TextBlock Text="{Binding Path=Severity}"/>
</TextBlock>
<TextBlock Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" Foreground="LightGray" Text="{Binding Path=Message}"></TextBlock>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.Template>
<ControlTemplate>
<StackPanel Background="Black" IsItemsHost="True" >
</StackPanel>
</ControlTemplate>
</ListBox.Template>
</ListBox>
</ScrollViewer>
Run Code Online (Sandbox Code Playgroud)
唯一的问题是所选项目右侧有一个蓝色框.我假设有一种方法可以改变选择颜色,但我找不到它.
如何创建一个具有可选参数和参数的方法?
static void Main(string[] args)
{
TestOptional("A",C: "D", "E");//this will not build
TestOptional("A",C: "D"); //this does work , but i can only set 1 param
Console.ReadLine();
}
public static void TestOptional(string A, int B = 0, params string[] C)
{
Console.WriteLine(A);
Console.WriteLine(B);
Console.WriteLine(C.Count());
}
Run Code Online (Sandbox Code Playgroud) 我是xUnit.net和AutoFixture的新手.
我正在开发一个"测试项目"来熟悉xUnit.net和Autofixture.有一件事我不明白.
[Fact]和之间有什么区别[Theory, AutoMoqData]?
如果以下两段代码相同,你能告诉我吗?我问这个是因为测试成功了,但我想以正确的方式学习它.
[Fact]
public void UpdateVersionWillUpdateCorrectlyInRepository()
{
var fixture = new Fixture().Customize(new AutoMoqCustomization());
var contract = fixture.Create<VersionContract>();
var version = fixture.Create<Version>();
fixture.Freeze<Mock<IContractMapper>>()
.Setup(r => r.Map(contract)).Returns(version);
var repMock = fixture.Freeze<Mock<VersionRepository>>();
var sut = fixture.Create<VersionManagementService>();
sut.UpdateVersion(contract);
repMock.Verify(r => r.UpdateVersion(version));
}
Run Code Online (Sandbox Code Playgroud)
和
[Theory, AutoMoqData]
public void UpdateVersionWillUpdateCorrectlyInRepository(
VersionContract contract,
Version version,
[Frozen]Mock<IContractMapper> mapMock,
[Frozen]Mock<VersionRepository> repMock,
VersionManagementService sut)
{
mapMock.Setup(r => r.Map(contract)).Returns(version);
sut.UpdateVersion(contract);
repMock.Verify(r => r.UpdateVersion(version));
}
Run Code Online (Sandbox Code Playgroud)
是什么让我认为关键词[Fact]和关键词存在差异[Theory].
我假设[Theory]关键字告诉xUnit.net框架提供的数据来自某处,其中某处是Autofixture.虽然[Fact]告诉xUnit没有关于数据的来源,我需要手动构建对象.
使用log4net,我已经看到了使用称为NDC的每个线程堆栈的上下文标签的可能性.
通过指定%x或%ndc格式参数,在此堆栈上推送的标签将显示在PatternLayout中.
用法如下:
ILog log = log4net.LogManager.GetLogger(...) ;
//pattern layout format: "[%ndc] - %message%newline"
log.Info("message 1");
using(log4net.NDC.Push("context")
{
using(log4net.NDC.Push("inner_context")
{
log.Info("message 2");
}
log.Info("message 3");
}
log.Info("message 4");
Run Code Online (Sandbox Code Playgroud)
输出类似于:
null - message 1
context inner_context - message 2
context - message 3
null - message 4
Run Code Online (Sandbox Code Playgroud)
在使用log4net的编程经验中,您何时发现此功能有用?
有没有办法设置一个git存储库,所以git pull默认为一个远程,git push默认为另一个?我知道我可以通过改变分支部分中变量的值来设置两者,但是如何分别对每个方向进行设置?remote.git/config
在c ++中使用模板时,我遇到了将typename T转换为string的问题.例如:
template <typename T>
class Matrix {
public:
Matrix() {
//my_type = string type of T. i.e. if T is char. I want my_type to be "char".
}
string my_type;
}
Run Code Online (Sandbox Code Playgroud)
如何将T转换为表示T是什么的字符串.
注意:我只是在玩耍,所以请不要担心什么时候需要这样的东西.
WaitHandleC#.net线程背后的基本概念是什么?它的用途是什么?什么时候用?什么是利用了WaitAll和了WaitAny里面的方法呢?
我试图unique_ptr在一个unique_ptr带有基类的函数中使用一个派生类.就像是:
class Base {};
class Derived : public Base {};
void f(unique_ptr<Base> const &base) {}
…
unique_ptr<Derived> derived = unique_ptr<Derived>(new Derived);
f(derived);
Run Code Online (Sandbox Code Playgroud)
如果我正确理解了这个答案,那么这段代码应该可行,但它会导致以下编译错误:
错误C2664:'f':无法将参数1从'std :: unique_ptr <_Ty>'转换为'const std :: unique_ptr <_Ty>&'
IntelliSense:没有合适的用户定义转换,从"std :: unique_ptr <Derived,std :: default_delete <Derived >>"到"const std :: unique_ptr <Base,std :: default_delete <Base >>"存在
如果我改变f采取unique_ptr<Derived> const &derived,它工作正常,但这不是我想要的.
难道我做错了什么?我该怎么做才能解决这个问题?
我正在使用Visual Studio 2012.