几个星期前,我跳过了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使用它来进行所有测试.
好的,以下是我对这种情况的疑问:
我一直在探索重新发明DataTable,我想知道它的用途是什么MarshalByValueComponent.我相信它是用于.NET远程处理(也许WinForms和的WebForms),但被换下的WCF取代.我在GitHub或Google上找不到任何值得注意的用法.是否MarshalByValueComponent仍在使用?
使用 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#编写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".我不知道为什么它告诉我在我已有的时候引用它.