‘.NET’ カテゴリーのアーカイブ

IronPythonとジェネリックと初期値わたし
- 2013/08/12
- murave
- IronPython
C#勉強会に行って自己紹介で「MVVMパターン用のフレームワーク使ってViewとViewModelはC#、ModelはIronPythonで書います」と言ったら微妙な空気になりました、夏。
muraveです。
というわけでC#とIronPythonを組み合わせて書いてます。基本的に配列や辞書は便利なのでPythonのモノ使ってましたがパフォーマンス・チューニングやっててジェネリック使ったほうがいいところもあるかな、と。
IronPython側でのジェネリックの書き方は知っててたまに使ってたんですが(C#での<>が[]になります)、試したら普通に初期値も与えられるのにちょっと感激。
C#で書くとList<string>の場合
>>> import site
>>> import clr
>>> from System.Collections import Generic as DotNetCollectionsG
>>> dot_l = DotNetCollectionsG.List[str]()
>>> print dot_l
List[str]()
>>> dot_l = DotNetCollectionsG.List[str](['a', 'b'])
>>> print dot_l
List[str](['a', 'b'])
C#で書くとDictionary<string, int>の場合
>>> import site
>>> import clr
>>> from System.Collections import Generic as DotNetCollectionsG
>>> dot_dict = DotNetCollectionsG.Dictionary[str,int]()
>>> print dot_dict
Dictionary[str, int]()
>>> dot_dict = DotNetCollectionsG.Dictionary[str,int]({'a':1 ,'b':1})
>>> print dot_dict
Dictionary[str, int]({'b' : 1, 'a' : 1})
ね♪
helpみたら普通に書いてありました。
>>> help(dot_dict)
Help on Dictionary[str, int] object:
class Dictionary[str, int](object)
| Dictionary[str, int]()
| Dictionary[str, int](capacity: int)
| Dictionary[str, int](comparer: IEqualityComparer[str])
| Dictionary[str, int](capacity: int, comparer: IEqualityComparer[str])
| Dictionary[str, int](dictionary: IDictionary[str, int])
| Dictionary[str, int](dictionary: IDictionary[str, int], comparer: IEqualityComparer[str])
|
以下略
えらいなぁ。






VB.NET + NUnit 2.5 の Assert.Throws で例外のテスト
[ブログ紹介] NUnit 2.5 で追加された Assert.Throws: TDD.NET
を参考に VB.NET の書き方にしただけですが。
Imports NUnit.Framework
Public Class ExceptionTest
Private Shared Function throwArgumentException(ByVal obj As Object) As String
Throw New ArgumentException("VarType Error: " & TypeName(obj))
End Function
Public Sub ArgumentExceptionTest(ByVal obj As Object, ByVal expected As String)
Dim ex = Assert.Throws(Of ArgumentException)(
Function() throwArgumentException(obj)
)
Assert.That(ex.Message = expected)
End Sub
End Class
サンプルコードは実際のコードを編集して書きましたが動作確認してないので参考程度に。
.NET のサンプルってC#の場合が多いので「VB.NETではどう書けばいいのかなぁ」ってことも結構あります。






VS2010+NUnit2.5.10でテストメソッドをデバッグ
VB.NET+NUnitで開発をしていますが、テストをしてもメソッドの戻り値しか確認できず不便だったので、テストメソッドもデバッグできるようにしました。
1.
ソリューションのプロパティからスタートアッププロジェクトをテストプロジェクトに変更。
マルチスタートアップでも可。
2.
テストプロジェクトのプロパティを開いてデバッグの開始動作を[外部プログラムの開始]に変更。
ファイル選択からnunit.exeを指定。
3.
開始オプションのコマンドライン引数にテストプロジェクトのdllを指定。
これでできるはずだったのですが、起動はしてもブレークポイントに反応してくれません。
調べてみると、
[VS2010][NAgile] VisualStudio2010 Express + NUnit でDebug実行ができない場合
こちらのページを発見しました。
どうやら.NET Framework 4.0だとデバッグ実行ができないみたいです。
なので上のページを参考にさせていただいて、nunit.exe.configを編集。
4.
nunit.exe.configのconfiguration内に下の3行を追加。
上書きして実行してみると、ちゃんとブレークポイントに反応するようになりました!




