【发布时间】:2014-03-09 23:24:46
【问题描述】:
我有一个包含许多属性的非静态类
private static List<FileClass> FileClassList = new List<FileClass>();
internal void AddFile(Alphaleonis.Win32.Filesystem.FileInfo finfo)
{
this.FileID = FileIDCounter;
this.Name = finfo.Name;
this.FullName = finfo.FullName;
this.Attributes = GetFileAttributes(finfo);
this.CreationTime = finfo.CreationTime;
this.Extension = finfo.Extension;
this.isReadOnly = finfo.IsReadOnly;
this.LastAccessTime = finfo.LastAccessTime;
this.LastWriteTime = finfo.LastWriteTime;
this.Length = finfo.Length;
Interlocked.Increment(ref FileIDCounter); // As a Static value this is shared amongst all the instances of the class
// Interlocked.Increment is the Thread Safe way of saying FileIDCounter ++;
FileClassList.Add(this);
if (FileClassFileAdded != null) FileClassFileAdded(this);
}
虽然添加了类FileClassList.Add(this);最终结果是一个 FileClassList,其中填充了类的最后一个实例,而不是 this.Properties 值。
那么,如何将 FileClass 的当前实例添加到 FileClassList 中,以便 FileClassList 的内容包含 FileClass 的不同实例。
【问题讨论】:
-
您能说明一下您是如何尝试使用该列表的吗?
-
您希望您的列表包含什么?
this.Properties在您的 sn-p 中的任何地方都没有提到,所以我对您的列表没有它并不感到惊讶... -
@Ben 可能,this.Properties 是 this.FileID、Name 等。所以可以像 FileClassList[i].Name 一样访问它们...
-
@JleruOHeP 如果是这样,那么
FileClass的实例就可以了;没有更多信息,就不可能确切知道要问什么。
标签: c# list class static instance-variables