. NET determines whether all properties of an object are empty

Source: Internet
Author: User

Title, this instance takes into account the many properties of the object (regardless of whether this object design is reasonable), when you want to verify that many objects are empty, if else is not considered, and is expected to be implemented with the simplest code, as follows:

Reference: https://codereview.stackexchange.com/questions/70341/check-if-any-of-class-properties-is-not-null-empty-was-assigned

Code:

Objectproperties op =Newobjectproperties (); Op.name="Test"; Op.age= the; StringBuilder SB=NewStringBuilder (); Propertyinfo[] Properties=op. GetType ().            GetProperties (); foreach(PropertyInfo Piinchproperties) {                if(pi.) GetValue (OP,NULL) !=""&& Pi. GetValue (OP,NULL) !=NULL) {sb. Append (string. Format ("Name: {0} | Value: {1}", Pi. Name, Pi. GetValue (OP,NULL)                        )                ); }} Console.WriteLine (sb.)            ToString ());                    Console.ReadLine (); }

Code logic is simple, console application implementation, key code PI. GetValue (OP, null), has run through, to achieve expectations, such as errors, please correct me.

. NET determines whether all properties of an object are empty

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.