本文介绍了如何在expando对象上获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我将txt文件读入一个文件夹中,然后用expando Object对对象进行水化处理.

First I read txt files into a folder, and after I hydrated objects with expando Object.

但是现在我想从该对象中获取一些价值来填充列表视图(winforms).

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++;
    }
}

那么是否有可能在expando对象上获得价值?

So is it possible to get value on expando object?

推荐答案

dynamic eod = eo;

value = eod.Foo;

这篇关于如何在expando对象上获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 21:07