Esempio n. 1
0
        /// <summary>
        /// Performs a deep copy of an object if possible.
        /// </summary>
        public static object Clone(object source)
        {
            if (source == null)
            {
                return(null);
            }
            if (source.GetType().IsValueType)
            {
                return(source);
            }

            if (source.GetType().IsArray || source.GetType() == typeof(Array))
            {
                Array array = (Array)((Array)source).Clone();

                for (int ii = 0; ii < array.Length; ii++)
                {
                    array.SetValue(OpcConvert.Clone(array.GetValue(ii)), ii);
                }

                return(array);
            }

            try { return(((ICloneable)source).Clone()); }
            catch { throw new NotSupportedException("Object cannot be cloned."); }
        }
Esempio n. 2
0
 /// <summary>
 /// Returns a copy of the collection as an array.
 /// </summary>
 public virtual Array ToArray()
 {
     return((Array)OpcConvert.Clone(array_));
 }
Esempio n. 3
0
        /// <summary>
        /// Converts an object to the specified type and returns a deep copy.
        /// </summary>
        public static object ChangeType(object source, System.Type newType)
        {
            // check for null source object.
            if (source == null)
            {
                if (newType != null && newType.IsValueType)
                {
                    return(Activator.CreateInstance(newType));
                }

                return(null);
            }

            // check for null type or 'object' type.
            if (newType == null || newType == typeof(object) || newType == source.GetType())
            {
                return(Clone(source));
            }

            System.Type type = source.GetType();

            // convert between array types.
            if (type.IsArray && newType.IsArray)
            {
                ArrayList array = new ArrayList(((Array)source).Length);

                foreach (object element in (Array)source)
                {
                    array.Add(Technosoftware.DaAeHdaClient.OpcConvert.ChangeType(element, newType.GetElementType()));
                }

                return(array.ToArray(newType.GetElementType()));
            }

            // convert scalar value to an array type.
            if (!type.IsArray && newType.IsArray)
            {
                ArrayList array = new ArrayList(1);
                array.Add(OpcConvert.ChangeType(source, newType.GetElementType()));
                return(array.ToArray(newType.GetElementType()));
            }

            // convert single element array type to scalar type.
            if (type.IsArray && !newType.IsArray && ((Array)source).Length == 1)
            {
                return(System.Convert.ChangeType(((Array)source).GetValue(0), newType));
            }

            // convert array type to string.
            if (type.IsArray && newType == typeof(string))
            {
                StringBuilder buffer = new StringBuilder();

                buffer.Append("{ ");

                int count = 0;

                foreach (object element in (Array)source)
                {
                    buffer.AppendFormat("{0}", Technosoftware.DaAeHdaClient.OpcConvert.ChangeType(element, typeof(string)));

                    count++;

                    if (count < ((Array)source).Length)
                    {
                        buffer.Append(" | ");
                    }
                }

                buffer.Append(" }");

                return(buffer.ToString());
            }

            // convert to enumerated type.
            if (newType.IsEnum)
            {
                if (type == typeof(string))
                {
                    // check for an integer passed as a string.
                    if (((string)source).Length > 0 && Char.IsDigit((string)source, 0))
                    {
                        return(Enum.ToObject(newType, System.Convert.ToInt32(source)));
                    }

                    // parse a string value.
                    return(Enum.Parse(newType, (string)source));
                }
                else
                {
                    // convert numerical value to an enum.
                    return(Enum.ToObject(newType, source));
                }
            }

            // convert to boolean type.
            if (newType == typeof(bool))
            {
                // check for an integer passed as a string.
                if (typeof(string).IsInstanceOfType(source))
                {
                    string text = (string)source;

                    if (text.Length > 0 && (text[0] == '+' || text[0] == '-' || Char.IsDigit(text, 0)))
                    {
                        return(System.Convert.ToBoolean(System.Convert.ToInt32(source)));
                    }
                }

                return(System.Convert.ToBoolean(source));
            }

            // use default conversion.
            return(System.Convert.ChangeType(source, newType));
        }