vendredi 4 mai 2018

Reflection casting

I have an object with some fields in it and I'm using reflection to iterate the fields of that object. This object is get Serialized and Deserialized. I'm writing the Deserialization routine now. I want to iterate through the fields, grab the value from the deserializations SerializationInfo. Before I started to implement Reflection, I did everything manually:

public SKA_MAP(SerializationInfo info, StreamingContext ctxt)
{
    //Get the values from info and assign them to the appropriate properties
    DISP_SEGS = (int)info.GetValue("DISP_SEGS", typeof(int)); 
    //other fields followed
}

and now:

public SKA_MAP(SerializationInfo info, StreamingContext ctxt)
{
    //Get the values from info and assign them to the appropriate properties
    //DISP_SEGS = (int)info.GetValue("DISP_SEGS", typeof(int));

    foreach (FieldInfo FI in this.GetType().GetFields())
    {
        FI.SetValue(this, info.GetValue(FI.Name, FI.GetType()));
    }
}

I get

'Invalid cast from 'System.Int32' to 'System.Reflection.RtFieldInfo'.'

OK, yes, but I put different casts in there "not shown" and nothing.





Aucun commentaire:

Enregistrer un commentaire