Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / XML

netTierGenerator

Rate me:
Please Sign up or sign in to vote.
4.81/5 (20 votes)
30 Nov 2008CPOL14 min read 67.4K   2.8K   108  
A 3-tier application framework and code generation tool - the way for rapid and effective development.
using System;
using System.Globalization;
using System.Xml;

namespace Sample.Common.History
{
    public class HistoryDeserializedInfoMember
    {
        #region members
        private string entityType;
        private string type;
        private string value;
        #endregion members

        #region properties
        public string EntityType
        {
            get { return this.entityType; }
            set { this.entityType = value; }
        }
        public string Type
        {
            get { return this.type; }
            set { this.type = value; }
        }
        public string Value
        {
            get { return this.value; }
            set { this.value = value; }
        }
        #endregion properties

        public HistoryDeserializedInfoMember(string type, string entityType, string value)
        {
            this.type = type;
            this.entityType = entityType;
            this.value = value;
        }

        #region public methods
        public object GetValue()
        {
            if (String.IsNullOrEmpty(this.Value))
            {
                return null;
            }

            Type valueType = System.Type.GetType(this.Type, false, true);
            if (valueType != null)
            {
                if (valueType.IsEnum)
                {
                    return Enum.Parse(valueType, this.Value);
                }
                else if (valueType == typeof(DateTime))
                {
                    long ticks;
                    if (Int64.TryParse(this.Value, out ticks))
                    {
                        return new DateTime(ticks, DateTimeKind.Utc).ToLocalTime();
                    }
                    else
                    {
                        return XmlConvert.ToDateTime(this.Value, XmlDateTimeSerializationMode.Local);

                        //DateTime datetime;
                        //if (DateTime.TryParse(this.Value, CultureInfo.InvariantCulture.DateTimeFormat, DateTimeStyles.AssumeLocal, out datetime))
                        //{
                        //    return datetime;
                        //}
                    }
                }
                else if (valueType.GetInterface(typeof(IConvertible).FullName) != null)
                {
                    return Convert.ChangeType(this.Value, valueType, CultureInfo.InvariantCulture);
                }
                else if (valueType == typeof(Guid) || valueType == typeof(Guid?))
                {
                    try
                    {
                        return new Guid(this.Value);
                    }
                    catch { }
                }
            }

            return null;
        }
        #endregion public methods
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions