简体   繁体   English

我可以限制类型只包含可为空的属性吗?

[英]Can I constraint the type to only contain nullable properties?

I have such class where all properties must be nullable types. 我有一个这样的类,其中所有属性都必须是可为null的类型。 Is it possible to add design(not runtime) time validation for Sessions class properties to check is added new property has nullable type? 是否可以为Sessions类属性添加设计(而非运行时)时间验证,以检查是否添加了具有可空类型的新属性? The compiler should give error and not compile code if property will not have nulable type. 如果属性不具有可空类型,则编译器应给出错误且不编译代码。

public class Sessions : SessionInfo<Sessions>
{
    public int? UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string OldEmail { get; set; }
    public bool? UserManager { get; set; }
    public bool? UserManagerVisible { get; set; }
    public bool? TransactionCall { get; set; }        
}

At first, you are slightly incorrect because in your example only int? 起初,您有点不正确,因为在您的示例中,仅是int? and bool? bool? properties are nullable . 属性是可以为null的 String is a reference type , it can accept null (like any reference type) but it isn't nullable per se (in the C# sense of the word ). String是一种引用类型 ,它可以接受null (就像任何引用类型一样),但是它本身不能为null (在C#的意义上 )。

Secondly, no, there isn't any way to “check” the class definition at compile type and frankly it doesn't make any sense to me. 其次,不,没有任何方法可以“检查”编译类型的类定义,坦率地说,这对我没有任何意义。 Do you expect this restriction to be coded somewhere? 您是否希望将此限制编码在某个地方? So what if you decide to drop it eventually? 那么,如果您决定最终放弃它,该怎么办? Would you change this “constraining” code? 您会更改此“约束”代码吗? But then what is the point of the restriction if it can be lifted by just editing the code? 但是,如果可以仅通过编辑代码来解除限制,那么限制的意义何在? It's much like installing a lock on the door only to keep keys in it. 这就像在门上安装锁只是为了保持钥匙锁。

Yes, you can work around this by using a static analyzer tool like FxCop, as suggested by Anders , but from my point of view the very need for this highlights a design problem in your code. 是的,您可以按照Anders的建议使用FxCop之类的静态分析器工具来解决此问题,但是从我的角度来看,对此的需求非常突出了代码中的设计问题 Such rules are usually created to enforce certain design policy on the project level, not to constrain a single class in your code. 通常创建此类规则是为了在项目级别实施某些设计策略,而不是约束代码中的单个类。

I'm curious about the role of SessionInfo<T> . 我对SessionInfo<T>的角色感到好奇。 Does it go over each property with Reflection? 是否通过反射遍历每个属性? If you explained your original problem, we could help you come up with a better solution. 如果您解释了最初的问题,我们可以帮助您提出更好的解决方案。 The use of Curiously Recurring Template Pattern also suggests that you may be the type of person to oversolve the problems . 使用“ 好奇地重复出现的模板模式”还表明您可能是那种过度解决问题的人

What are you trying to accomplish? 你想达到什么目的?

First write a custom FxCop rule for this (either hard code the rule to check only the Sessions class, or use a custom attribute that the FxCop rule will use to find the classes in question. There is a good tutorial on writing custom FxCop rules at binarycoder. Here is an example where the rule checks the naming of every field in a class http://www.binarycoder.net/fxcop/html/ex_classfieldnameprefixes.html 首先为此编写一个自定义FxCop规则(或者对规则进行硬编码以仅检查Sessions类,或者使用FxCop规则将用于查找相关类的自定义属性。有关如何编写自定义FxCop规则的教程很不错,网址为这是一个示例,其中该规则检查类http://www.binarycoder.net/fxcop/html/ex_classfieldnameprefixes.html中每个字段的命名

Once your rule is written, right click your project -> properties. 编写规则后,右键单击项目->属性。 Go to the Code Analysis tab. 转到“代码分析”选项卡。 Check "Run code analysis on build". 选中“在构建时运行代码分析”。 Make sure the Rule Set includes your custom rule. 确保规则集包含您的自定义规则。

Now FxCop will run static code analysis on each compile of the project, and warnings will be reported in the "Error List" tab in visual studio, together with compilation warnings. 现在,FxCop将在项目的每次编译中运行静态代码分析,并且警告将与编译警告一起报告在Visual Studio的“错误列表”选项卡中。

暂无
暂无

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

相关问题 如何约束通用类型参数以仅接受C#中的可为空的值类型? - How do I constraint a generic type parameter to only accept nullable value types in C#? 如何使派生类具有所有基本数据类型属性都为Nullable类型的强制性? - How can I make mandatory for a derived class to have all basic data-type properties to be of Nullable types? 如何将“任何不可为空类型”指定为泛型类型参数约束? - How do I specify "any non-nullable type" as a generic type parameter constraint? 一切可以为空的 C# 泛型类型约束 - C# generic type constraint for everything nullable 为什么Nullable类型必须具有struct约束 - Why Nullable type must have struct constraint EF Core:我可以在我的实体中将 map 可空 SQL 类型转换为不可空值类型吗? - EF Core: Can I map nullable SQL type to non-nullable value type in my Entity? 模型是否可以/应该仅包含用于单元测试的属性/变量? - Can/should model contain properties/variables only for unit testing? 在对不同类型的属性创建唯一约束时,如何解决错误“找不到隐式类型数组的最佳类型”? - How can I resolve error 'No best type found for implicitly-typed array' while creating unique constraint on properties of different types? 将 int 或 string 作为泛型类型传递并在可空和不可空属性中使用的接口 - Interface passing int or string as generic type and using in nullable and not nullable properties 如何显示可为null的DateTime类型的字符串表示形式? - How can I show a string representation of a nullable DateTime type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM