c#加载/保存对象或对象数组

时间:2015-11-19 15:18:55

标签: c# arrays object save load

我认为这是我使用的代码,但是无法加载它, 基本上imagebook是一个单独的对象,它存储包含图像和整数的“imagedata对象数组”,用于我的加载保存。 但是在加载子中的一行上有“类型”错误的问题。

如果有办法保存整个imagedata数组并删除也可以的imagebook。

namespace FastCrack
{
    [Serializable]
    class ImageBook1
    {
        public ImageData1[] imgdat;
        public ImageBook1(ImageData1[] dat) {
            imgdat = new ImageData1[1000];
            imgdat = dat;
        }
    }
}




namespace FastCrack
{
    public partial class Form1 : Form
    {
        private void saveCrack()
        {      
        try
        {
              book1 = new ImageBook1(imagedataSheets);
            // Create a FileStream that will write data to file.
            FileStream writerFileStream = new FileStream(Filename1, FileMode.Create, FileAccess.Write); // Save our dictionary of friends to file
            this.formatter.Serialize(writerFileStream, this.book1); 
            writerFileStream.Close();
        }
        catch (Exception) {
            Console.WriteLine("Unable to save our friends' information");
        } // end try-catch
        }


        private void loadCrack()
        {

            if (File.Exists(Filename1))
            {

                try
                {
                    // Create a FileStream will gain read access to the 
                    // data file.
                    FileStream readerFileStream = new FileStream(Filename1,
                        FileMode.Open, FileAccess.Read);
                    // Reconstruct information of our friends from file.




  //this.friendsDictionary = (Dictionary<String, Friend> 
 //<--this is line from example

this.book1 = (Dictionary<ImageBook1, book1>) 
 // (Dictionary<String, Friend>)
    <--  this is the line giving me type error




                        this.formatter.Deserialize(readerFileStream);
                    // Close the readerFileStream when we are done
                    readerFileStream.Close();

                }
                catch (Exception)
                {
                    Console.WriteLine("There seems to be a file that contains " +
                        "friends information but somehow there is a problem " +
                        "with reading it.");
                } // end try-catch

            } // end if
        }
    }
}

1 个答案:

答案 0 :(得分:1)

public class GCMNotificationIntentService extends IntentService { // Sets an ID for the notification, so it can be updated public static final int notifyID = 9001; JSONArray array_add = null; NotificationCompat.Builder builder; // object helper to store the notification configuration private SharedPreferenceNotification shared_pref; String type, content, nr, title, link, subtype; public GCMNotificationIntentService() { super("GcmIntentService"); } @Override protected void onHandleIntent(Intent intent) { Bundle extras = intent.getExtras(); GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); String messageType = gcm.getMessageType(intent); if (!extras.isEmpty()) { if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR .equals(messageType)) { sendNotification("Send error: " + extras.toString()); } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED .equals(messageType)) { sendNotification("Deleted messages on server: " + extras.toString()); } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE .equals(messageType)) { sendNotification("[" + extras.get(ApplicationConstants.MSG_KEY) + "]"); } } GcmBroadcastReceiver.completeWakefulIntent(intent); } private void sendNotification(String msg) { try { array_add = new JSONArray(msg); try { for (int i = 0; i < array_add.length(); i++) { JSONObject c = array_add.getJSONObject(i); type = c.getString(Utils.NOT_TYPE); content = "[" + c.getString(Utils.NOT_CONTENT) + "]"; JSONArray content_a = new JSONArray(content); for (int j = 0; j < content_a.length(); j++) { JSONObject b = content_a.getJSONObject(j); title = b.getString(Utils.NOT_TITLE); nr = b.getString(Utils.NOT_NR); link = b.getString(Utils.NOT_LINK); subtype = b.getString(Utils.NOT_SUBTYPE); } } } catch (Exception e) { } } catch (Exception e) { } Tracker t = GoogleAnalytics.getInstance(this) .newTracker("UA-2983962-14"); // Build and send an Event. t.send(new HitBuilders.EventBuilder() .setCategory("Notifications") .setAction(type) .setLabel("Dergimi") .build()); // initialize shared preference manager shared_pref = new SharedPreferenceNotification(this); if (shared_pref.getValue(type)) { Intent resultIntent; PendingIntent resultPendingIntent = null; if (type.equalsIgnoreCase("news"){ resultIntent =new Intent(this, ViewInWeb.class); Bundle basket = new Bundle(); basket.putString(TimeUtils.TAG_LINK,link ); basket.putString(TimeUtils.TAG_TITLE,title ); basket.putInt("TYPE", 1); resultIntent.putExtras(basket); t.send(new HitBuilders.EventBuilder() .setCategory("Notifications") .setAction(type) .setLabel(title) .build()); resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_ONE_SHOT); displayNot(resultPendingIntent); Log.e("Erdhi ketu", "Erdhi ketuttttttt"); } } } private void displayNot(PendingIntent resultPendingIntent){ MyApplication mApplication = (MyApplication) getApplicationContext(); mApplication.getTracker(MyApplication.TrackerName.APP_TRACKER); NotificationCompat.Builder mNotifyBuilder; NotificationManager mNotificationManager; mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotifyBuilder = new NotificationCompat.Builder(this) .setContentTitle("AppName").setContentText(title) .setSmallIcon(R.drawable.ic_launcher); // Set pending intent mNotifyBuilder.setContentIntent(resultPendingIntent); // Set Vibrate, Sound and Light int defaults = 0; defaults = defaults | Notification.DEFAULT_LIGHTS; defaults = defaults | Notification.DEFAULT_VIBRATE; defaults = defaults | Notification.DEFAULT_SOUND; mNotifyBuilder.setDefaults(defaults); // Set the content for Notification mNotifyBuilder.setContentText(title); // Set autocancel mNotifyBuilder.setAutoCancel(true); // Post a notification mNotificationManager.notify(Integer.valueOf(nr), mNotifyBuilder.build()); } 变量似乎是book1(不确定;您没有与我们分享声明),因此您无法为其分配字典。

您序列化的类型也应该是反序列化的类型,因此可能想要将其强制转换为ImageBook1

ImageBook1

如果没有,您将需要编辑您的答案并指出想要的字典类型。