如何设置类实例成员上的编译时验证 [英] how to set compile time validation on class instance members

查看:109
本文介绍了如何设置类实例成员上的编译时验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个类如下:

class myclass
{
  Public  int myint;
}



现在我想限制myint的范围(最小5和最大10) 。

Now I want to restrict the range of myint(min 5 and max 10).

我想如果任何一个设置myint的值不在范围内,它将给出编译时错误(不是运行时错误)
请帮助

I want if any one set the value of myint which is not in the range it will give a compile time error(not runtime error) please help me how to achive it.

推荐答案

这是实验性的,但你可以看看 Spec#。它可以为您提供更多的编译时检查。代码合约是另一个选项(但只能在Visual Studio的某些版本中使用)。

It's experimental, but you could look into Spec#. It can provide you with more compile-time checks. Code Contracts are another option (but that's only available in certain editions of Visual Studio).

编辑

看起来像代码联系人现在可以作为独立下载获得!请参见此页

Looks like code contacts may now be available as a standalone download! See this page!

这篇关于如何设置类实例成员上的编译时验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆