<tfoot id='SDer9'></tfoot>

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

      1. <legend id='SDer9'><style id='SDer9'><dir id='SDer9'><q id='SDer9'></q></dir></style></legend>

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

        在 C# 的属性中使用变量

        Using variables within Attributes in C#(在 C# 的属性中使用变量)
          <bdo id='52jIt'></bdo><ul id='52jIt'></ul>
          • <i id='52jIt'><tr id='52jIt'><dt id='52jIt'><q id='52jIt'><span id='52jIt'><b id='52jIt'><form id='52jIt'><ins id='52jIt'></ins><ul id='52jIt'></ul><sub id='52jIt'></sub></form><legend id='52jIt'></legend><bdo id='52jIt'><pre id='52jIt'><center id='52jIt'></center></pre></bdo></b><th id='52jIt'></th></span></q></dt></tr></i><div id='52jIt'><tfoot id='52jIt'></tfoot><dl id='52jIt'><fieldset id='52jIt'></fieldset></dl></div>

              <tfoot id='52jIt'></tfoot>

                  <tbody id='52jIt'></tbody>
              1. <legend id='52jIt'><style id='52jIt'><dir id='52jIt'><q id='52jIt'></q></dir></style></legend>

                  <small id='52jIt'></small><noframes id='52jIt'>

                  本文介绍了在 C# 的属性中使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我们有一些属性良好的数据库代码,如下所示:

                  We have some Well-Attributed DB code, like so:

                  [Display(Name = "Phone Number")]
                  public string Phone { get; set; }
                  

                  由于它非常通用,我们想再次使用它,但在属性的名称部分使用不同的字符串.由于它是一个属性,它似乎希望事物是 const,所以我们尝试了:

                  Since it is quite generic we'd like to use it again, but with a different string in the Name part of the attribute. Since it's an attribute it seems to want things to be const, so we tried:

                  const string AddressType = "Student ";
                  [Display(Name = AddressType + "Phone Number")]
                  public string Phone { get; set; }
                  

                  这似乎没问题,除了拥有一个 const 字符串意味着我们不能在任何基类中覆盖它,从而删除了我们最初打算添加的功能,并暴露了我的问题:

                  This seems to work alright, except that having a const string means we can't overwrite it in any base classes, thereby removing the functionality that we originally were intending to add, and exposing my question:

                  有没有办法在属性内部使用某种变量,以便我们可以继承和保留属性装饰?

                  Is there a way to use some sort of variable inside of an attribute so that we can inherit and keep the attribute decorations?

                  推荐答案

                  编译器必须在编译时知道属性中的所有内容.变量本质上是变量 (!),因此不能在属性中使用.

                  Everything inside an attribute must be known to the compiler at compile-time. Variables are inherently variable (!) so can't be used in attributes.

                  如果您可以使用代码生成工具,您就可以将不同的(常量)值动态地注入到每个派生类中.

                  If you can use a code generation tool, you'd be able to dynamically inject different (constant) values into each derived class.

                  这篇关于在 C# 的属性中使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 中的代码生成属性相关联)
                    <tfoot id='kiVWy'></tfoot>
                      <tbody id='kiVWy'></tbody>

                    • <legend id='kiVWy'><style id='kiVWy'><dir id='kiVWy'><q id='kiVWy'></q></dir></style></legend>

                        • <bdo id='kiVWy'></bdo><ul id='kiVWy'></ul>

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