Home > Mobile >  VB object and c# object - access to JProperty different
VB object and c# object - access to JProperty different

Time:11-28

So if you have a situation that you can access to object with vb like this

Private Sub LoadAssetEvents(OpenSeaData)
    For Each mRoot As Assets In OpenSeaData
        For Each item As AssetEvent In mRoot.asset_events
            If item.asset_bundle IsNot Nothing Then
                For Each item_single In item.asset_bundle.assets
                    ObtainEventDetails(item, item_single)
                Next
            Else
                ObtainEventDetails(item, Nothing)
            End If
        Next
    Next
End Sub

Object item.asset_bundle contains an array of assets for some of Assets

And without any problems I can access to them and loop through them. That code translated to C#

private void LoadAssetEvents(object OpenSeaData)
{
    foreach (Assets mRoot in OpenSeaData as System.Collections.IEnumerable)
    {
        foreach (AssetEvent item in mRoot.asset_events)
        {
            if (item.asset_bundle != null)
            { 
                foreach (object item_single in item.asset_bundle.assets as System.Collections.IEnumerable)
                {
                    ObtainEtherScanDetails(item, item_single);
                }
                    
            }
            else
                ObtainEtherScanDetails(item, null);
        }
    }
}

Is throwing

'object' does not contain a definition for 'assets' and no accessible extension method 'assets' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

And I Can't figure out what is the difference Debug mode says this is JProperty

enter image description here

But further I Can navigate to those two assets

enter image description here

C# - CLASS

VB - CLASS

CodePudding user response:

That is not a correct conversion. "item_single" is not of type 'object' in the original code - the compiler will infer it, assuming that 'Option Infer On' is set (which it seems to be). The correct C# conversion also uses inferred typing, via 'var':

foreach (var item_single in item.asset_bundle.assets)

CodePudding user response:

Solved it on a way loading JProperty to dynamic JSON and then looping through it

if (item.asset_bundle != null)
{
    dynamic data = JsonConvert.DeserializeObject(item.asset_bundle.ToString());
    foreach (var item_single in data.assets as System.Collections.IEnumerable)
    {
        Asset dataA = JsonConvert.DeserializeObject<Asset>(item_single.ToString());
        while (true)
        {
            if (ObtainEtherScanDetails(item, dataA))
                break;
        }
    }
}
  • Related