Imports TBench.TBench
Imports System.Reflection
Imports System.ComponentModel
Module CodeGenerator
Private _CodeStrIndentent As Int32
Private StrIndent As String
Public Property CodeIndent() As Int32
Get
Return _CodeStrIndentent
End Get
Set(ByVal Value As Int32)
_CodeStrIndentent = Value
StrIndent = New String(" ", Value)
End Set
End Property
Public Function GetFieldText(ByVal Parameter As ParameterInfo) As String
Dim Code As New System.Text.StringBuilder
Code.AppendFormat("{0}Private _{1} As {2}{3}", StrIndent, Parameter.Name, Parameter.ParameterType.FullName, vbCrLf)
Return Code.ToString
End Function
Public Function GetFieldText(ByVal Method As MethodInfo)
Dim Code As New System.Text.StringBuilder
Code.AppendFormat("{0}Private _{1} As New {2}{3}", StrIndent, Method.Name, "Type_" & Method.Name, vbCrLf)
Return Code.ToString
End Function
'Overload: solo per parametri
Public Function GetPropertyText(ByVal Parameter As ParameterInfo) As String
Dim Code As New System.Text.StringBuilder
Dim BaseTypes As String() = {"String", "Char", "Boolean", "Date", "Short", "Integer", "Long", "Int32", "Int64", "Int16", "UInt16", "UInt32", "UInt64", "Single", "Deciaml", "Double", "DateTime", "Byte", "SByte"}
With Code
'0 - StrIndententazione
'1 - A capo
'2 - Descrizione
'3 - Nome
'4 - Tipo
.AppendFormat( _
"{0}<CategoryAttribute(""Parametri""), Browsable(True), DefaultValueAttribute(""""), DescriptionAttribute(""{2}"")> _{1}", StrIndent, vbCrLf, GetDescription(Parameter))
.AppendFormat( _
"{0}Public Property {3}() As {4}{1}" & _
"{0} Get{1}" & _
"{0} Return _{3}{1}" & _
"{0} End Get{1}" & _
"{0} Set(ByVal Value As {4}){1}" & _
"{0} _{3} = Value{1}" & _
"{0} End Set{1}" & _
"{0}End Property{1}{1}", StrIndent, vbCrLf, GetDescription(Parameter), Parameter.Name, Parameter.ParameterType.FullName)
End With
Return Code.ToString
End Function
'Overload: solo per metodi
Public Function GetPropertyText(ByVal Method As MethodInfo) As String
Dim Code As New System.Text.StringBuilder
Dim Attr As TBenchAttribute
Dim Type As String = "Type_" & Method.Name
Attr = Method.GetCustomAttributes(GetType(TBenchAttribute), False)(0)
With Code
.AppendFormat("{0}<Category(""{2}""), Browsable(True),DefaultValue(""""), Description(""{3}"")", StrIndent, vbCrLf, Attr.Group, GetDescription(Method))
If Method.GetParameters.Length > 0 Then
.AppendFormat(", _{2}{0}TypeConverter(GetType({1}Converter))", StrIndent, Type, vbCrLf)
End If
.AppendLine("> _")
.AppendFormat("{3}Public Property {0}() As {1}{2}" & _
"{3} Get{2}" & _
"{3} Return _{0}{2}" & _
"{3} End Get{2}" & _
"{3} Set(ByVal Value As {1}){2}" & _
"{3} _{0} = Value{2}" & _
"{3} End Set{2}" & _
"{3}End Property{2}{2}", Method.Name, Type, vbCrLf, StrIndent)
End With
Return Code.ToString
End Function
'Overload: solo per metodi
Public Function GetDescription(ByVal Method As MethodInfo)
Dim SimpleSignature As New System.Text.StringBuilder
With SimpleSignature
.AppendFormat("Imposta i parametri di {0}, secondo la seguente signature: ", Method.Name)
.Append("(")
For Each PI As ParameterInfo In Method.GetParameters
If PI.Position > 0 Then
.Append(", ")
End If
.AppendFormat("{0} As {1}", PI.Name, PI.ParameterType.FullName)
Next
.Append(")")
End With
Return SimpleSignature.ToString
End Function
'Overload: solo per parametri
Public Function GetDescription(ByVal Parameter As ParameterInfo) As String
Return String.Format("Inserire un valore di tipo {0} per questo parametro.", Parameter.ParameterType.FullName)
End Function
Public Function GetExpandable(ByVal Method As MethodInfo) As String
Dim Code As New System.Text.StringBuilder
Dim Type As String = "Type_" & Method.Name
Dim Params() As ParameterInfo = Method.GetParameters
With Code
.AppendFormat("{2}Public Class {0}Converter{1}", Type, vbCrLf, StrIndent)
.AppendFormat("{1} Inherits ExpandableObjectConverter{0}", vbCrLf, StrIndent)
.AppendFormat("{1} Public Overloads Overrides Function CanConvertTo(ByVal Context As System.ComponentModel.ITypeDescriptorContext, ByVal DestinationType As System.Type) As Boolean{0}", vbCrLf, StrIndent)
.AppendFormat("{2} If (DestinationType Is GetType({0})) Then{1}", Type, vbCrLf, StrIndent)
.AppendFormat("{1} Return True{0}", vbCrLf, StrIndent)
.AppendFormat("{1} End If{0}", vbCrLf, StrIndent)
.AppendFormat("{1} Return MyBase.CanConvertFrom(Context, DestinationType){0}", vbCrLf, StrIndent)
.AppendFormat("{1} End Function{0}{0}", vbCrLf, StrIndent)
.AppendFormat("{1} Public Overloads Overrides Function CanConvertFrom(ByVal Context As System.ComponentModel.ITypeDescriptorContext, ByVal SourceType As System.Type) As Boolean{0}", vbCrLf, StrIndent)
.AppendFormat("{1} If (SourceType Is GetType(String)) Then{0}", vbCrLf, StrIndent)
.AppendFormat("{1} Return True{0}", vbCrLf, StrIndent)
.AppendFormat("{1} End If{0}", vbCrLf, StrIndent)
.AppendFormat("{1} Return MyBase.CanConvertFrom(Context, SourceType){0}", vbCrLf, StrIndent)
.AppendFormat("{1} End Function{0}{0}", vbCrLf, StrIndent)
.AppendFormat("{1} Public Overloads Overrides Function ConvertFrom(ByVal Context As System.ComponentModel.ITypeDescriptorContext, ByVal Culture As System.Globalization.CultureInfo, ByVal Value As Object) As Object{0}", vbCrLf, StrIndent)
.AppendFormat("{1} If TypeOf Value Is String Then{0}", vbCrLf, StrIndent)
.AppendFormat("{1} Dim S As String = DirectCast(Value, String){0}", vbCrLf, StrIndent)
.AppendFormat("{2} Dim Obj As New {0}{1}", Type, vbCrLf, StrIndent)
.AppendFormat("{1} Dim Vals() As String = S.Split(""|""){0}", vbCrLf, StrIndent)
For I As Int32 = 0 To Params.Length - 1
.AppendFormat("{4} Obj.{0} = CType(Vals({1}), {2}){3}", Params(I).Name, I, Params(I).ParameterType.FullName, vbCrLf, StrIndent)
Next
.AppendFormat("{1} Return Obj{0}", vbCrLf, StrIndent)
.AppendFormat("{1} End If{0}", vbCrLf, StrIndent)
.AppendFormat("{1} Return MyBase.ConvertFrom(Context, Culture, Value){0}", vbCrLf, StrIndent)
.AppendFormat("{1} End Function{0}{0}", vbCrLf, StrIndent)
.AppendFormat("{1} Public Overloads Overrides Function ConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object, ByVal destinationType As System.Type) As Object{0}", vbCrLf, StrIndent)
.AppendFormat("{2} If (DestinationType Is GetType(String)) AndAlso (TypeOf Value Is {0}) Then{1}", Type, vbCrLf, StrIndent)
.AppendFormat("{2} Dim Temp As {0} = CType(Value, {0}){1}", Type, vbCrLf, StrIndent)
.AppendFormat("{0} Return ", StrIndent)
For I As Int32 = 0 To Params.Length - 1
.AppendFormat("Temp.{0}.ToString", Params(I).Name)
If I < Params.Length - 1 Then
.Append(" & ""|"" &")
End If
Next
.AppendLine()
.AppendFormat("{1} End If{0}", vbCrLf, StrIndent)
.AppendFormat("{1} Return MyBase.ConvertTo(Context, Culture, Value, DestinationType){0}", vbCrLf, StrIndent)
.AppendFormat("{1} End Function{0}", vbCrLf, StrIndent)
.AppendFormat("{1}End Class{0}", vbCrLf, StrIndent)
End With
Return Code.ToString
End Function
'Inutilizzato nella versione finale-----------------------------------------
Private _UniversalConverter As String
Public ReadOnly Property UniversalConverter() As String
Get
Return _UniversalConverter
End Get
End Property
Public Sub LoadExpandable()
'_UniversalConverter = IO.File.ReadAllText(Application.StartupPath & "\UniversalConverter.vb")
End Sub
End Module