.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

How to set all string properties to empty using Reflection?

Posted By:Jean Paul       Posted Date: September 29, 2010    Points: 10    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

Usage: Employee employee = new Employee(); // Existing class employee.Name = null; // string property SetNullToEmpty(employee);

        public void SetNullToEmpty(object obj)
            Type type = obj.GetType();

            foreach (PropertyInfo pinfo in type.GetProperties())
                if (pinfo.PropertyType == typeof(String))
                    object value = pinfo.GetValue(obj, null);
                    if (value == null)
                        pinfo.SetValue(obj, String.Empty, null);


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend