Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将非静态类添加到静态列表_C#_List_Class_Static_Instance Variables - Fatal编程技术网

C# 将非静态类添加到静态列表

C# 将非静态类添加到静态列表,c#,list,class,static,instance-variables,C#,List,Class,Static,Instance Variables,我有一个非静态类,它包含许多属性 private static List<FileClass> FileClassList = new List<FileClass>(); internal void AddFile(Alphaleonis.Win32.Filesystem.FileInfo finfo) { this.FileID = FileIDCounter; this.Name = finfo.Name; this.

我有一个非静态类,它包含许多属性

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);

   }
private static List FileClassList=new List();
内部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);//作为静态值,在类的所有实例之间共享
//Increment是表示FileIDCounter++的线程安全方式;
FileClassList.Add(这个);
如果(FileClassFileAdded!=null)FileClassFileAdded(this);
}
虽然类被添加到了FileClassList.Add(this);最后的结果是一个FileClassList,其中填充了类的最后一个实例,而不是this.Properties


那么,如何将FileClass的当前实例添加到FileClassList中,以便FileClassList的内容包含FileClass的不同实例。

我认为您的设计有点歪。我不认为AddFile应该是FileClassList的一部分。 然而,在莱乌没有另一个地方举行它。我要说的是:

   internal static void AddFile(Alphaleonis.Win32.Filesystem.FileInfo finfo, FileClass theClass)
   {
       theClass.FileID = FileIDCounter;
       theClass.Name = finfo.Name;
       theClass.FullName = finfo.FullName;
       theClass.Attributes = GetFileAttributes(finfo);
       theClass.CreationTime = finfo.CreationTime;
       theClass.Extension = finfo.Extension;
       theClass.isReadOnly = finfo.IsReadOnly;
       theClass.LastAccessTime = finfo.LastAccessTime;
       theClass.LastWriteTime = finfo.LastWriteTime;
       theClass.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(theClass);
       if (FileClassFileAdded != null) FileClassFileAdded(theClass);

   }
更好的方法是在FileClass上创建一个contrstuctor,该contrstuctor接受一个FileInfo并填充这些属性,然后像这样调用它:

var someFileClass = new FileClass(theFileInfo);
FileClass.AddFile(someClassFile);
而AddFile将是:

   internal static void AddFile(Alphaleonis.Win32.Filesystem.FileInfo finfo, FileClass theClass)
   {

       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(theClass);
       if (FileClassFileAdded != null) FileClassFileAdded(theClass);

   }

即使这样,我认为AddFile应该是调用方而不是被调用方的方法

以下是您最可能遇到的问题

具有以下特征:

  internal void AddFile(Alphaleonis.Win32.Filesystem.FileInfo finfo)
在某些地方,你可能会:

 MyClassWhichAddsAFile cls = new MyClassWhichAddsAFile();
 cls.AddFile(fileInfo1);
 cls.AddFile(fileInfo2);
 cls.AddFile(fileInfo3);
您需要(在此设计下)执行以下操作:


再说一次,我不是在这里讨论你的设计,也不是在讨论如何把它做好。我告诉你,你的问题可能来自于这种情况

你能展示一下你是如何使用这个列表的吗?你想让列表保存什么
this.Properties
在您的代码段中的任何地方都没有引用,因此我对您的列表中没有它并不感到惊讶…@Ben可能,this.Properties是this.FileID、Name等。因此可以像FileClassList[I]那样访问它们。Name…@JleruOHeP如果是这样的话,
FileClass
的实例就可以了;如果没有更多的信息,就不可能准确地知道被问到了什么。是的,我没有重新实例化这个类-很好!
 MyClassWhichAddsAFile cls1 = new MyClassWhichAddsAFile();
 cls1.AddFile(fileInfo1);
 MyClassWhichAddsAFile cls2 = new MyClassWhichAddsAFile();
 cls2.AddFile(fileInfo2);
 ........