questions/49280483/serialize-and-deserialize-ajaxfileuploadeventargs-no-parameterless-constructor

CSharp · Console

/Program.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters;
using System.IO;
using System.Data;
using System.ComponentModel.DataAnnotations;
using System.Globalization;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;	

using AjaxControlToolkit;

// https://stackoverflow.com/questions/49280483/serialize-and-deserialize-ajaxfileuploadeventargs-no-parameterless-constructor
// https://github.com/DevExpress/AjaxControlToolkit/blob/master/AjaxControlToolkit/AjaxFileUpload/AjaxFileUploadEventArgs.cs

    class TestClass
    {
        public static void Test()
        {
			var args0 = new AjaxFileUploadEventArgs("file id", AjaxFileUploadState.Unknown, "status message", "file name", 1010101, "text");

			var jsonString = JsonConvert.SerializeObject(args0, Formatting.Indented);
			
			Console.WriteLine(jsonString);
			
			var deserializeFile = 
				Newtonsoft.Json.JsonConvert.DeserializeObject<AjaxFileUploadEventArgs>(jsonString);
			
			var json2 = JsonConvert.SerializeObject(deserializeFile, Formatting.Indented);
			
			Console.WriteLine("Deserialized and re-serialized {0}", args0);
			
			Console.WriteLine(json2);
			
			Assert.IsTrue(jsonString == json2);
		}
	}


	public class Program
	{
		public static void Main()
		{
			Console.WriteLine("Environment version: " + Environment.Version);
			Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);
			Console.WriteLine();

			try
			{
				TestClass.Test();
			}
			catch (Exception ex)
			{
				Console.WriteLine("Failed with unhandled exception: ");
				Console.WriteLine(ex);
				throw;
			}
		}
	}

    public class AssertionFailedException : System.Exception
    {
        public AssertionFailedException() : base() { }

        public AssertionFailedException(string s) : base(s) { }
    }

    public static class Assert
    {
        public static void IsTrue(bool value)
        {
            IsTrue(value, "failed");
        }

        public static void IsTrue(bool value, string message)
        {
            if (value == false)
                throw new AssertionFailedException(message ?? "failed");
        }
    }
;