I have a class that i want to use as follows: PersonCollection pc = PersonCollection.Load("person.xml"); Person p = new Person(); p.Name = "Test"; pc.Add(p); pc.Save();
my code works and compiles correctly, but CodeAnalysis is complaining that I have a static member on a generic type (i.e. my Load method) "CA1000: Do not declare static members on generic types"
Here is my code: public interface ISaveable { string FileName { get; set; } bool Dirty { get; set; } } public class MyCollection<TItem> : BindingList<TItem>, ISaveable { string _fileName; bool _dirty;
public string FileName { get { return _fileName; } set { _fileName = value; } } public bool Dirty { get { return _dirty; } set { _dirty = value; } } } public class SaveableCollection<TList, TItem> : MyCollection<TItem> where TList : ISaveable, new() { public SaveableCollection() { this.ListChanged += new ListChangedEventHandler(SaveableCollection_ListChanged); } void SaveableCollection_ListChanged(object sender, ListChangedEventArgs e) { this.Dirty = true; } public static TList Load(string fileName) { TList list = new TList();
//... Load code goes here
return list; } public void Save() { //... Save code goes here } }