• <i id='k9sM6'><tr id='k9sM6'><dt id='k9sM6'><q id='k9sM6'><span id='k9sM6'><b id='k9sM6'><form id='k9sM6'><ins id='k9sM6'></ins><ul id='k9sM6'></ul><sub id='k9sM6'></sub></form><legend id='k9sM6'></legend><bdo id='k9sM6'><pre id='k9sM6'><center id='k9sM6'></center></pre></bdo></b><th id='k9sM6'></th></span></q></dt></tr></i><div id='k9sM6'><tfoot id='k9sM6'></tfoot><dl id='k9sM6'><fieldset id='k9sM6'></fieldset></dl></div>

      • <bdo id='k9sM6'></bdo><ul id='k9sM6'></ul>
      <tfoot id='k9sM6'></tfoot>

      <legend id='k9sM6'><style id='k9sM6'><dir id='k9sM6'><q id='k9sM6'></q></dir></style></legend>

      <small id='k9sM6'></small><noframes id='k9sM6'>

        为什么是“十进制"?不是有效的属性参数类型?

        Why quot;decimalquot; is not a valid attribute parameter type?(为什么是“十进制?不是有效的属性参数类型?)

          <bdo id='0ZiCI'></bdo><ul id='0ZiCI'></ul>
              <tbody id='0ZiCI'></tbody>

                <small id='0ZiCI'></small><noframes id='0ZiCI'>

                <tfoot id='0ZiCI'></tfoot>
                <legend id='0ZiCI'><style id='0ZiCI'><dir id='0ZiCI'><q id='0ZiCI'></q></dir></style></legend>

                  <i id='0ZiCI'><tr id='0ZiCI'><dt id='0ZiCI'><q id='0ZiCI'><span id='0ZiCI'><b id='0ZiCI'><form id='0ZiCI'><ins id='0ZiCI'></ins><ul id='0ZiCI'></ul><sub id='0ZiCI'></sub></form><legend id='0ZiCI'></legend><bdo id='0ZiCI'><pre id='0ZiCI'><center id='0ZiCI'></center></pre></bdo></b><th id='0ZiCI'></th></span></q></dt></tr></i><div id='0ZiCI'><tfoot id='0ZiCI'></tfoot><dl id='0ZiCI'><fieldset id='0ZiCI'></fieldset></dl></div>
                1. 本文介绍了为什么是“十进制"?不是有效的属性参数类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这真的令人难以置信,但却是真实的.此代码将不起作用:

                  It is really unbelievable but real. This code will not work:

                  [AttributeUsage(AttributeTargets.Property|AttributeTargets.Field)]
                  public class Range : Attribute
                  {
                      public decimal Max { get; set; }
                      public decimal Min { get; set; }
                  }
                  
                  public class Item
                  {
                      [Range(Min=0m,Max=1000m)]  //compile error:'Min' is not a valid named attribute argument because it is not a valid attribute parameter type 
                      public decimal Total { get; set; }  
                  }
                  

                  虽然这有效:

                  [AttributeUsage(AttributeTargets.Property|AttributeTargets.Field)]
                  public class Range : Attribute
                  {
                      public double Max { get; set; }
                      public double Min { get; set; }
                  }
                  
                  public class Item
                  {
                      [Range(Min=0d,Max=1000d)]
                      public decimal Total { get; set; }  
                  }
                  

                  谁能告诉我为什么 double 可以,而 decimal 不行.

                  Who can tell me why double is OK while decimal is not.

                  推荐答案

                  这是一个 CLR 限制.仅有的原始常量或数组原语可以用作属性参数.原因是一个属性必须完全编码在元数据.这不同于一个用 IL 编码的方法体.使用元数据只会严格限制可以使用的值的范围.在当前版本的 CLR 中,元数据值仅限于原语、null、类型和数组原语(可能错过了一个未成年人一).

                  This is a CLR restriction. Only primitive constants or arrays of primitives can be used as attribute parameters. The reason why is that an attribute must be encoded entirely in metadata. This is different than a method body which is coded in IL. Using MetaData only severely restricts the scope of values that can be used. In the current version of the CLR, metadata values are limited to primitives, null, types and arrays of primitives (may have missed a minor one).

                  取自 this 答案JaredPar.

                  基本类型不是小数原始类型,因此不能以元数据表示,可防止它不是一个属性参数.

                  Decimals while a basic type are not a primitive type and hence cannot be represented in metadata which prevents it from being an attribute parameter.

                  这篇关于为什么是“十进制"?不是有效的属性参数类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                  Accessing attribute info from DTE(从 DTE 访问属性信息)
                  c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                  Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                  How can I force the PropertyGrid to show a custom dialog for a specific property?(如何强制 PropertyGrid 显示特定属性的自定义对话框?)
                  Associate attribute with code generated property in .net(将属性与 .net 中的代码生成属性相关联)
                    • <i id='MXBLr'><tr id='MXBLr'><dt id='MXBLr'><q id='MXBLr'><span id='MXBLr'><b id='MXBLr'><form id='MXBLr'><ins id='MXBLr'></ins><ul id='MXBLr'></ul><sub id='MXBLr'></sub></form><legend id='MXBLr'></legend><bdo id='MXBLr'><pre id='MXBLr'><center id='MXBLr'></center></pre></bdo></b><th id='MXBLr'></th></span></q></dt></tr></i><div id='MXBLr'><tfoot id='MXBLr'></tfoot><dl id='MXBLr'><fieldset id='MXBLr'></fieldset></dl></div>

                        <bdo id='MXBLr'></bdo><ul id='MXBLr'></ul>

                            <tbody id='MXBLr'></tbody>
                          <legend id='MXBLr'><style id='MXBLr'><dir id='MXBLr'><q id='MXBLr'></q></dir></style></legend>

                          <small id='MXBLr'></small><noframes id='MXBLr'>

                          <tfoot id='MXBLr'></tfoot>