Im 正在设计一个双形应用程序, 需要能够将不同类型属性的值输出到序列/ 数字端口 。
我在想,在运行时,这个工程中的所有班级是否都有一个简单的循环方式,然后向用户显示一组选择的集合(也许通过使用属性?)及其相应的字符串值,这样他们就可以输出这些值的任何组合。
所以,对于(简化的)例子来说, 说我有两个班级:
public class ClassA
{
public double Price{get;set;}
public int Units{get;set}
//other properties that I don t want visible to the user
}
public class ClassB
{
public string Name{get;set;}
public string Description{get;set;}
//other properties not visible to the user
}
我将有许多这类班级在运行时即刻进行(它们不一定相互连接/参照/相关)。
例如:
public ClassA Cars = new ClassA();
public ClassB Models = new ClassB();
public ClassA PCs = new ClassA();
//set properties of these instances
//etc.. more of these
我要收集所有这些实例,并向用户展示相应的字段:
Cars.Price = "1000";
Cars.Units = "3";
Models.Name = "BMW";
Models.Description = "Luxury Car";
PCs.Price = "3000";
PCs.Units = "20";
Note that the values will be changing at runtime, and this will need to be updated when sending out data. I was hoping someone could point me in the right direction here.
编辑 编辑 编辑 编辑 编辑 编辑
看来这也许不可行
是否有一种方法我可以存储一个对象列表, 这些对象指向我的属性/ 字段, 这样我就可以检索它的价值? 当然假设这些类实例将只创建一次, 我可以在我的输出类中注册分类属性/ 字段。 然后输出类可以复制到这个列表中, 获取它指向的对象的值, 然后发送所需的数据?
例如:
class OutputData
{
Dictionary<string,object> OutputDataList = new Dictionary<string,object>();
public void RegisterData(string displayName, ref object dataField)
{
OutputDataList.Add(displayName,dataField);
}
//iterate over OutputDataList and get the value of the property/field it points to
}