Search Unity

Fixing Object type System.Object[] cannot be converted to target type: imAnEnum[]

Discussion in 'Scripting' started by amzin7000, Apr 30, 2016.

  1. amzin7000

    amzin7000

    Joined:
    Nov 16, 2014
    Posts:
    48
    So i modified a json serialization script documented here: https://unitygem.wordpress.com/json-serializer/ because i needed to write a system that could save arrays of multiple different classes that inherit from a parent class.

    Since the script for some reason doesn't have support for serializing arrays that aren't classes, I easily added that feature myself, improving the script a bit everywhere along the way.

    but now when trying to deserialize my json, i get error mentioned in the title:
    Code (csharp):
    1. //o is my object.  info is a feildinfo representing the field i want to set.  rootObject is an object created with Activator.CreateInstance(rootType) that will be outputted by the deserializer
    2. info.SetValue(rootObject, o);

    A little bit of research later i had a chain of if statements that fixed the problem using linq casts:
    Code (csharp):
    1. if(attrFieldType == typeof(string)){
    2.     info.SetValue(rootObject, o.Cast<string>().ToArray());
    3. }else if(attrFieldType == typeof(float)){
    4.     info.SetValue(rootObject, o.Cast<float>().ToArray());
    5. }else if(attrFieldType == typeof(int)){
    6.     info.SetValue(rootObject, o.Cast<int>().ToArray());
    7. }else if(attrFieldType.IsEnum){
    8.     o = o.Cast<string>().Select(x => Enum.Parse(attrFieldType, x.ToString()));//convert from string first
    9.     if(attrFieldType == typeof(imAnEnum)){
    10.        info.SetValue(rootObject, o.Cast<imAnEnum>().ToArray());
    11.     }else if(/*test for another enum!*/){
    12.      
    13.     }else if(/*and another!*/){
    14.      
    15.     }else if( ect...
    16. }

    Now this would be ok, if i didn't have to store many data types, but i do. After a full day of trying to figure out how to make this work, Ive run out of ideas.
    I can't do things like o.cast<type-variable> or o.cast(type-variable).
    So what other way is there of doing this? and why can't the SetValue convert the object to the correct datatype itself in the first place?


    Also trying things like this work fine for classes but not for ints, floats, enums, or anything else, and i can't figure out why:
    Code (csharp):
    1.  
    2. object o = Array.CreateInstance(type, jsonArray.Length);
    3. object[] objs = (object[])o;//error: InvalidCastException: Cannot cast from source type to destination type.
    4. //add stuff to objs.  works very well for setting arrays of classes
    5. info.SetValue(rootObject, o);
    6.  

    Thanks in advance for any help :)
     
    Last edited: May 1, 2016
  2. amzin7000

    amzin7000

    Joined:
    Nov 16, 2014
    Posts:
    48
    bump.

    I'd really appreciate it if someone with much greater C# experience could tell me of another way to do this.
    all i'm asking is if i could replace my object.Cast<TypeThing>().ToArray() to something where i can specify the type with a Type-storing variable.

    And could someone explain to me why feildinfo.SetValue(rootObject, object); can convert some objects to the feildinfo's type but not other objects?
    How could i turn my object into something SetValue() knows how to convert?

    Thanks guys, in advance, but i would really like some input.
     
  3. amzin7000

    amzin7000

    Joined:
    Nov 16, 2014
    Posts:
    48