Присоедините существующий CustomAttribute к полю, используя C #

Если вы не хотите или не можете преобразовать в Observable Collection, попробуйте следующее:

public class EventList<T> : IList<T> /* NOTE: Changed your List<T> to IList<T> */
{
  private List<T> list; // initialize this in your constructor.
  public event ListChangedEventDelegate ListChanged;
  public delegate void ListChangedEventDelegate();

  private void notify()
  {
      if (ListChanged != null
          && ListChanged.GetInvocationList().Any())
      {
        ListChanged();
      }
  }

  public new void Add(T item)
  {
      list.Add(item);
      notify();
  }

  public List<T> Items {
    get { return list; } 
    set {
      list = value; 
      notify();
    }
  }
  ...
}

Теперь, для вашего свойства, вы сможете сократить свой код до этого:

public EventList List
{
  get { return m_List.Items; }
  set
  {
      //m_List.ListChanged -= List_ListChanged;

      m_List.Items = value;

      //m_List.ListChanged += List_ListChanged;
      //List_ListChanged();
  }
}

Почему? Установка чего-либо в EventList.Items вызовет вашу приватную notify().

0
задан Martien de Jong 14 March 2019 в 13:21
поделиться

2 ответа

Это невозможно. CustomAttribute должен создаваться динамически. Смотрите этот вопрос для получения дополнительной информации. TypeBuilder - Добавление атрибутов

0
ответ дан Martien de Jong 14 March 2019 в 13:21
поделиться

Вы можете построить CustomAttributeBuilder для вашего атрибута:

Type[] ctorParams = new Type[] { /*Types of your Attributes Constructor*/ };
ConstructorInfo classCtorInfo = typeof(ColumnAttribute).GetConstructor(ctorParams);

CustomAttributeBuilder myCABuilder = new CustomAttributeBuilder(
                        classCtorInfo,
                        /*arguments for your Attribute*/);

propertyBuilder.SetCustomAttribute(myCABuilder);
0
ответ дан Ackdari 14 March 2019 в 13:21
поделиться
Другие вопросы по тегам:

Похожие вопросы: