Practical .NET

Processing All Properties on an Object

If you ever need to work with all of the properties in some object, you can use GetType and GetProperties to retrieve the object's PropertyInfo objects. After that you can do what you want. Here's an extension method that sets all those properties to their defaults, for example.

Every once in a while I want to do something with all the properties on an object. The .NET Framework gives me the GetType method which returns a Type object for an object and the Type object's GetProperties method gives me a PropertyInfo object for each property in the object. Armed with those PropertyInfo objects, I can retrieve/set property values and determine if a property holds a value type (integers, dates, etc.) or a reference type (some object), among other cool tricks.

For example, sometimes I need an object with all of its properties all set to their default values. In the bad old days, I would create a new instance of the object even if I already had a copy of the object lying around. Eventually, I created an extension method (shown in Listing 1) that loops through all the properties on any object, resetting each property to its default value.

Listing 1: Extension Method To Reset Property Values

Module PHVExtensions
    <Extension>
    Public Sub SetPropertiesToDefaultValues(Of T)(obj As T)
        Dim props = obj.GetType.GetProperties()
        Dim propType As Type
        For Each prop In props
            propType = prop.GetType
            If prop.PropertyType.Name = "String" Then
                prop.SetValue(obj, String.Empty)
            ElseIf propType.IsValueType Then
                prop.SetValue(obj, Activator.CreateInstance(propType))
            Else
                prop.SetValue(obj, Nothing)
            End If
        Next
    End Sub
End Module

You'll notice that I have a special test to handle the String type. You might prefer to omit that code, in which case String properties will be set to null/Nothing.

You would use this extension method like this:

Dim cust As Customer
cust = New Customer("A123")
cust.SetPropertiesToDefaultValues()

What will you do with all the time you save?

About the Author

Peter Vogel is a system architect and principal in PH&V Information Services. PH&V provides full-stack consulting from UX design through object modeling to database design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His blog posts on user experience design can be found at http://blog.learningtree.com/tag/ui/.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube