一种拥有用于反序列化和序列化对象的静态类的方法
本文关键字:序列化 对象 反序列化 静态类 方法 用于 一种 拥有 | 更新日期: 2023-09-27 17:53:14
我想知道是否有人可以帮助我一个类的例子,可以采取任何类型,并序列化成字符串,如JSON或delim字符串,并回到它的原始类型。文本文件中的配置设置是一行带分隔符的值。
例如,我有一个对象或类是一个configurationsetting。因此,当在配置/文本文件中读取一行时,我想将其序列化为ConfigurationSetting对象。任何帮助将非常感激,并提前感谢!我在JSOn中见过这种情况,但找不到它的引用,所以忘记了它是如何处理的。
下面是一个配置文件的例子:
一行格式如下:ZZZVISA***|PUBLIX VISA MAIN.PDF|LITERAL|TRUE
所以这是不可能的,因为我没有属性名称与值,为。net知道如何处理它?
您可以使用JSON之类的东西。Net来序列化JSON和JSON之间的对象。
文档中的基本读写页面展示了如何从。net对象中读取和写入JSON。
如果您只是想分割分隔的信息,并创建它,您可能还想考虑使用String.Split
和String.Join
。这些方法允许您将分隔的文本分解为数组,并将数组重新连接回分隔的文本。
您可以使用string.format
以该格式序列化数据。要反序列化它,您可以使用RegEx将其分成组。我在一些项目中使用一个类来解析来自串行连接设备的行。我的Parser
类如下所示,它接受一个Regex字符串,并且可以返回强类型的结果。
。
var sp = new Parser("# (d+) VO(d+) SP(d+)''+");
int a, b, c;
bool ok = sp.Parse<int, int, int>("# 123 VO256 SP256+", out a, out b, out c);
.
/// <summary>
/// A strongly typed parse result containing up to 10 elements, each individually strongly typed
/// </summary>
public class Parser
{
Regex regex;
public Parser(string regularExpression)
{
this.regex = new Regex(regularExpression, RegexOptions.Compiled);
}
public bool Parse<T>(string input, out T value1)
{
bool ok = true;
var match = this.regex.Match(input);
ok = Assign(match, 1, out value1) && ok;
return ok;
}
public bool Parse<T1, T2>(string input, out T1 value1, out T2 value2)
{
bool ok = true;
var match = this.regex.Match(input);
ok = Assign(match, 1, out value1) && ok;
ok = Assign(match, 2, out value2) && ok;
return ok;
}
public bool Parse<T1, T2, T3>(string input, out T1 value1, out T2 value2, out T3 value3)
{
bool ok = true;
var match = this.regex.Match(input);
ok = Assign(match, 1, out value1) && ok;
ok = Assign(match, 2, out value2) && ok;
ok = Assign(match, 3, out value3) && ok;
return ok;
}
public bool Parse<T1, T2, T3, T4>(string input, out T1 value1, out T2 value2, out T3 value3, out T4 value4)
{
bool ok = true;
var match = this.regex.Match(input);
ok = Assign(match, 1, out value1) && ok;
ok = Assign(match, 2, out value2) && ok;
ok = Assign(match, 3, out value3) && ok;
ok = Assign(match, 4, out value4) && ok;
return ok;
}
public bool Parse<T1, T2, T3, T4, T5>(string input, out T1 value1, out T2 value2, out T3 value3, out T4 value4, out T5 value5)
{
bool ok = true;
var match = this.regex.Match(input);
ok = Assign(match, 1, out value1) && ok;
ok = Assign(match, 2, out value2) && ok;
ok = Assign(match, 3, out value3) && ok;
ok = Assign(match, 4, out value4) && ok;
ok = Assign(match, 5, out value5) && ok;
return ok;
}
public bool Parse<T1, T2, T3, T4, T5, T6>(string input, out T1 value1, out T2 value2, out T3 value3, out T4 value4, out T5 value5, out T6 value6)
{
bool ok = true;
var match = this.regex.Match(input);
ok = Assign(match, 1, out value1) && ok;
ok = Assign(match, 2, out value2) && ok;
ok = Assign(match, 3, out value3) && ok;
ok = Assign(match, 4, out value4) && ok;
ok = Assign(match, 5, out value5) && ok;
ok = Assign(match, 6, out value6) && ok;
return ok;
}
public bool Parse<T1, T2, T3, T4, T5, T6, T7>(string input, out T1 value1, out T2 value2, out T3 value3, out T4 value4, out T5 value5, out T6 value6, out T7 value7)
{
bool ok = true;
var match = this.regex.Match(input);
ok = Assign(match, 1, out value1) && ok;
ok = Assign(match, 2, out value2) && ok;
ok = Assign(match, 3, out value3) && ok;
ok = Assign(match, 4, out value4) && ok;
ok = Assign(match, 5, out value5) && ok;
ok = Assign(match, 6, out value6) && ok;
ok = Assign(match, 7, out value7) && ok;
return ok;
}
public bool Parse<T1, T2, T3, T4, T5, T6, T7, T8>(string input, out T1 value1, out T2 value2, out T3 value3, out T4 value4, out T5 value5, out T6 value6, out T7 value7, out T8 value8)
{
bool ok = true;
var match = this.regex.Match(input);
ok = Assign(match, 1, out value1) && ok;
ok = Assign(match, 2, out value2) && ok;
ok = Assign(match, 3, out value3) && ok;
ok = Assign(match, 4, out value4) && ok;
ok = Assign(match, 5, out value5) && ok;
ok = Assign(match, 6, out value6) && ok;
ok = Assign(match, 7, out value7) && ok;
ok = Assign(match, 8, out value8) && ok;
return ok;
}
public bool Parse<T1, T2, T3, T4, T5, T6, T7, T8, T9>(string input, out T1 value1, out T2 value2, out T3 value3, out T4 value4, out T5 value5, out T6 value6, out T7 value7, out T8 value8, out T9 value9)
{
bool ok = true;
var match = this.regex.Match(input);
ok = Assign(match, 1, out value1) && ok;
ok = Assign(match, 2, out value2) && ok;
ok = Assign(match, 3, out value3) && ok;
ok = Assign(match, 4, out value4) && ok;
ok = Assign(match, 5, out value5) && ok;
ok = Assign(match, 6, out value6) && ok;
ok = Assign(match, 7, out value7) && ok;
ok = Assign(match, 8, out value8) && ok;
ok = Assign(match, 9, out value9) && ok;
return ok;
}
public bool Parse<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(string input, out T1 value1, out T2 value2, out T3 value3, out T4 value4, out T5 value5, out T6 value6, out T7 value7, out T8 value8, out T9 value9, out T10 value10)
{
bool ok = true;
var match = this.regex.Match(input);
ok = Assign(match, 1, out value1) && ok;
ok = Assign(match, 2, out value2) && ok;
ok = Assign(match, 3, out value3) && ok;
ok = Assign(match, 4, out value4) && ok;
ok = Assign(match, 5, out value5) && ok;
ok = Assign(match, 6, out value6) && ok;
ok = Assign(match, 7, out value7) && ok;
ok = Assign(match, 8, out value8) && ok;
ok = Assign(match, 9, out value9) && ok;
ok = Assign(match, 10, out value10) && ok;
return ok;
}
private bool Assign<X>(Match match, int index, out X value)
{
if (match.Success)
{
Group group = match.Groups[index];
var stringValue = group.Value;
var foo = TypeDescriptor.GetConverter(typeof(X));
value = (X)(foo.ConvertFromInvariantString(stringValue));
return true;
}
else
{
value = default(X);
return false;
}
}