Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

skydrive System.Dynamic.DynamicObject

I'm trying to get a listing of all the folders for a signed in user on SkyDrive.

LiveOperationResult operationResult = await client.GetAsync("me/skydrive/files");
dynamic result = operationResult.Result;

I'd like to be able to do something like this:

Dictionary<string, object> folderData = (Dictionary<string, object>)result;
List<object> folders = (List<object>)folderData["data"];

foreach (object item in folders)
{
    Dictionary<string, object> folder = (Dictionary<string, object>)item;
    if (folder["name"].ToString() == "Folder Name")
    {
        showToastMessage(folder["id"].ToString());
        return;
    }
}

However, on this line:

Dictionary<string, object> folderData = (Dictionary<string, object>)result;

I'm getting an error:

Cannot convert type 'System.Dynamic.DynamicObject' to 'System.Collections.Generic.Dictionary'

Does anyone have any idea how I can do get around this issue?

like image 848
webdad3 Avatar asked Dec 14 '25 17:12

webdad3


2 Answers

The problem is that LiveOperationResult.Result isn't necessarily guaranteed to be a Dictionary<string, object>. It is however defined as an IDictionary<string, object>.

Mind you, you don't appear to even need to cast the Result property to a dictionary of any sort; you should be able to use the dynamic variable to directly access the list you want to iterate.

List<object> folders = (List<object>)result.data;
like image 106
Adam Maras Avatar answered Dec 17 '25 05:12

Adam Maras


I think that you receive this because you have declared result using the following code

dynamic result = operationResult.Result;

this will declare result as a new System.Dynamic.DynamicObject so that, when we say

Dictionary<string, object> folderData = (Dictionary<string, object>)result;

You are trying to convert result of type System.Dynamic.DynamicObject to System.Collections.Generic.Dictionary which is not possible and that's why you receive the error.

Thanks,
I hope you find this helpful :)

like image 28
Picrofo Software Avatar answered Dec 17 '25 07:12

Picrofo Software



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!