Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6046577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:15:20+00:00 2026-05-23T07:15:20+00:00

I have such generic class of list: using System; using System.Collections; using System.Collections.Generic; using

  • 0

I have such generic class of list:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lab7
{
    public class MyListClass<T>: IEnumerable<T>
    {
        public delegate void MyDelegate();
        public event MyDelegate AddEvent;
        public event MyDelegate RemEvent;   

        List<T> list;

        public T this[int index]
        {
            get { return list[index]; }
            set { list[index] = value; }
        }

        public void Add(T item)
        {
            list.Add(item);
            if (AddEvent != null)
                AddEvent();   
        }

        public void Remove(T item)
        {
            list.Remove(item);
            if (RemEvent != null)
                RemEvent();   
        }

        public void RemoveAt(int index)
        {
            list.RemoveAt(index);
            if (RemEvent != null)
                RemEvent();   
        }

        public MyListClass()
        {
            list = new List<T>();
        }

        public MyListClass(List<T> list)
        {
            this.list = list;
        }

        public IEnumerator<T> GetEnumerator()
        {
            return list.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return list.GetEnumerator();
        }

        #region Events
        /*static void AddHandler()
        {
            Console.WriteLine("Объект добавлен в коллекцию"); 
        }
        static void RemoveHandler()
        {
            Console.WriteLine("Объект удалён из коллекции коллекцию");
        }*/
        #endregion

    }
}

Where i must put attribute to write something to different classes, according to type of this list? How do I write this attribute?

I didn’t understand sense of attributes in this lab.

If you want to see all lab, it’s here

Write generic class of list with
opportunity to generate events when
you call some class methods.
Information about events must e
written in some files, which must be
determinated by attached to this class
attribute.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:15:21+00:00Added an answer on May 23, 2026 at 7:15 am

    If I understand correctly, you are asked to write to a specified file information about the events fired when some specified methods are called. This file must be specified through a class level Attribute.

    I think (not easy to understand what you are asking) the Attribute should be defined in the generic type T.

    If thats what you are asking I would do something similar to the following:

    1. Define your custom attribute:

      [AttributeUsage(AttributeTargets.Class)] 
      public class LogFileAttribute: Attribute
      {
          readonly string path;
      
          public LogFileAttribute(string path)
          {
              this.path = path;
          }
      
          public string Path { get { return this.path; } }
      }
      
    2. Modify your generic list class:

      public class MyListClass<T> : IEnumerable<T>
      {
           public delegate void MyDelegate();
           public event MyDelegate AddEvent;
           public event MyDelegate RemEvent;
           private LogFileAttribute logFileAttribute;
      
           List<T> list;
      
           public T this[int index]
           {
               get { return list[index]; }
               set { list[index] = value; }
           }
      
           public void Add(T item)
           {
               list.Add(item);
               if (AddEvent != null)
               {
                    AddEvent();
               }
               if (this.logFileAttribute != null)
               {
                   logEvent("Add");
               }
           }
      
           public void Remove(T item)
           {
               list.Remove(item);
               if (RemEvent != null)
               {
                   RemEvent();
               }
               if (this.logFileAttribute != null)
               {
                   logEvent("Remove");
               }
           }
      
           public void RemoveAt(int index)
           {
               list.RemoveAt(index);
               if (RemEvent != null)
               {
                   RemEvent();
              }
              if (this.logFileAttribute != null)
              {
                  logEvent("RemoveAt");
              }
           }
      
           public MyListClass()
           {
               list = new List<T>();
               checkLogFileAttribute();
           }
      
           public MyListClass(List<T> list)
           {
               this.list = list;
               checkLogFileAttribute();
           }
      
           public IEnumerator<T> GetEnumerator()
           {
               return list.GetEnumerator();
           }
      
           IEnumerator IEnumerable.GetEnumerator()
           {
               return list.GetEnumerator();
           }
      
           private void logEvent(string methodName)
           {
               File.AppendAllLines(this.logFileAttribute.Path, new string[] { methodName + " called." });
           }
      
           private void checkLogFileAttribute()
           {
               object[] attributes = typeof(T).GetCustomAttributes(typeof(LogFileAttribute), false);
               if (attributes.Length > 0)
               {
                   this.logFileAttribute = (LogFileAttribute)attributes[0];
               }
           }
      
       }
      

    Now if you define the following class:

    [LogFile(@"C:\EventLog.txt")]
    public class Foo
    {
        readonly int id;
        public Foo(int id)
        {
            this.id = id;
        }
    
        public int Id { get { return this.id; } }
    }
    

    And run the following code:

    MyListClass<Foo> foos = new MyListClass<Foo>();
    foos.Add(new Foo(1));
    foos.Add(new Foo(2));
    

    you will get the desired behavior (if I understood your question correctly).

    EDIT: Removed Append form LogFileAttribute as it didn’t make any sense in this context.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example I have such query: Query q = sess.createQuery(from Cat cat); List cats
If I have a method such as: public void MyMethod(int arg1, string arg2) How
If I have class names such as left, right, clear and xhtml like <a
In a proof-of-concept project I'm working on, I have a generic abstract class that
I have a Repository Class with the following method... public T Single<T>(Predicate<T> expression) {
Lets say we have a program which contains such classes: public interface AbstractItem {
So i have such code. public interface IGeoDataSet<out T> : IDataSet where T :
I have the following generic question Here is a my generic message interface. public
I would like to make a class based on interfaces such as a list
I'm trying determine if it's possible in C# to have a generic class that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.