how get value on expando object #

≯℡__Kan透↙ 提交于 2020-04-10 08:31:13

问题


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?


回答1:


var eo = new ExpandoObject();
object value = null;

Method #1: Dynamic

dynamic eod = eo;

value = eod.Foo;

Method #2: IDictionary

var eoAsDict = ((IDictionary<String, Object>)eo);

if (eoAsDict.TryGetValue("Foo", out value))
{
    //  Stuff
}

foreach (var kvp in eoAsDict)
{
    Console.WriteLine("Property {0} equals {1}", kvp.Key, kvp.Value);
}

You won't say what typeDoc is (is it another ExpandoObject?), but if you're putting x in it, and x is an ExpandoObject, you can take x back out and it'll still be one. The fact that x is typed as a reference to IDictionary<String, Object> inside that loop is neither here nor there. It also doesn't matter if GetObject() returns object; a reference typed object can refer to anything at all. The type of the thing GetObject() returns is inherent in the actual thing being returned, not in the reference to it.

Hence:

dynamic ob = GetObject(tag + i);

or

var ob = GetObject(tag + i) as IDictionary<String, Object>;

...depending on whether you'd prefer to access the properties as ob.Foo or ob["Foo"].



来源:https://stackoverflow.com/questions/43546261/how-get-value-on-expando-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!