标签: system.componentmodel

使用NUnit进行MEF和单元测试

几个星期前,我跳过了MEF(ComponentModel)的潮流,现在我正在使用它来插入很多插件和共享库.总的来说,除了我经常犯的错误之外,它一直很棒,这导致令人沮丧的调试会话.

无论如何,我的应用程序运行良好,但我的MEF相关代码更改导致我的自动构建失败.我的大多数单元测试都失败了,因为我测试的模块依赖于需要由MEF加载的其他模块.我通过绕过MEF并直接实例化这些对象来解决这些问题.

换句话说,通过MEF,我会有类似的东西

[Import]
public ICandyInterface ci { get; set; }
Run Code Online (Sandbox Code Playgroud)

[Export(typeof(ICandyInterface))]
public class MyCandy : ICandyInterface
{
    [ImportingConstructor]
    public MyCandy( [Import("name_param")] string name) {}
    ...
}
Run Code Online (Sandbox Code Playgroud)

但在我的单元测试中,我会使用

CandyInterface MyCandy = new CandyInterface( "Godiva");
Run Code Online (Sandbox Code Playgroud)

另外,CandyInterface需要连接到数据库,我通过将测试数据库添加到我的单元测试文件夹来解决这个问题,我让NUnit使用它来进行所有测试.

好的,以下是我对这种情况的疑问:

  1. 这是一种做坏事的坏方法吗?
  2. 你会建议在[SetUp]中编写部件吗?
  3. 我还没有学会如何在单元测试中使用模拟 - 这是一个很好的例子,我可能想要模拟底层数据库连接(不知何故)只返回虚拟数据而不是真正需要数据库?
  4. 如果您之前遇到过这样的事情,您能否提供您的经验和解决问题的方式?(或者这应该进入社区维基?)

unit-testing mef system.componentmodel

4
推荐指数
1
解决办法
6457
查看次数

MarshalByValueComponent仍然使用或有用吗?

我一直在探索重新发明DataTable,我想知道它的用途是什么MarshalByValueComponent.我相信它是用于.NET远程处理(也许WinForms和的WebForms),但被换下的WCF取代.我在GitHub或Google上找不到任何值得注意的用法.是否MarshalByValueComponent仍在使用?

.net c# .net-4.0 system.componentmodel .net-remoting

4
推荐指数
1
解决办法
981
查看次数

是否可以向 C# 枚举对象添加自定义属性?

使用 c# 是否可以使用关联每个枚举项的属性?

我已使用描述属性enum项目添加英文描述。

为了给每个项目添加英文描述,我做了以下操作

public enum MyEnum
{
    [Description("My First Item")]
    First,

    [Description("My Second Item")]
    Second,

    [Description("My Third Item")]
    Third
}
Run Code Online (Sandbox Code Playgroud)

enum然后我向我的调用添加了一个扩展方法GetDescription(),它允许我像这样获得描述

public static string GetDescription(this Enum value)
{
    Type type = value.GetType();

    string name = Enum.GetName(type, value);

    if (name != null)
    {
        FieldInfo field = type.GetField(name);
        if (field != null)
        {
            DescriptionAttribute attr = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
            if (attr != null)
            {
                return attr.Description;
            }
        }
    }

    return name;
} …
Run Code Online (Sandbox Code Playgroud)

c# enums system.componentmodel

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

C#使用和编译Windows.Form

我正在使用C#编写GUI代码.我正在按照这个简单的教程开始.要编译,我需要引用System.Windows.Forms.DLL System.Drawing.DLL,所以我输入

csc /r:System.Windows.Forms.DLL /r:System.Drawing.DLL FirstForm.cs
Run Code Online (Sandbox Code Playgroud)

但是,我得到这样的错误:

FirstForm.cs(6,14): error CS0012: The type 'System.ComponentModel.Component' is
    defined in an assembly that is not referenced. You must add a reference
    to assembly 'System, Version=4.0.0.0, Culture=neutral,
    PublicKeyToken=b77a5c561934e089'.
c:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Windows.Forms.dll:
    (Location of symbol related to previous error)
Run Code Online (Sandbox Code Playgroud)

我已经在我的路径中添加了"c:\ Windows\Microsoft.NET\Framework\v4.0.30319".我不知道为什么它告诉我在我已有的时候引用它.

c# user-interface reference system.componentmodel

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