开发者

c# attributes and generic--list

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.


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.


if i've decoded your question correctly u need something like this:

[
YouFileDeterminant(@"c:\first.log"),
YouFileDeterminant(@"d:\second.log")
]
public class MyListClass<T>: IEnumerable<T>
{
...
}

then before firing the event u should get those attribute(s) to determine files

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜