Class Property

Posted on 2005-04-27
Last Modified: 2010-04-23
How Can i get the properties  of specific class .
the properties Name & Values
so i can iterate through the properties and View it

Question by:SamehRefaat
    1 Comment
    LVL 10

    Accepted Solution

    You need to use reflection, heres an example taken from the online help

    The following example creates two public properties and one protected property and displays information for the properties that match the specified binding constraints.

    Note   This example shows how to use one of the overloaded versions of GetProperties. For other examples that might be available, see the individual overload topics.

    Imports System
    Imports System.Reflection
    Imports System.Reflection.Emit
    Imports Microsoft.VisualBasic

    ' Create a class having three properties.
    Public Class MyTypeClass
        Public ReadOnly Property MyProperty1() As [String]
                Return "hello"
            End Get
        End Property
        Public ReadOnly Property MyProperty2() As [String]
                Return "hello"
            End Get
        End Property
        Protected ReadOnly Property MyProperty3() As [String]
                Return "hello"
            End Get
        End Property
    End Class 'MyTypeClass

    Public Class TypeMain
        Public Shared Sub Main()
            Dim myType As Type = GetType(MyTypeClass)
            ' Get the public properties.
            Dim myPropertyInfo As PropertyInfo() = myType.GetProperties((BindingFlags.Public Or BindingFlags.Instance))
            Console.WriteLine("The number of public properties is {0}.", myPropertyInfo.Length.ToString())
            ' Display the public properties.
            ' Get the nonpublic properties.
            Dim myPropertyInfo1 As PropertyInfo() = myType.GetProperties((BindingFlags.NonPublic Or BindingFlags.Instance))
            Console.WriteLine("The number of protected properties is {0}.", myPropertyInfo1.Length.ToString())
            ' Display the nonpublic properties.
        End Sub 'Main

        Public Shared Sub DisplayPropertyInfo(ByVal myPropertyInfo() As PropertyInfo)
            ' Display the information for all properties.
            Dim i As Integer
            For i = 0 To myPropertyInfo.Length - 1
                Dim myPropInfo As PropertyInfo = CType(myPropertyInfo(i), PropertyInfo)
                Console.WriteLine("The property name is {0}.", myPropInfo.Name.ToString())
                Console.WriteLine("The property type is {0}.", myPropInfo.PropertyType.ToString())
            Next i
        End Sub 'DisplayPropertyInfo
    End Class 'TypeMain


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now