我希望能够遍历引用其自身属性的类属性。为什么你会问?因为如果我想向该类添加更多属性,以后会更容易管理。
让我解释一下:
public interface IElementBox
{
string Filename { get; }
string FileDefinition { get; set; }
void ExtractFromFile(string stringData);
}
public abstract class Element
{
public Element(string stringData)
{
this.DefFromFile(stringData);
}
public string Name { get; set; }
protected abstract void DefFromFile(string stringData);
}
public class Solid : Element
{
public Solid(string stringData) : base(stringData) { }
public string SolidSpecificProperty { get; set; }
protected override void DefFromFile(string stringData)
{
// Assign SolidSpecificProperty from string
}
}
public class Liquid : Element
{
public Liquid(string stringData) : base(stringData) { }
public string LiquidSpecificProperty { get; set; }
protected override void DefFromFile(string stringData)
{
// Assign LiquidSpecificProperty from string
}
}
public class Gas : Element
{
public Gas(string stringData) : base(stringData) { }
public string GasSpecificProperty { get; set; }
protected override void DefFromFile(string stringData)
{
// Assign GasSpecificProperty from string
}
}
public abstract class ElementBox<T> : IElementBox where T : Element
{
public List<T> Elements { get; set; }
public List<T> GetElementsFromName(string name)
{
return this.Elements.FindAll(x => x.Name == name);
}
public abstract string Filename { get; }
public string FileDefinition { get; set; }
public abstract void ExtractFromFile(string filename);
}
public class SolidBox : ElementBox<Solid>
{
public override string Filename
{
get { return "Solid.txt"; }
}
public override void ExtractFromFile(string stringData)
{
this.Elements.Add(new Solid(stringData));
}
}
public class LiquidBox : ElementBox<Liquid>
{
public override string Filename
{
get { return "Liquid.txt"; }
}
public override void ExtractFromFile(string stringData)
{
this.Elements.Add(new Liquid(stringData));
}
}
public class GasBox : ElementBox<Gas>
{
public override string Filename
{
get { return "Gas.txt"; }
}
public override void ExtractFromFile(string stringData)
{
this.Elements.Add(new Gas(stringData));
}
}
public static class DataDefinition
{
public static SolidBox SolidBox { get; set; }
public static LiquidBox LiquidBox { get; set; }
public static GasBox GasBox { get; set; }
public static IElementBox[] ElementBoxes = new IElementBox[] { DataDefinition.SolidBox, DataDefinition.LiquidBox, DataDefinition.GasBox };
}
public static class Loader
{
public static void LoadInfo()
{
for (int elementBoxNb = 0; elementBoxNb < DataDefinition.ElementBoxes.Length; elementBoxNb++)
{
string dataFilepath = DataDefinition.ElementBoxes[elementBoxNb].Filename;
System.IO.StreamReader sr = System.IO.File.OpenText(dataFilepath);
DataDefinition.ElementBoxes[elementBoxNb].ExtractFromFile(sr.ReadToEnd());
}
}
}
此结构的全部目的是能够在文本文件中定义所有对象属性。这样所有 SolidBox.Elements 对象都是从该文本文件定义中动态分配的。
我的问题如下:
DataDefinition 中的属性数组是否会被值引用。这意味着我所有的数据分配都会被吸收到虚无中?
如果是(或否..),他们是否是完成整件事的更好方法/最好的方法是什么?
我已经在该数据结构上工作了一段时间,现在我为自己能取得的成就感到自豪。如果这一切都是徒劳的,我会很难过。不过,如果您能为我提供一种更好/最佳的方式来完成整个事情,我将不胜感激,并将我的代码放入我的存档文件夹中。
此外,请记住这是数据结构的示例。它并没有准确反射(reflect)我的代码的样子,而是为了便于理解和阅读。
如果需要更多信息,请随时提出问题。
最佳答案
反序列化(从持久性介质中读取对象)已通过无数种不同的方式得到解决。尝试使用数据契约(Contract),例如 this answer演示。您只需向您的属性添加一些属性以指示您想要序列化的内容,然后它就会为您完成几乎所有的工作。
关于c# - 自引用属性数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13243632/