First I read txt files into a folder, and after I hydrated objects with expando Object.
But now I would like to get some value from this objects to fill a listview (winforms).
private void Form1_Load(object sender, EventArgs e)
{
string pattern = "FAC*.txt";
var directory = new DirectoryInfo(@"C:\\TestLoadFiles");
var myFile = (from f in directory.GetFiles(pattern)
orderby f.LastWriteTime descending
select f).First();
hydrate_object_from_metadata("FAC",listBox3);
hydrate_object_from_metadata("BL", listBox4);
this.listBox3.MouseDoubleClick += new MouseEventHandler(listBox3_MouseDoubleClick);
this.listBox1.MouseClick += new MouseEventHandler(listBox1_MouseClick);
}
void hydrate_object_from_metadata(string tag, ListBox listBox)
{
SearchAndPopulateTiers(@"C:\TestLoadFiles", tag + "*.txt", tag);
int count = typeDoc.Count(D => D.Key.StartsWith(tag));
for (int i = 0; i < count; i++)
{
object ob = GetObject(tag + i);
///HERE I WOULD LIKE GET DATA VALUE FROM ob object
}
}
Object GetObject(string foo)
{
if (typeDoc.ContainsKey(foo))
return typeDoc[foo];
return null;
}
void SearchAndPopulateTiers(string path, string extention, string tag)
{
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] files = di.GetFiles(extention);
int i = 0;
foreach (FileInfo file in files)
{
var x = new ExpandoObject() as IDictionary<string, Object>;
string[] strArray;
string s = "";
while ((s = sr.ReadLine()) != null)
{
strArray = s.Split('=');
x.Add(strArray[0],strArray[1]);
}
typeDoc.Add(tag+i,x);
i++;
}
}
So is it possible to get value on expando object?
The ExpandoObject class enables you to add and delete members of its instances at run time and also to set and get values of these members. This class supports dynamic binding, which enables you to use standard syntax like sampleObject. sampleMember instead of more complex syntax like sampleObject.
For ExpandoObject, you can simply check whether the property is defined as a key in the underlying dictionary. For other implementations, it might be challenging and sometimes the only way is to work with exceptions.
dynamic eod = eo;
value = eod.Foo;
I was doing this generically/dynamically so didn't have the option of including the actual field name in the code and ended up doing it this way:
eo.Where(v => v.Key == keyNameVariable).Select(x => x.Value).FirstOrDefault();
Probably a better way to do this, but it works.
The "LINQ" way:
I have written a quick example to demonstrate you the idea. Consider the following code (the JSON part is just for quickly generating some data which will then be converted to an expando object, the interesting part starts with step 3.):
// uses Newtonsoft.Json
void Main()
{
// 1. prepare data as JSON string (just to have some data for demo)
var rowData = "{\"rows\": ["
+ "{"
+ "\"index\":0,"
+ "\"file\":\"calc.exe\""
+ "},"
+ "{"
+ "\"index\": 1,"
+ "\"file\":\"cmd.exe\""
+ "}"
+ "]}";
// display JSON string
rowData.Dump();
// 2. convert JSON string to expando object
var expConverter = new ExpandoObjectConverter();
dynamic exp = JsonConvert.DeserializeObject<ExpandoObject>(rowData, expConverter);
// 3. convert to queryable list (so you can query the rows)
var rows = (List<dynamic>)exp.rows;
// 4. query it by selecting row with index==0
var q = rows.Where(w => w.index == 0);
// 5. getting the file property
var fileName = q.Select(s => (string)s.file).First();
// display result
fileName.Dump();
}
Now after providing the expando object (variable exp
, which is completely dynamic) the next step (3.) converts it to something we can query (here a list of dynamic objects).
Step 4. is then using LINQ to query it by index, and in Step 5. we want to get back the file information.
Note:
file
and index
are dynamically created properties, the are coming from the JSON data. Of course you can get the ExpandoObject
from different sources.ExpandoObject
, notice that any typo in the property names will not result in a compile error, but will cause a runtime error (and only, if the related code part is being executed).You can run the example in LinqPad or in Visual Studio, but don't forget to add Newtonsoft.Json (as a NUGET package).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With