小编Ste*_*ger的帖子

调用模拟对象的异步方法时出现NullReferenceException

我正在尝试使用MOQ对以下ViewModel的LoginExecute方法进行单元测试

public class LoginViewModel : ViewModelBase, ILoginViewModel
{
    INavigationService navigationService;
    IDialogService dialogService;
    IAdminService adminService;

    public RelayCommand LoginCommand { get; set; }

    private string _productID;

    public string ProductID
    {
        get { return _productID; }
        set
        {
            _productID = value;
            RaisePropertyChanged("ProductID");
        }
    }

    public LoginViewModel(INavigationService navigationService, IDialogService dialogService, IAdminService adminService)
    {
        this.navigationService = navigationService;
        this.dialogService = dialogService;
        this.adminService = adminService;
        InitializeCommands();
    }

    private void InitializeCommands()
    {
        LoginCommand = new RelayCommand(() => LoginExecute());
    }

    public async Task LoginExecute()
    {
        await this.navigationService.TestMethod();
        this.navigationService.Navigate(typeof(ITherapistsViewModel));
    } …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing mstest asynchronous mocking

11
推荐指数
2
解决办法
4190
查看次数

使用反射获取通用IDictionary的值

我有一个实现的实例IDictionary<T, K>,我不知道编译时的T和K,并希望从中获取所有元素.我不想IEnumerable出于某种原因使用,这将是唯一实现的非通用接口IDictionary.

我到目前为止的代码:

// getting types
Type iDictType = instance.GetType().GetInterface("IDictionary`2");
Type keyType = iDictType.GetGenericArguments()[0];
Type valueType = iDictType.GetGenericArguments()[1];

// getting the keys
IEnumerable keys = (IEnumerable)dictType.GetProperty("Keys")
  .GetValue(instance, null);

foreach (object key in keys)
{
  // ==> this does not work: calling the [] operator
  object value = dictType.GetProperty("Item")
    .GetValue(instance, new object[] {key } );


  // getting the value from another instance with TryGet
  MethodInfo tryGetValue = iDictType.GetMethod("TryGetValue");
  object[] arguments = new object[] { key, …
Run Code Online (Sandbox Code Playgroud)

c# generics reflection idictionary

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

SqlConnection.ClearAllPools,清除了什么?

我有一个应用程序,用于SqlConnection.ClearAllPools在删除数据库之前关闭所有连接.

有一种情况仍然存在连接.此连接已在另一个应用程序域中创建.

所以我想知道哪些连接关闭了SqlConnection.ClearAllPools

  • 只有调用进程(或AppDomain)打开的连接?
  • 这台机器打开了所有连接?
  • ...?

.net sql-server ado.net connection-pooling

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

异步代码的单元测试

我有一些代码,它使用HttpWebRequest类的.BeginGetResponse()方法,它是异步的.此外,我正在使用Microsoft的Unit Test App项目来测试应用程序.

问题是测试框架没有等待运行异步代码的结束,所以我无法检查其结果.

我应该如何使用Unit Test App项目测试异步代码?我没有使用async/await modificators.

c# unit-testing windows-phone-8

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

NHibernate:映射列表字典

我的班级有一个类型的领域Dictionary<string, List<string>>.用NHibernate映射它的最佳方法是什么?我最好把它作为一个领域,不想暴露它.

非常感谢!

乌鲁

mapping nhibernate

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

C#DataContract序列化,如何反序列化到已存在的实例

我有一个类,它包含所有现有实例的静态字典,这些实例是在编译时定义的.

基本上它看起来像这样:

[DataContract]
class Foo
{
  private static Dictionary<long, Foo> instances = new Dictionary<long, Foo>();

  [DataMember]
  private long id;

  public static readonly Foo A = Create(1);
  public static readonly Foo B = Create(2);
  public static readonly Foo C = Create(3);

  private static Foo Create(long id)
  {
    Foo instance = new Foo();
    instance.id = id;
    instances.Add(instance);
    return instance;
  }

  public static Foo Get(long id)
  {
    return instances[id];
  }    

}
Run Code Online (Sandbox Code Playgroud)

还有其他字段,派生类,但这对问题无关紧要.

只有id序列化.当反序列化此类型的实例时,我想获取已创建为静态字段(A,BC)的Foo.Get(id)实例,而不是获取新实例. …

c# serialization netdatacontractserializer

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

NHibernate和hilo生成器:如何设计关键表?

我准备将我的一些实体从身份转换为hilo id-generator.

我不知道如何设计保持下一个高值的表.

  • 我应该为所有实体或一组相关实体使用单个表吗?
  • 我应该为每个实体使用另一张表吗?
  • 我应该使用每个实体的另一行或列的单个表吗?

有没有最佳做法?需要考虑什么?任何方法都有任何优缺点吗?

nhibernate database-design hilo

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

如何使用Nullable Int的条件运算

一个小问题.任何想法的人为什么这不起作用?

int? nullableIntVal = (this.Policy == null) ? null : 1;
Run Code Online (Sandbox Code Playgroud)

null如果左手表达式为True,我试图返回,否则1.看似简单,但给出了编译错误.

无法确定条件表达式的类型,因为null和之间没有隐式转换int.

如果我用任何有效的替换nullin ,那么没有问题.? null : 1int

c# nullable conditional-operator

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

强制(通用)值类型作为引用

我有一些带有一些字段的结构.其中一个字段是泛型类型.泛型类型可以是引用类型或值类型.

我想强制它在内部存储为引用,以避免结构变得太大.

struct Foo<T>
{
  T field; // should be a reference
}
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用object或者T[],但两者都很尴尬.是不是像通用的参考类型?

struct Foo<T>
{
  Reference<T> field;
}
Run Code Online (Sandbox Code Playgroud)

是的,当然,我可以写自己的.但我试图避免这种情况

c# reference value-type

8
推荐指数
2
解决办法
2463
查看次数

C#方法重载和通用接口

我对项目中遇到的问题感到困惑.我试图简化它以重现效果:

interface IBar { }

class Bar : IBar {}

interface IFoo<T> where T : IBar { }

class Foo<T> : IFoo<T> where T : IBar { }


class Class1
{
    public void DoTheFoo<T>(T bar) where T : IBar
    {}

    public void DoTheFoo<T>(IFoo<T> foo) where T : IBar
    {}


    public void Test()
    {
        var bar = new Bar();
        var foo = new Foo<Bar>();

        DoTheFoo(bar); // works

        DoTheFoo<Bar>(foo); // works
        DoTheFoo((IFoo<Bar>)foo); // works
        DoTheFoo(foo); // complains
    }
}
Run Code Online (Sandbox Code Playgroud)

对我来说这看起来很好,但编译器在最后一次调用时抱怨,因为它试图DoTheFoo<T>(T bar) …

c# generics overloading

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