简体   繁体   English

如何强制编译器检查是否设置了特定属性

[英]How to force the compiler to check if a specific property is set

In the below class I want to force the compiler to check the PropC whether its set, and if not, show the error in compile mode. 在下面的类中,我要强制编译器检查PropC是否设置了它,如果没有,则在编译模式下显示错误。

public class ClassA 
{
    public object PropA { get; set; }
    public object PropB { get; set; }
    public object PropC { get; set; }        
}

I know that I can use the PostShart to create custom attribute, but I want to use .NET built in class. 我知道我可以使用PostShart创建自定义属性,但是我想使用内置在类中的.NET。

This is not a default feature of the .NET framework. 这不是.NET框架的默认功能。

However, you can use the Code Contracts package to ensure Object Invariants . 但是,您可以使用Code Contracts包来确保Object Invariants

You can configure the settings to run Static Code Analysis on your classes to ensure that verifiable invariants are met. 您可以配置设置以在您的类上运行静态代码分析 ,以确保满足可验证的不变式。 As a fall back, you will get runtime safety of these contracts, because accession of the property with an invalid return value will result in a runtime error or debug assertion. 作为回退,您将获得这些合同的运行时安全性,因为使用无效的返回值访问属性将导致运行时错误或调试断言。 This is accomplished by the Code Contract package performing rewriting of your code during compilation and inserting additional code to validate your contracts. 这是通过代码合同程序包完成的,该程序包在编译过程中执行代码的重写并插入其他代码以验证您的合同。

Note, that these behaviors are all dependent upon the settings that you configure. 请注意,这些行为都取决于您配置的设置。 The package is too complex to explain in a stackoverflow answer, but you can get read the manual/documentation at http://research.microsoft.com/en-us/projects/contracts/userdoc.pdf . 该软件包太复杂,无法在stackoverflow答案中进行解释,但是您可以在http://research.microsoft.com/zh-cn/projects/contracts/userdoc.pdf上阅读手册/文档。

I though it would be similar to check if your program will finish , but I think you want much more scoped version: "if my object created make sure the property is set before object leaves the function it was created in". 我虽然检查程序是否将完成将是相似的,但是我认为您需要更大范围的版本:“如果创建了我的对象,请确保在对象离开其创建功能之前设置属性”。 It is possible in theory but C# compiler does not provide a way to do it. 从理论上讲这是可能的,但是C#编译器没有提供实现此目的的方法。 I believe broader check "if property assigned before usage" would not be possible at compile time at all. 我相信在编译时根本不可能进行更广泛的检查“如果在使用之前分配了属性”。

Refactoring maybe better approach - if you simply not allow creating invalid objects it is much easier to prove that such objects are not present in the program. 重构也许是更好的方法-如果您根本不允许创建无效的对象,则更容易证明程序中不存在此类对象。 Ie factory method pattern maybe one option: 工厂方法模式可能是一种选择:

public class ClassA 
{
   public object PropC { get; private set; }        
   private ClassA (){};
   public Create (object propC)
   {
      return new ClassA{ PropC = propC };
   }
 }

What you are asking for is only available at run-time, not compile time. 您所要求的仅在运行时可用,而在编译时不可用。 You should use unit tests to check if the property is being set. 您应该使用单元测试来检查是否设置了属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM