Discussion:
How do I fill an object when reading a file
(too old to reply)
Tony Johansson
2017-07-10 20:53:18 UTC
Permalink
Hello!

I read a file that contains a lot of rows it could be hundreds of rows or
more
At the bottom is an example of some rows that could exist in the file.
When I have a type that contains AIC like AIC233 or AIC234 I want to load an
object with all the values.
I have defined a class called AIC with these properties
HYST
NAME
UNIT
DESCR
DISPMAX
DISPMIN
DEC
NORM_TR
H2_R_FCL
H1_R_FCL,
L1_R_FCL
L2_R_FCL
PROC_SEC
CLASS
H1_LIM2
HI_LIM1

I think I could use reflexion in some smart way but doesn't know how.
I call the first string in a row for key and the second string as the value
for this key.
So for the first row HYST is the key and the value is 7.000000e+00

My question is how do I load the property with the corresponding value when
the property have the same name as the key without using code like this

AIC aic = new AIC();
I could do in the program
if (key==HYST)
{
aic.HYST = value;
}
else if(key==NAME)
{
aic.NAME = value;
}


AIC233 AIC
:HYST 7.000000e+00
:NAME PC440151.MV0
:UNIT kPa
:DESCR Difftr Lättfasavsk
:DISPMAX 3.500000E+02
:DISPMIN 0.00000
:DEC 0
:NORM_TR 1
:H2_R_FCL 1
:H1_R_FCL 1
:L1_R_FCL 1
:L2_R_FCL 1
:PROC_SEC 3
:CLASS 42

AIC234 AIC
:HI_LIM2 1.100000e+03
:HI_LIM1 1.100000e+03
:NAME KS400460_5.ANS
:UNIT ggr
:DESCR CS Tvp2 AntalSmörj
:DISPMAX 1.000000E+03
:DISPMIN 0.000000
:DEC 0
:H2_R_FCL 1
:H1_R_FCL 1
:L1_R_FCL 1
:L2_R_FCL 1
:PROC_SEC 2
:CLASS 43

//Tony


---
This email has been checked for viruses by AVG.
http://www.avg.com
Arne Vajhøj
2017-07-11 00:58:22 UTC
Permalink
Post by Tony Johansson
I read a file that contains a lot of rows it could be hundreds of rows
or more
At the bottom is an example of some rows that could exist in the file.
When I have a type that contains AIC like AIC233 or AIC234 I want to
load an object with all the values.
I have defined a class called AIC with these properties
HYST
NAME
UNIT
DESCR
DISPMAX
DISPMIN
DEC
NORM_TR
H2_R_FCL
H1_R_FCL,
L1_R_FCL
L2_R_FCL
PROC_SEC
CLASS
H1_LIM2
HI_LIM1
I think I could use reflexion in some smart way but doesn't know how.
I call the first string in a row for key and the second string as the
value for this key.
So for the first row HYST is the key and the value is 7.000000e+00
My question is how do I load the property with the corresponding value
when the property have the same name as the key without using code like
this
AIC aic = new AIC();
I could do in the program
if (key==HYST)
{
aic.HYST = value;
}
else if(key==NAME)
{
aic.NAME = value;
}
AIC233 AIC
:HYST 7.000000e+00
:NAME PC440151.MV0
:UNIT kPa
:DESCR Difftr Lättfasavsk
:DISPMAX 3.500000E+02
:DISPMIN 0.00000
:DEC 0
:NORM_TR 1
:H2_R_FCL 1
:H1_R_FCL 1
:L1_R_FCL 1
:L2_R_FCL 1
:PROC_SEC 3
:CLASS 42
I see 4 different approaches:
* if statements (as you use)
* switch (almost the same as if)
* reflection
* delegate lookup

Below are examples of all 4.

Arne

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;

namespace E
{
public class AIC
{
public string HYST { get; set; }
public string NAME { get; set; }
public string UNIT { get; set; }
public string DESCR { get; set; }
public string DISPMAX { get; set; }
public string DISPMIN { get; set; }
public string DEC { get; set; }
public string NORM_TR { get; set; }
public string H2_R_FCL { get; set; }
public string H1_R_FCL { get; set; }
public string L1_R_FCL { get; set; }
public string L2_R_FCL { get; set; }
public string PROC_SEC { get; set; }
public string CLASS { get; set; }
public string H1_LIM2 { get; set; }
public string H1_LIM1 { get; set; }
public override string ToString()
{
return string.Format("[AIC HYST={0}, NAME={1}, UNIT={2},
DESCR={3}, DISPMAX={4}, DISPMIN={5}, DEC={6}, NORM_TR={7}, H2_R_FCL={8},
H1_R_FCL={9}, L1_R_FCL={10}, L2_R_FCL={11}, PROC_SEC={12}, CLASS={13},
H1_LIM2={14}, H1_LIM1={15}]", HYST, NAME, UNIT, DESCR, DISPMAX, DISPMIN,
DEC, NORM_TR, H2_R_FCL, H1_R_FCL, L1_R_FCL, L2_R_FCL, PROC_SEC, CLASS,
H1_LIM2, H1_LIM1);
}
}
public abstract class FileProcessor
{
public delegate void ObjectHandler(object o);
private static Regex typdef = new Regex(@"^\w+\s+(\w+)$");
private static Regex propdef = new Regex(@"^\s*:(\w+)\s+(\S+)$");
public void Process(string fnm, ObjectHandler oh)
{
using(StreamReader sr = new StreamReader(fnm))
{
object currobj = new object();
string line;
while((line = sr.ReadLine()) != null)
{
Match m1 = typdef.Match(line);
if(m1.Success)
{
currobj = CreateObject(m1.Groups[1].Value);
} else {
Match m2 = propdef.Match(line);
if(m2.Success)
{
SetProperty(currobj, m2.Groups[1].Value,
m2.Groups[2].Value);
} else {
if(line.Trim().Length == 0)
{
oh(currobj);
}
else
{
// ????
}
}
}
}
}
}
public abstract object CreateObject(string typnam);
public abstract void SetProperty(object o, string nam, string val);
}
public class IfFileProcessor : FileProcessor
{
public override object CreateObject(string typnam)
{
if(typnam == "AIC")
{
return new AIC();
}
else
{
throw new Exception("Unsupported type : " + typnam);
}
}
public override void SetProperty(object o, string nam, string val)
{
if(o is AIC)
{
AIC aic = (AIC)o;
if(nam == "HYST")
{
aic.HYST = val;
}
else if(nam == "NAME")
{
aic.NAME = val;
}
else if(nam == "UNIT")
{
aic.UNIT = val;
}
else if(nam == "DESCR")
{
aic.DESCR = val;
}
else if(nam == "DISPMAX")
{
aic.DISPMAX = val;
}
else if(nam == "DISPMIN")
{
aic.DISPMIN = val;
}
else if(nam == "DEC")
{
aic.DEC = val;
}
else if(nam == "NORM_TR")
{
aic.NORM_TR = val;
}
else if(nam == "H2_R_FCL")
{
aic.H2_R_FCL = val;
}
else if(nam == "H1_R_FCL")
{
aic.H1_R_FCL = val;
}
else if(nam == "L1_R_FCL")
{
aic.L1_R_FCL = val;
}
else if(nam == "L2_R_FCL")
{
aic.L2_R_FCL = val;
}
else if(nam == "PROC_SEC")
{
aic.PROC_SEC = val;
}
else if(nam == "CLASS")
{
aic.CLASS = val;
}
else if(nam == "H1_LIM2")
{
aic.H1_LIM2 = val;
}
else if(nam == "H1_LIM1")
{
aic.H1_LIM1 = val;
}
else
{
throw new Exception("Unknown property : " + nam);
}
}
}
}
public class SwitchFileProcessor : FileProcessor
{
public override object CreateObject(string typnam)
{
switch(typnam)
{
case "AIC":
return new AIC();
default:
throw new Exception("Unsupported type : " + typnam);
}
}
public override void SetProperty(object o, string nam, string val)
{
if(o is AIC)
{
AIC aic = (AIC)o;
switch(nam)
{
case "HYST":
aic.HYST = val;
break;
case "NAME":
aic.NAME = val;
break;
case "UNIT":
aic.UNIT = val;
break;
case "DESCR":
aic.DESCR = val;
break;
case "DISPMAX":
aic.DISPMAX = val;
break;
case "DISPMIN":
aic.DISPMIN = val;
break;
case "DEC":
aic.DEC = val;
break;
case "NORM_TR":
aic.NORM_TR = val;
break;
case "H2_R_FCL":
aic.H2_R_FCL = val;
break;
case "H1_R_FCL":
aic.H1_R_FCL = val;
break;
case "L1_R_FCL":
aic.L1_R_FCL = val;
break;
case "L2_R_FCL":
aic.L2_R_FCL = val;
break;
case "PROC_SEC":
aic.PROC_SEC = val;
break;
case "CLASS":
aic.CLASS = val;
break;
case "H1_LIM2":
aic.H1_LIM2 = val;
break;
case "H1_LIM1":
aic.H1_LIM1 = val;
break;
default:
throw new Exception("Unknown property : " + nam);
}
}
}
}
public class ReflectionFileProcessor : FileProcessor
{
private IDictionary<string,PropertyInfo> propcache = new
Dictionary<string,PropertyInfo>();
public override object CreateObject(string typnam)
{
return Activator.CreateInstance(Type.GetType("E." + typnam));
}
public override void SetProperty(object o, string nam, string val)
{
string key = o.GetType().FullName + "#" + nam;
PropertyInfo pi;
if(propcache.ContainsKey(key))
{
pi = propcache[key];
}
else
{
pi = o.GetType().GetProperty(nam);
if(pi == null) throw new Exception("Unknown property :
" +nam);
propcache.Add(key, pi);
}
pi.SetValue(o, val, null);
}
}
public class DelegateLookupFileProcessor : FileProcessor
{
private delegate object ObjectCreator();
private delegate void PropertySetter(object o,string val);
private IDictionary<string,ObjectCreator> typs = new
Dictionary<string,ObjectCreator>();
private IDictionary<string,IDictionary<string,PropertySetter>>
props = new Dictionary<string,IDictionary<string,PropertySetter>>();
public DelegateLookupFileProcessor()
{
typs.Add("AIC", ()=>new AIC());
props.Add("E.AIC", new Dictionary<string,PropertySetter>());
props["E.AIC"].Add("HYST", (o,val) => ((AIC)o).HYST = val);
props["E.AIC"].Add("NAME", (o,val) => ((AIC)o).NAME = val);
props["E.AIC"].Add("UNIT", (o,val) => ((AIC)o).UNIT = val);
props["E.AIC"].Add("DESCR", (o,val) => ((AIC)o).DESCR = val);
props["E.AIC"].Add("DISPMAX", (o,val) => ((AIC)o).DISPMAX =
val);
props["E.AIC"].Add("DISPMIN", (o,val) => ((AIC)o).DISPMIN =
val);
props["E.AIC"].Add("DEC", (o,val) => ((AIC)o).DEC = val);
props["E.AIC"].Add("NORM_TR", (o,val) => ((AIC)o).NORM_TR =
val);
props["E.AIC"].Add("H2_R_FCL", (o,val) => ((AIC)o).H2_R_FCL
= val);
props["E.AIC"].Add("H1_R_FCL", (o,val) => ((AIC)o).H1_R_FCL
= val);
props["E.AIC"].Add("L1_R_FCL", (o,val) => ((AIC)o).L1_R_FCL
= val);
props["E.AIC"].Add("L2_R_FCL", (o,val) => ((AIC)o).L2_R_FCL
= val);
props["E.AIC"].Add("PROC_SEC", (o,val) => ((AIC)o).PROC_SEC
= val);
props["E.AIC"].Add("CLASS", (o,val) => ((AIC)o).CLASS = val);
props["E.AIC"].Add("H1_LIM2", (o,val) => ((AIC)o).H1_LIM2 =
val);
props["E.AIC"].Add("H1_LIM1", (o,val) => ((AIC)o).H1_LIM1 =
val);
}
public override object CreateObject(string typnam)
{
return typs[typnam]();
}
public override void SetProperty(object o, string nam, string val)
{
props[o.GetType().FullName][nam](o, val);
}
}
public class Program
{
public static void Print(object o)
{
Console.WriteLine(o);
}
public static void Main(string[] args)
{
FileProcessor fp1 = new IfFileProcessor();
fp1.Process(@"\work\tony.dat", Print);
FileProcessor fp2 = new SwitchFileProcessor();
fp2.Process(@"\work\tony.dat", Print);
FileProcessor fp3 = new ReflectionFileProcessor();
fp3.Process(@"\work\tony.dat", Print);
FileProcessor fp4 = new DelegateLookupFileProcessor();
fp4.Process(@"\work\tony.dat", Print);
Console.ReadKey();
}
}
}

Continue reading on narkive:
Loading...