CreateInstanceFromString() public static method

Creates an instance of a type based on a string. Assumes that the type's
public static CreateInstanceFromString ( string typeName ) : object
typeName string
return object
コード例 #1
0
/// <summary>
/// Serializes an object instance to a JSON file.
/// </summary>
/// <param name="value">the value to serialize</param>
/// <param name="fileName">Full path to the file to write out with JSON.</param>
/// <param name="throwExceptions">Determines whether exceptions are thrown or false is returned</param>
/// <param name="formatJsonOutput">if true pretty-formats the JSON with line breaks</param>
/// <returns>true or false</returns>
        public static bool SerializeToFile(object value, string fileName, bool throwExceptions = false, bool formatJsonOutput = false)
        {
            dynamic    writer = null;
            FileStream fs     = null;

            try
            {
                Type type = value.GetType();

                dynamic json = CreateJsonNet(throwExceptions);
                if (json == null)
                {
                    return(false);
                }

                fs = new FileStream(fileName, FileMode.Create);
                var sw = new StreamWriter(fs, Encoding.UTF8);

                //JsonTextWriter writer = new JsonTextWriter(sw);
                writer = ReflectionUtils.CreateInstanceFromString("Newtonsoft.Json.JsonTextWriter", sw);

                if (formatJsonOutput)
                {
                    writer.Formatting =
                        (dynamic)ReflectionUtils.GetStaticProperty("Newtonsoft.Json.Formatting", "Indented");
                }
                else
                {
                    writer.Formatting =
                        (dynamic)ReflectionUtils.GetStaticProperty("Newtonsoft.Json.Formatting", "None");
                }

                writer.QuoteChar = '"';
                json.Serialize(writer, value);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("JsonSerializer Serialize error: " + ex.Message);
                if (throwExceptions)
                {
                    throw;
                }
                return(false);
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
                if (fs != null)
                {
                    fs.Close();
                }
            }

            return(true);
        }
コード例 #2
0
        /// <summary>
        /// Dynamically creates an instance of JSON.NET
        /// </summary>
        /// <param name="throwExceptions">If true throws exceptions otherwise returns null</param>
        /// <returns>Dynamic JsonSerializer instance</returns>
        public static dynamic CreateJsonNet(bool throwExceptions = true)
        {
            if (JsonNet != null)
            {
                return(JsonNet);
            }

            lock (SyncLock)
            {
                if (JsonNet != null)
                {
                    return(JsonNet);
                }

                // Try to create instance
                dynamic json = ReflectionUtils.CreateInstanceFromString("Newtonsoft.Json.JsonSerializer");

                if (json == null)
                {
                    try
                    {
                        json = ReflectionUtils.CreateInstanceFromString("Newtonsoft.Json.JsonSerializer");
                    }
                    catch
                    {
                        if (throwExceptions)
                        {
                            throw;
                        }
                        return(null);
                    }
                }

                if (json == null)
                {
                    return(null);
                }

                if (FormattingType == null)
                {
                    FormattingType = ReflectionUtils.GetTypeFromName("Newtonsoft.Json.Formatting");
                }
                JsonTextReaderType         = ReflectionUtils.GetTypeFromName("Newtonsoft.Json.JsonTextReader");
                JsonTextWriterType         = ReflectionUtils.GetTypeFromName("Newtonsoft.Json.JsonTextWriter");
                json.ReferenceLoopHandling =
                    (dynamic)ReflectionUtils.GetStaticProperty("Newtonsoft.Json.ReferenceLoopHandling", "Ignore");

                // Enums as strings in JSON
                dynamic enumConverter = ReflectionUtils.CreateInstanceFromString("Newtonsoft.Json.Converters.StringEnumConverter");
                json.Converters.Add(enumConverter);

                JsonNet = json;
            }

            return(JsonNet);
        }
コード例 #3
0
/// <summary>
/// Dynamically creates an instance of JSON.NET
/// </summary>
/// <param name="throwExceptions">If true throws exceptions otherwise returns null</param>
/// <returns>Dynamic JsonSerializer instance</returns>
        public static dynamic CreateJsonNet(bool throwExceptions = true)
        {
            if (JsonNet != null)
            {
                return(JsonNet);
            }

            lock (SyncLock)
            {
                if (JsonNet != null)
                {
                    return(JsonNet);
                }

                // Try to create instance
                dynamic json = ReflectionUtils.CreateInstanceFromString("Newtonsoft.Json.JsonSerializer");

                if (json == null)
                {
                    try
                    {
                        var ass = AppDomain.CurrentDomain.Load("Newtonsoft.Json");
                        json = ReflectionUtils.CreateInstanceFromString("Newtonsoft.Json.JsonSerializer");
                    }
                    catch (Exception ex)
                    {
                        if (throwExceptions)
                        {
                            throw;
                        }
                        return(null);
                    }
                }

                if (json == null)
                {
                    return(null);
                }

                json.ReferenceLoopHandling =
                    (dynamic)ReflectionUtils.GetStaticProperty("Newtonsoft.Json.ReferenceLoopHandling", "Ignore");

                // Enums as strings in JSON
                dynamic enumConverter = ReflectionUtils.CreateInstanceFromString("Newtonsoft.Json.Converters.StringEnumConverter");
                json.Converters.Add(enumConverter);

                JsonNet = json;
            }

            return(JsonNet);
        }
コード例 #4
0
/// <summary>
/// Deserializes an object from file and returns a reference.
/// </summary>
/// <param name="fileName">name of the file to serialize to</param>
/// <param name="objectType">The Type of the object. Use typeof(yourobject class)</param>
/// <param name="binarySerialization">determines whether we use Xml or Binary serialization</param>
/// <param name="throwExceptions">determines whether failure will throw rather than return null on failure</param>
/// <returns>Instance of the deserialized object or null. Must be cast to your object type</returns>
        public static object DeserializeFromFile(string fileName, Type objectType, bool throwExceptions = false)
        {
            dynamic json = CreateJsonNet(throwExceptions);

            if (json == null)
            {
                return(null);
            }

            object     result = null;
            dynamic    reader = null;
            FileStream fs     = null;

            try
            {
                fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                var sr = new StreamReader(fs, Encoding.UTF8);
                reader = ReflectionUtils.CreateInstanceFromString("Newtonsoft.Json.JsonTextReader", sr);
                result = json.Deserialize(reader, objectType);
                reader.Close();
            }
            catch (Exception ex)
            {
                Debug.WriteLine("JsonNetSerialization Deserialization Error: " + ex.Message);
                if (throwExceptions)
                {
                    throw;
                }

                return(null);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }

                if (fs != null)
                {
                    fs.Close();
                }
            }

            return(result);
        }
コード例 #5
0
        public static object Deserialize(string jsonText, Type type, bool throwExceptions = false)
        {
            dynamic json = CreateJsonNet(throwExceptions);

            if (json == null)
            {
                return(null);
            }

            object  result = null;
            dynamic reader = null;

            try
            {
                StringReader sr = new StringReader(jsonText);
                reader = ReflectionUtils.CreateInstanceFromString("Newtonsoft.Json.JsonTextReader", sr);
                result = json.Deserialize(reader, type);
                reader.Close();
            }
            catch (Exception ex)
            {
                Debug.WriteLine("JsonSerializer Deserialize error: " + ex.Message);
                if (throwExceptions)
                {
                    throw;
                }

                return(null);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }

            return(result);
        }