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

      1. <small id='xiQ6q'></small><noframes id='xiQ6q'>

      2. 我可以在运行时向对象属性添加属性吗?

        Can I add attributes to an object property at runtime?(我可以在运行时向对象属性添加属性吗?)

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

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

                <bdo id='xBs0D'></bdo><ul id='xBs0D'></ul>
                • <tfoot id='xBs0D'></tfoot>
                • <i id='xBs0D'><tr id='xBs0D'><dt id='xBs0D'><q id='xBs0D'><span id='xBs0D'><b id='xBs0D'><form id='xBs0D'><ins id='xBs0D'></ins><ul id='xBs0D'></ul><sub id='xBs0D'></sub></form><legend id='xBs0D'></legend><bdo id='xBs0D'><pre id='xBs0D'><center id='xBs0D'></center></pre></bdo></b><th id='xBs0D'></th></span></q></dt></tr></i><div id='xBs0D'><tfoot id='xBs0D'></tfoot><dl id='xBs0D'><fieldset id='xBs0D'></fieldset></dl></div>
                  本文介绍了我可以在运行时向对象属性添加属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  例如,我想删除或更改以下属性属性或添加新属性.有可能吗?

                  For example I want to remove or change below property attributes or add a new one. Is it possible?

                  [XmlElement("bill_info")]
                  [XmlIgnore]
                  public BillInfo BillInfo
                  {
                    get { return billInfo; }
                    set { billInfo = value; }
                  }
                  

                  推荐答案

                  (编辑 - 我误读了原来的问题)

                  (edit - I misread the original question)

                  您不能添加实际属性(它们被烧录到 IL 中);但是,使用 XmlSerializer 您不必这样做 - 您可以在构造函数中为 XmlSerializer 提供其他属性.但是,如果这样做,您需要小心地缓存 XmlSerializer 实例,否则它将为每个实例创建一个额外的程序集,这有点泄漏.(如果您使用仅采用 Type 的简单构造函数,则不会这样做).查看 XmlAttributeOverrides.

                  You cannot add actual attributes (they are burned into the IL); however, with XmlSerializer you don't have to - you can supply additional attributes in the constructor to the XmlSerializer. You do, however, need to be a little careful to cache the XmlSerializer instance if you do this, as otherwise it will create an additional assembly per instance, which is a bit leaky. (it doesn't do this if you use the simple constructor that just takes a Type). Look at XmlAttributeOverrides.

                  举个例子:

                  using System;
                  using System.Xml.Serialization;
                   public class Person
                  {
                      static void Main()
                      {
                          XmlAttributeOverrides overrides = new XmlAttributeOverrides();
                          XmlAttributes attribs = new XmlAttributes();
                          attribs.XmlIgnore = false;
                          attribs.XmlElements.Add(new XmlElementAttribute("personName"));
                          overrides.Add(typeof(Person), "Name", attribs);
                  
                          XmlSerializer ser = new XmlSerializer(typeof(Person), overrides);
                          Person person = new Person();
                          person.Name = "Marc";
                          ser.Serialize(Console.Out, person);
                      }
                      private string name;
                      [XmlElement("name")]
                      [XmlIgnore]
                      public string Name { get { return name; } set { name = value; } }
                  }
                  

                  请注意;如果 xml 属性只是说明性的,那么有一种 second 方法可以为与数据绑定相关的事物添加属性,方法是使用 TypeDescriptor.CreatePropertyICustomTypeDescriptorTypeDescriptionProvider.恐怕比 xml 的情况复杂得多 - 并且不适用于所有代码 - 只是使用组件模型的代码.

                  Note also; if the xml attributes were just illustrative, then there is a second way to add attributes for things related to data-binding, by using TypeDescriptor.CreateProperty and either ICustomTypeDescriptor or TypeDescriptionProvider. Much more complex than the xml case, I'm afraid - and doesn't work for all code - just code that uses the component-model.

                  这篇关于我可以在运行时向对象属性添加属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

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

                      • <small id='Dnv8Y'></small><noframes id='Dnv8Y'>

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

                            <tfoot id='Dnv8Y'></tfoot>