我有相對經常的方法,我只想填充參數作爲輸入。如果有人調用該方法,並且參數不正確,則應該拋出錯誤。有沒有辦法在c#中註釋允許的值#
有沒有一種方法來註釋一個方法來說:只允許某些值範圍,或者它們應該不爲空?
隨着泛型有像「where」子句的限制(迄今還沒有價值)。
所以我想要做的,而不是
private static void DoSomething(string string_in, object object_in,...)
{
if (null == object_in) throw new NullReferenceException("Input parameter of object is empty.");
if (String.IsNullOrEmpty(string)) throw new NullReferenceException("Input parameter string is empty.");
像
private static void DoSomething(string string_in, object object_in,...)
where string _in:!String.IsNullOrEmpty(string_in)
where object_in : object_in !=null
或
private static void DoSomething(string string_in != null, object object_in != null,...)
或(這也是我最喜歡的)
[Restriction string_in: value != null, value != empty]
[Restriction object_in: value != null]
[Restriction int_in: value inRange 3..9]
private static void DoSomething(string string _in, object object_in,...)
所以總之有沒有更好的方法來限制被調用的類型的一定數量的值,然後只是手動反覆比較一些東西?
還有[代碼合同](http://msdn.microsoft.com/en-us/library/dd264808.aspx) – Giedrius
謝謝,看起來很有希望(爲什麼它是一個評論,而不是一個答案?) – Offler