<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# 的属性中使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)
                    <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'>