在几年没有全天候使用它之后再回到.NET,并想知道现在好的单元测试包是什么.
我对NUnit很熟悉(几年前),并且曾经与IronRuby进行过简短的比赛,其目标是获得像rspec这样的东西,但除此之外我不知道.
我知道我可以谷歌这个并称之为一天,但我相信我可能会在这里问一个问题得到一个更好,更明智的回应:-)
建议?
我在C#中有一个标准的"动态字典"类型 -
class Bucket : DynamicObject
{
readonly Dictionary<string, object> m_dict = new Dictionary<string, object>();
public override bool TrySetMember(SetMemberBinder binder, object value)
{
m_dict[binder.Name] = value;
return true;
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
return m_dict.TryGetValue(binder.Name, out result);
}
}
Run Code Online (Sandbox Code Playgroud)
现在我打电话给它,如下:
static void Main(string[] args)
{
dynamic d = new Bucket();
d.Name = "Orion"; // 2 RuntimeBinderExceptions
Console.WriteLine(d.Name); // 2 RuntimeBinderExceptions
}
Run Code Online (Sandbox Code Playgroud)
该应用程序执行您所期望的,但调试输出如下所示:
A first chance exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in Microsoft.CSharp.dll A first chance …
背景:我正在使用MVVM创建一个WPF应用程序,并使用DI容器来构建我的ViewModel
我的App.xaml看起来像这样:
<Application x:Class="WpfApp.App"
...xmlns etc...
StartupUri="MainWindow.xaml">
<Application.Resources>
<app:ServiceLocator x:Key="serviceLocator" />
</Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)
MainWindow.xaml看起来像这样:
<Window x:Class="CompositeMefWpfApp.MainWindow"
...xmlns etc... >
<Control.DataContext>
<Binding Path="MainWindowViewModel" Source="{StaticResource serviceLocator}" />
</Control.DataContext>
Run Code Online (Sandbox Code Playgroud)
现在,这一切都运行正常,但是StartupUri硬编码到XAML中,我不想要.
根据我发现的几篇博文和文章的指导,我删除了StartupUri,并尝试MainWindow通过挂钩OnStartup在App.xaml.cs中创建,如下所示:
protected override void OnStartup( StartupEventArgs e )
{
base.OnStartup(e);
new MainWindow().Show();
}
Run Code Online (Sandbox Code Playgroud)
问题是,在尝试显示窗口时应用程序崩溃,但有以下异常:
找不到名为"{serviceLocator}"的资源.资源名称区分大小写.标记文件'WpfApp; component/mainwindow.xaml'中对象'System.Windows.Data.Binding'出错'第8行第45位.
据我所知,该<Application.Resources>部分根本没有从xaml文件中读出.我可以在OnStartup中添加一些代码来以编程方式添加资源,如下所示:
Resources.BeginInit();
Resources.Add("serviceLocator", new ServiceLocator());
Resources.EndInit();
Run Code Online (Sandbox Code Playgroud)
然而,这是一个丑陋的黑客,如果我想稍后在app.xaml文件中添加其他东西,并没有帮助我:-(
我应该挂钩其他一些活动吗?有没有解决的办法?
相比
String.Format("Hello {0}", "World");
Run Code Online (Sandbox Code Playgroud)
同
"Hello {0}".Format("World");
Run Code Online (Sandbox Code Playgroud)
为什么.Net设计者选择静态方法而不是实例方法?你怎么看?
我有一个带out参数的方法,我想指出一个Action或Func(或其他类型的委托).
这很好用:
static void Func(int a, int b) { }
Action<int,int> action = Func;
Run Code Online (Sandbox Code Playgroud)
但事实并非如此
static void OutFunc(out int a, out int b) { a = b = 0; }
Action<out int, out int> action = OutFunc; // loads of compile errors
Run Code Online (Sandbox Code Playgroud)
这可能是重复的,但搜索'out参数'并不是特别富有成效.
我有一个.NET 2.0 Windows窗体应用程序,它大量使用该ListView控件.
我已经将ListView类子类化为一个模板化的SortableListView<T>类,因此它可以更明智地表示它如何显示事物并对其进行排序.
不幸的是,这似乎打破了VS2005和2008中的Visual Studio Forms Designer.
该程序编译并运行正常,但当我尝试在设计器中查看拥有的表单时,我得到这些错误:
没有可用于此错误的堆栈跟踪或错误行信息
在MyApp.Main.Designer.cs行:XYZ列:1
Call stack:
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.Error(IDesignerSerializationManager manager, String exceptionText, String helpLink)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeExpression(IDesignerSerializationManager manager, String name, CodeExpression expression)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeExpression(IDesignerSerializationManager manager, String name, CodeExpression expression)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeStatement(IDesignerSerializationManager manager, CodeStatement statement)
Run Code Online (Sandbox Code Playgroud)
有问题的代码行是它实际添加到表单的位置,而且是
this.imagesTab.Controls.Add( this.listViewImages );
Run Code Online (Sandbox Code Playgroud)
listViewImages声明为
private MyApp.Controls.SortableListView<Image> listViewImages;
Run Code Online (Sandbox Code Playgroud)
并在InitializeComponent方法中实例化如下:
this.listViewImages = new MyApp.Controls.SortableListView<Image>();
Run Code Online (Sandbox Code Playgroud)
正如前面提到的,程序编译和运行完美,我已经尝试将SortableListView类转换为单独的程序集,因此它可以单独编译,但这没有区别.
我不知道从哪里开始.任何帮助,将不胜感激!
如果我创建一个没有的swift结构init,那么我可以调用编译器生成的默认成员初始化器,如下所示:
struct OrderFill {
let price:Int
let qty: Int
let timeStamp: NSDate
}
let o = OrderFill(price: 2, qty: 1, timeStamp: someDate)
Run Code Online (Sandbox Code Playgroud)
我想要做的是创建一个方便的init方法来从字典反序列化,然后字典链接到默认的成员init.就像是
struct OrderFill {
let price:Int
let qty: Int
let timeStamp: NSDate
init(dict:[String:AnyObject]) throws {
self.init(
price: dict["price"] as! Int
qty: dict["qty"] as! Int
timeStamp: try parseDate(dict["ts"] as! String)
}
}
let o = OrderFill(someDict)
Run Code Online (Sandbox Code Playgroud)
当我尝试编写这段代码时,编译器(Xcode 7.2)在调用中给出了错误"额外参数'数量',好像它没有看到默认的成员并试图递归调用 init(dictionary)
我可以编写自己的成员init,或者我可以直接从我的分配属性init(dictionary),但如果我可以链接调用它会很好.有没有办法在swift中做到这一点?
正常的铁路集合急切加载如下:
Person.find(:all, :include=>:companies)
Run Code Online (Sandbox Code Playgroud)
这会生成一些sql
LEFT OUTER JOIN companies ON people.company_id = companies.id
Run Code Online (Sandbox Code Playgroud)
但是,我需要一个自定义连接(如果我使用的话也会出现这种情况find_by_sql),所以我不能使用香草:include => :companies
自定义join/sql将获取我需要的所有数据,但是如何告诉activerecord它属于关联Company对象而不仅仅是一堆额外的行?
我需要在连接中添加其他条件.像这样的东西:
SELECT blah blah blah
LEFT OUTER JOIN companies ON people.company_id = companies.id AND people.magical_flag IS NULL
<Several other joins>
WHERE blahblahblah
Run Code Online (Sandbox Code Playgroud) Rails的ActiveSupport模块使用许多方法扩展了内置的ruby Time类.
值得注意的是,有一种to_formatted_s方法,它允许您编写Time.now.to_formatted_s(:db)以获取数据库格式的字符串,而不是必须在strftime任何地方编写丑陋的格式字符串.
我的问题是,有没有办法倒退?
类似于Time.parse_formatted_s(:db)解析数据库格式的字符串,返回一个新的Time对象.这似乎是rails应该提供的东西,但如果是,我找不到它.
我只是无法找到它,或者我是否需要自己编写?
谢谢