C#.NET:当属性设置为无效值时,是否可能创建编译时警告? [英] C# .NET: Is it possible to create compile time warnings when property is set to invalid values?

查看:170
本文介绍了C#.NET:当属性设置为无效值时,是否可能创建编译时警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这只是一个可行性问题。我知道如果我说

This is just a feasibility question. I know that if I say

   int myInt = "5"; 

我得到一个编译时错误。我想做的是在对象上创建编译时错误或警告。所以,让我们说我有一个自定义对象有几个属性。其中一个属性不能为空,否则解决方案将无法编译:

I get a compile time error. What I want to do is create compile time errors or warnings on objects. So let's say I have a custom object with a few properties. One of the properties cannot be null otherwise the solution will not compile:

   public static class NoNullObjects
   {
       //NotNullable
       public static NotNullObject {get; set;}
   }

MyClass.cs:

MyClass.cs:

   Line#55   NoNullObjects.NotNullObject = null;

当我建立我想看到:

   Error: NotNullObject cannot be set to null. MyClass.cs Line 55.

有办法吗?

推荐答案

不,不是只有C#。 Microsoft的代码合同工作可能会给您您想要的: http://msdn.microsoft .com / en-us / devlabs / dd491992.aspx

No, not with just C#. Microsoft's Code Contracts work may give you what you want: http://msdn.microsoft.com/en-us/devlabs/dd491992.aspx.

这篇关于C#.NET:当属性设置为无效值时,是否可能创建编译时警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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