Transforming URL pass parameters into custom entity methods

Source: Internet
Author: User

Define the entity you want to get first:

  public class Inputclass    {public        long Id {get; set;}        public int Status {get; set;}        Public DateTime udpatetime {get; set;}    }

converting URL parameters to corresponding dictionary types

       Private dictionary<string, string> getinputnamevalues (string value)        {            var array = value. Split (new char[] {' & '}, stringsplitoptions.removeemptyentries);            char[] split = new char[] {' = '};            dictionary<string, string> result = new dictionary<string, string> (array. Length);            foreach (var item in array)            {                var Temparr = Item. Split (split, stringsplitoptions.removeemptyentries);                if (temparr.length = = 2)                {                    result. ADD (Temparr[0], temparr[1]);                }            }            return result;        }

To convert an entity class to a dictionary method:

        Private dictionary<string, system.reflection.propertyinfo> GetProperty (type type)        {            var pro = type. GetProperties ();            var result = new dictionary<string, propertyinfo> (pro. Length);            var jsonpropertytype = typeof (Jsonpropertyattribute);            foreach (var item in Pro)            {                var name = Item. Name;                var jsonplist = (jsonpropertyattribute[]) (item. GetCustomAttributes (Jsonpropertytype, false));                if (Jsonplist.length > 0)                {                    var jsonp = Jsonplist.first ();                    Jsonpropertyattribute att = new Jsonpropertyattribute ();                    Name = Jsonp. PropertyName;                }                Result. ADD (name, item);            }            return result;        }

Main method:

            var type = typeof (Inputclass);            String inputstring = "id=1111&status=1&udpatetime=2015-5-9";            var model = new Inputclass ();            var namevalues = getinputnamevalues (inputstring);            var PropertyKeys = GetProperty (type);            foreach (var item in PropertyKeys)            {                string value;                if (Namevalues.trygetvalue (item. Key, out value))                {                    item. Value.setvalue (model, Convert.changetype (value, item. Value.propertytype));                }            }            return model;

The returned model is the encapsulated entity.

Transforming URL pass parameters into custom entity methods

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.