gpt4 book ai didi

c# - 制作静态类当前状态的快照对象

转载 作者:太空宇宙 更新时间:2023-11-03 20:54:50 26 4
gpt4 key购买 nike

我的系统中有一个静态类,用于跟踪测量频率、当前读取的样本数量、哪些传感器处于打开状态以及哪些传感器处于关闭状态,以及所有这些不错的细节。

现在我做一个测量,想在报告中创建一个报告我想保存静态类中存储的所有信息。像这样:

public static class Details{
public static int samplesRead { get; set;}
public static int frequency { get; set;}
public static List<devices> devices { get; set;}
}

public class Patient{...} // name, surname , blabla

public class ResultsSet {
public DateTime date;
public Patient patient;
public *DetailsObject* details;
}

public void main {
patient p = new patient(...);
... make the measurements ...

var results = new ResultSet();
results.patient = p;
results.DateTime = DateTime.Now();
results.details = **here the magic ** Details.ToObject();

results.Serialize(myFilePath);
}

如何才能完成对单个已定义对象的转换?

最佳答案

it is the capability of making an snapshot of the static class in an object. [...] Just make an object.

所以您可以做的是创建一个与您的静态类具有相同属性的 DTO:

public class DetailsSnapshot 
{
public int samplesRead { get; set; }
public int frequency { get; set; }
public List<device> devices { get; set; }
}

您不能在任何给定时间映射并返回这样的对象:

public static class Details{
public static int samplesRead { get; set;}
public static int frequency { get; set; }
public static List<device> devices { get; set; }

public static DetailsSnapshot MakeSnapShot()
{
return new DetailsSnapshot
{
samplesRead = samplesRead,
frequency = frequency,
devices = devices.ToList()
};
}
}

然后您可以在结果中包含这样一个快照对象:

public class ResultsSet
{
public DateTime date;
public Patient patient;
public DetailsSnapshot detailsSnapShot;
}

并按以下方式制作快照(这里是魔法):

results.detailsSnapShot = Details.MakeSnapShot();

编辑:

还有一种方法是使用反射。使用这种方法,您将扫描 Details 类以查找属性并提取值。您可以返回一个 Dictionary,它基本上将属性的名称映射到值:

public static Dictionary<string, object> MakeSnapShotReflection()
{
PropertyInfo [] allPorperties = typeof(Details).GetProperties(BindingFlags.Public | BindingFlags.Static);

Dictionary<string, object> valuemapping = new Dictionary<string, object>();

for (int i = 0; i < allPorperties.Length; i++)
{
valuemapping.Add(allPorperties[i].Name, allPorperties[i].GetValue(null));
}

return valuemapping;
}

这种方式允许您使用更多属性扩展 Details 类,而不必担心扩展任何其他内容。

或者简短的版本:

public static Dictionary<string, object> MakeSnapShotReflection()
{
PropertyInfo[] allPorperties = typeof(Details).GetProperties(BindingFlags.Public | BindingFlags.Static);
return allPorperties.ToDictionary(key => key.Name, value => value.GetValue(null));
}

通过这种方法,您仍然可以使用智能感知来访问正确的值:

测试数据:

public static class Details
{
public static int samplesRead { get; set;} = 100;
public static int frequency { get; set; } = 2700;
public static List<device> devices { get; set; } = new List<device>()
{
new device { Name = "sensor1" },
new device { Name = "sensor 2" }
};
}

public class device
{
public string Name { get; set; }
}

访问值的测试代码:

void Main()
{
Dictionary<string, object> details = Details.MakeSnapShotReflection();

Console.WriteLine(details[nameof(Details.frequency)]);
Console.WriteLine(details[nameof(Details.samplesRead)]);

foreach (var element in details[nameof(Details.devices)] as IEnumerable<device>)
{
Console.WriteLine(element.Name);
}
}

输出:

2700
100
sensor1
sensor 2

关于c# - 制作静态类当前状态的快照对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51555841/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com