8.2. 将字符串反序列化为对象:deserializeStrToObj


// deserialize the string to an object
public bool deserializeStrToObj(string serializedStr, out object deserializedObj)
{
    bool deserializeOk = false;
    deserializedObj = null;

    try
    {
        byte[] restoredBytes = System.Convert.FromBase64String(serializedStr);
        MemoryStream restoredMemoryStream = new MemoryStream(restoredBytes);
        BinaryFormatter binaryFormatter = new BinaryFormatter();
        deserializedObj = binaryFormatter.Deserialize(restoredMemoryStream);

        deserializeOk = true;
    }
    catch
    {
        deserializeOk = false;
    }

    return deserializeOk;
}

    

例 8.2. deserializeStrToObj 的使用范例


    //restore login info
    object deserializedObj = null;
    if (skydrive.commLib.deserializeStrToObj(Settings.Default.loginInfoStr, out deserializedObj))
    {
        loginInfo = (skydrive.loginInfo_t)deserializedObj;