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

  • <tfoot id='V0JPj'></tfoot>

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

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

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

        如何将我的属性添加到代码生成的 Linq2Sql 类属性?

        How can I add my attributes to Code-Generated Linq2Sql classes properties?(如何将我的属性添加到代码生成的 Linq2Sql 类属性?)

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

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

            <tbody id='sgrfS'></tbody>
            <tfoot id='sgrfS'></tfoot>

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

                  本文介绍了如何将我的属性添加到代码生成的 Linq2Sql 类属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想向 Linq 2 Sql 类属性添加属性.比如这个 Column 在 UI 中是可浏览的,或者在 UI 中是只读的等等.

                  I would like to add attributes to Linq 2 Sql classes properties. Such as this Column is browsable in the UI or ReadOnly in the UI and so far.

                  我考虑过使用模板,有人知道怎么用吗?还是别的什么?

                  I've thought about using templates, anybody knows how to use it? or something different?

                  一般来说,您会通过代码生成的类来解决这个问题吗?

                  Generally speaking, would do you do to address this issue with classes being code-generated?

                  推荐答案

                  根据要求,这是一种使用 CustomTypeDescriptor 在运行时编辑属性的方法;这里的例子是win-forms,但是应该很简单的把它换成WPF看看它是否有效......

                  As requested, here's an approach using a CustomTypeDescriptor to edit the attributes at run-time; the example here is win-forms, but it should be pretty simple to swap it into WPF to see if it works...

                  using System;
                  using System.Collections.Generic;
                  using System.ComponentModel;
                  using System.Windows.Forms;
                  // example POCO
                  class Foo {
                      static Foo()
                      {   // initializes the custom provider (the attribute-based approach doesn't allow
                          // access to the original provider)
                          TypeDescriptionProvider basic = TypeDescriptor.GetProvider(typeof(Foo));
                          FooTypeDescriptionProvider custom = new FooTypeDescriptionProvider(basic);
                          TypeDescriptor.AddProvider(custom, typeof(Foo));
                      }
                      public string Name { get; set; }
                      public DateTime DateOfBirth { get; set; }
                  }
                  // example form
                  static class Program {
                      [STAThread]
                      static void Main() {
                          Application.EnableVisualStyles();
                          Application.Run( new Form {
                                  Controls = {
                                      new DataGridView {
                                          Dock = DockStyle.Fill,
                                          DataSource = new BindingList<Foo> {
                                              new Foo { Name = "Fred", DateOfBirth = DateTime.Today.AddYears(-20) }
                                          }
                                      }
                                  }
                              });
                      }
                  }
                  
                  class FooTypeDescriptionProvider : TypeDescriptionProvider
                  {
                      ICustomTypeDescriptor descriptor;
                      public FooTypeDescriptionProvider(TypeDescriptionProvider parent) : base(parent) { }
                      public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
                      {   // swap regular descriptor for bespoke (Foo) descriptor
                          if (descriptor == null)
                          {
                              ICustomTypeDescriptor desc = base.GetTypeDescriptor(typeof(Foo), null);
                              descriptor = new FooTypeDescriptor(desc);
                          }
                          return descriptor;
                      }
                  }
                  class FooTypeDescriptor : CustomTypeDescriptor
                  {
                      internal FooTypeDescriptor(ICustomTypeDescriptor parent) : base(parent) { }
                      public override PropertyDescriptorCollection GetProperties()
                      {   // wrap the properties
                          return Wrap(base.GetProperties());
                      }
                      public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
                      {   // wrap the properties
                          return Wrap(base.GetProperties(attributes));
                      }
                  
                      static PropertyDescriptorCollection Wrap(PropertyDescriptorCollection properties)
                      {
                          // here's where we have an opportunity to swap/add/remove properties
                          // at runtime; we'll swap them for pass-thru properties with
                          // edited atttibutes
                          List<PropertyDescriptor> list = new List<PropertyDescriptor>(properties.Count);
                          foreach (PropertyDescriptor prop in properties)
                          {
                              // add custom attributes here...
                              string displayName = prop.DisplayName;
                              if (string.IsNullOrEmpty(displayName)) displayName = prop.Name;
                  
                              list.Add(new ChainedPropertyDescriptor(prop, new DisplayNameAttribute("Foo:" + displayName)));
                          }
                          return new PropertyDescriptorCollection(list.ToArray(), true);
                      }
                  }
                  
                  
                  class ChainedPropertyDescriptor : PropertyDescriptor
                  {
                      // this passes all requests through to the underlying (parent)
                      // descriptor, but has custom attributes etc;
                      // we could also override properties here...
                      private readonly PropertyDescriptor parent;
                      public ChainedPropertyDescriptor(PropertyDescriptor parent, params Attribute[] attributes)
                          : base(parent, attributes)
                      {
                          this.parent = parent;
                      }
                      public override bool ShouldSerializeValue(object component) { return parent.ShouldSerializeValue(component); }
                      public override void SetValue(object component, object value) { parent.SetValue(component, value); }
                      public override object GetValue(object component) { return parent.GetValue(component); }
                      public override void ResetValue(object component) { parent.ResetValue(component); }
                      public override Type PropertyType {get { return parent.PropertyType; } }
                      public override bool IsReadOnly { get { return parent.IsReadOnly; } }
                      public override bool CanResetValue(object component) {return parent.CanResetValue(component);}
                      public override Type ComponentType { get { return parent.ComponentType; } }
                      public override void AddValueChanged(object component, EventHandler handler) {parent.AddValueChanged(component, handler);  }
                      public override void RemoveValueChanged(object component, EventHandler handler) { parent.RemoveValueChanged(component, handler); }
                      public override bool SupportsChangeEvents { get { return parent.SupportsChangeEvents; } }
                  }
                  

                  这篇关于如何将我的属性添加到代码生成的 Linq2Sql 类属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 中的代码生成属性相关联)
                1. <legend id='Lom9r'><style id='Lom9r'><dir id='Lom9r'><q id='Lom9r'></q></dir></style></legend>

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

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

                              <tbody id='Lom9r'></tbody>

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