小编mic*_*ael的帖子

ConcurrentDictionary是否是线程安全的,我可以将它用于静态缓存?

基本上,如果我想做以下事情:

public class SomeClass
{
    private static ConcurrentDictionary<..., ...> Cache { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这是否让我避免lock在整个地方使用s?

c# locking static-members concurrent-collections

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

类型参数"T"与外部类型"..."中的类型参数同名

public abstract class EntityBase { ... }

public interface IFoobar
{
    void Foo<T>(int x)
        where T : EntityBase, new();
}

public interface IFoobar<T>
    where T : EntityBase, new()
{
    void Foo(int x);
}

public class Foobar<T> : IFoobar, IFoobar<T>
    where T : EntityBase, new()
{
    public void Foo(int x) { ... }

    void IFoobar.Foo<T>(int x) { Foo(x); }
}
Run Code Online (Sandbox Code Playgroud)

我收到编译器警告: Type parameter 'T' has the same name as the type parameter from outer type '...'

我尝试过:void IFoobar.Foo<U>(int x) { …

c# generics interface constraints explicit-interface

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

WPF - 资源未从Generic.xaml加载

主题\ Generic.xaml:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="WPF Commons;component/Controls/Layout/Foo/FooItem.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

控制\布局\富\ FooItem.xaml:

<Style TargetType="{x:Type l:FooItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type l:FooItem}">
                <Border>
                    <ContentPresenter ContentSource="Header" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)


如果我将整个样式复制到我的usercontrol资源中,它可以正常工作.但是,如果我不这样做,则usercontrol显示为空.在Expression Blend 4中,我右键单击并选择Edit Template>,但它不会让我选择Edit a Copy...哪个让我相信某些内容严重错误并且Generic.xaml未正确加载.我认为它是Generic.xaml,因为如果我删除MergedDictionary调用并将xaml样式直接复制/粘贴到Generic.xaml中,它仍然无效.

wpf custom-controls resourcedictionary generic.xaml

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

如何使用moq测试抽象类中的具体方法?

在过去,当我想模拟一个抽象类时,我只是在扩展抽象类的代码中创建一个模拟类,然后在我的单元测试中使用该类...

public abstract class MyConverter : IValueConverter
{
    public abstract Object Convert(...) { ... };

    public virtual Object ConvertBack(...) { ... }
}

private sealed class MockedConverter : MyConverter { ... }

[TestMethod]
public void TestMethod1()
{
    var mock = new MockedConverter();

    var expected = ...;
    var actual = mock.ConvertBack(...);

    Assert.AreEqual(expected, actual);
}
Run Code Online (Sandbox Code Playgroud)

我想养成使用Moq的习惯.我不确定如何使用Moq来测试抽象类的默认返回值.有什么建议吗?

c# abstract-class unit-testing moq mocking

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

WPF棱镜 - 使用棱镜区域有什么意义?

我只是想知道地区的意义.我想我不明白他们解决的问题.

例如,我看到很多人使用区域作为导航区域,但是为什么不只是将ItemsControl绑定到ObservableCollection而不是具有区域并将不同的导航元素加载到该区域?


一个现实世界的例子,它的使用/好处超过替代品将摇滚!

wpf design-patterns prism prism-4

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

如何创建一个带有代理对的字符串?

我在Jon Skeet的博客上看到了这篇文章,他谈到了字符串翻转.我想尝试他自己展示的例子,但它似乎有效...这让我相信我不知道如何创建一个包含代理对的字符串,这实际上会导致字符串反转失败.如何实际创建一个带有代理对的字符串,以便我自己可以看到失败?

c# string utf-16 surrogate-pairs utf-32

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

ContentPresenter.ContentSource与内容

基本上,我不明白这里的真正区别是什么:

TabItem的Microsoft代码使用:

<ContentPresenter ContentSource="Header" ... />
Run Code Online (Sandbox Code Playgroud)

那么,何时使用该Content属性而不是(或除此之外)ContentSource

wpf xaml contentpresenter controltemplate

13
推荐指数
1
解决办法
7568
查看次数

改进了IValueConverter - MarkupExtension还是DependencyObject?

我在网上看到了两种不同的方法来增强IValueConverter.其中一个从MarkupExtension扩展了ValueConverter,另一个从DependencyObject扩展.我无法从两者延伸,所以我想知道是否有一个比另一个好?

c# wpf markup-extensions dependencyobject ivalueconverter

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

为什么没有人使用INotifyPropertyChanging?

我知道MVVM大量使用INotifyPropertyChanged,但我从未见过使用过任何INotifyPropertyChanging.有什么理由吗?

如果我确实想要使用它,那么将它集成到我的MVVM框架中的好方法是什么?我知道你不应该MessageBox在你的ViewModel 上使用,因为那时你无法对它进行单元测试.那么如何进行警报,然后继续使用PropertyChange(如果适用)?

c# wpf mvvm

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

缓存反射属性getter/setter的最佳方法是什么?

我知道反思可能很昂贵.我有一个经常获取/设置属性的类,我想到的一种方法是以某种方式缓存反射.我不确定我是否应该缓存表达式或者在这里做什么.这就是我目前正在做的事情:

typeof(T).GetProperty(propName).SetValue(obj, value, null);
typeof(T).GetProperty(propName).GetValue(obj, null);
Run Code Online (Sandbox Code Playgroud)

那么......什么才能让这个更快?

c# reflection performance caching properties

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