Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Declarations Generator

, 29 Jun 2010
A tool which helps in generating codes for events/properties, so that users of your classes can use events/properties of embedded classes
DeclarationsGenerator_0.0.0.3.zip
DeclarationsGenerator 0.0.0.3
EventsDeclarationsGenerator
bin
Debug
DeclarationsGenerator.exe
Release
DeclarationsGenerator.vbproj.user
Forms
Icons
AssemblyImg.bmp
ClassImg.bmp
Module & Classes
My Project
Application.myapp
Settings.settings
Temp
Tutorial
EventsPropertiesDeclarationsGenerator.gif
Tutorial AR.doc
Tutorial ENG.doc
EventsDeclarationsGenerator.suo
Imports System.Runtime.CompilerServices
Imports System.Text
Imports System.Reflection

Module mod_General

#Region "Extending the 'PropertyInfo' Type"

    <Extension()> _
       Private Function GetPropertyDeclaretion(ByVal Prop As PropertyInfo, _
                                               ByVal ControlName As String) As String
        Dim pmi = Prop.GetGetMethod
        Dim Pars As ParameterInfo() = pmi.GetParameters
        If Pars.Count > 0 Then
            '
        End If

        Dim PropName As String = ControlName & Prop.Name
        Dim PropTypeName As String = Prop.PropertyType.FullName  'Prop.PropertyType.ToString
        PropTypeName = PropTypeName.Replace("[]", "()") 'for Arrays, they get to me in [], I want them in ()
        PropTypeName = PropTypeName.Replace("+"c, "."c) 'some types get to me like this: System.Windows.Forms.Control+ControlCollection


        '-------------------
        'get Parameters declaration , 
        'I Think that Framework Classes Properties don't have Params , but UDT ones may do

        Dim paramsArr = (From p In Pars _
                          Select (If(p.IsOut, "ByRef", "Byval") & " " & _
                                  p.Name & " As " & p.ParameterType.ToString _
                                  ) _
                          ).ToArray
        Dim paramsDeclarations As String = " ( " & String.Join(" , ", paramsArr) & " )"
        '---------------


        Dim PropertyMethodeName As String = PropName & paramsDeclarations & " As " & PropTypeName


        Dim PropertyDeclaration As New StringBuilder("")
        PropertyDeclaration.Append("Public ")
        PropertyDeclaration.Append(If(Prop.CanWrite, "", "ReadOnly "))
        PropertyDeclaration.Append(If(Prop.CanRead, "", "WriteOnly "))

        PropertyDeclaration.AppendLine("Property " & PropertyMethodeName)

        Dim GetFun As String = "", SetFun As String = ""
        If Prop.CanRead Then
            GetFun = String.Format("{0}Get{3}{0}{0}Return {1}.{2}{3}{0}End Get", _
                                   vbTab, ControlName, Prop.Name, vbCrLf)
        End If

        If Prop.CanWrite Then
            SetFun = vbTab & "Set ( Value As " & PropTypeName & " )"
            SetFun &= String.Format("{3}{0}{0}{1}.{2} = Value{3}{0}End Set", _
                                    vbTab, ControlName, Prop.Name, vbCrLf)
        End If

        PropertyDeclaration.AppendFormat("{1}{0}{2}{0}End Property", _
                                        vbCrLf, GetFun, SetFun)

        Return PropertyDeclaration.ToString
    End Function

#End Region

#Region "Extending the 'EventInfo' Type"
    <Extension()> _
     Private Function GetEventDeclaretion(ByVal eve As EventInfo, _
                                          ByVal ControlName As String _
                                          ) As String

        Dim DelegateType As Type = eve.EventHandlerType
        Dim Invoke As MethodInfo = DelegateType.GetMethod("Invoke")
        Dim Pars As ParameterInfo() = Invoke.GetParameters()

        Dim EventName As String = ControlName & eve.Name

        Dim ParamsNamesArr = (From p In Pars Select p.Name).ToArray
        Dim ParamsNamesStr As String = " ( " & String.Join(" , ", ParamsNamesArr) & " )"

        Dim paramsDeclarationsArr = (From p In Pars _
                                     Select (p.Name & " As " & p.ParameterType.ToString) _
                                    ).ToArray
        Dim paramsDeclarationsStr As String = " ( " & String.Join(" , ", paramsDeclarationsArr) & " )"

        Dim EventInvokeMethodeName As String = String.Format("{0}{1}{2}", _
                                    ControlName, eve.Name, paramsDeclarationsStr)

        Dim RaiseEventStatment As String = vbTab & "RaiseEvent " & EventName & ParamsNamesStr

        Dim EventDeclaration As String = "Public Event " & EventInvokeMethodeName & vbCrLf
        EventDeclaration &= String.Format("Private Sub m_{0} Handles {1}.{2}{4}{3}{4}End Sub{4}", _
                                      EventInvokeMethodeName, ControlName, eve.Name, _
                                      RaiseEventStatment, vbCrLf)


        Return EventDeclaration
    End Function

#End Region

#Region "Extending the 'Type' Type"

    <Extension()> _
    Public Function GenerateEventsDeclarations(ByVal ContolType As System.Type, _
                                               ByVal ContolName As String, _
                                               ByVal EventsNames As IEnumerable(Of String)) As String
        Dim Output As New StringBuilder("")

        For Each evn In (From Ev In ContolType.GetEvents _
                         Where EventsNames.Contains(Ev.Name) _
                         Select Ev _
                          )

            Output.AppendLine(evn.GetEventDeclaretion(ContolName))

        Next

        Return Output.ToString

    End Function

    <Extension()> _
    Public Function GeneratePropertiesDeclarations(ByVal ContolType As System.Type, _
                                                   ByVal ContolName As String, _
                                                   ByVal PropertiesName As IEnumerable(Of String)) As String

        Dim Output As New StringBuilder("")
        For Each Prop In (From Pr In ContolType.GetProperties _
                          Where PropertiesName.Contains(Pr.Name) _
                          Select Pr _
                          )

            Output.AppendLine(Prop.GetPropertyDeclaretion(ContolName))

        Next
        Return Output.ToString

    End Function


#End Region

End Module

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

About the Author

vbnetskywalker
Software Developer
Syrian Arab Republic Syrian Arab Republic
The more I learn the more I see my ignorance.
To see all programs and tools I've made head to my Shared Folder

| Advertise | Privacy | Mobile
Web01 | 2.8.140814.1 | Last Updated 29 Jun 2010
Article Copyright 2010 by vbnetskywalker
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid