Librería de Snippets para VB.NET !! (Compartan aquí sus snippets)

<< < (119/119)

Eleкtro:
Comparto una forma que he ideado para automatizar la traducción, al idioma actual de la aplicación, los valores booleanos en un propertygrid (por ejemplo), mediante el uso clases de atributos.






El modo de empleo es muy sencillo:

Código
public class TestClass
 
<LocalizableBoolean>
<TypeConverter(GetType(LocalizableBooleanConverter))>
Public Property FeatureEnabled As Boolean = True
 
end class

Código
Me.PropertyGrid1.SelectedObject = new TestClass()

También se puede utilizar de esta forma alternativa para una representación arbitraria en los idiomas que se especifiquen mediante un string separado por comas (en este ejemplo, el español y el francés):

Código
<LocalizableBoolean("es, fr", "ssssí!!, Oui!", "nope!, Non!")>
<TypeConverter(GetType(LocalizableBooleanConverter))>
Public Property FeatureEnabled As Boolean = True




El código:

LocalizedBoolean.vb
Código
''' <summary>
''' Represents localized strings for <see langword="True"/> and <see langword="False"/> <see cref="Boolean"/> values.
''' </summary>
<DebuggerStepThrough>
Public NotInheritable Class LocalizedBoolean
 
   ''' <summary>
   ''' The <see cref="CultureInfo"/> that represents the region for
   ''' the localized strings in <see cref="LocalizedBoolean.True"/>
   ''' and <see cref="LocalizedBoolean.False"/> properties.
   ''' </summary>
   Public ReadOnly Property Culture As CultureInfo
 
   ''' <summary>
   ''' The localized string representation for <see langword="True"/> <see cref="Boolean"/> value.
   ''' </summary>
   Public ReadOnly Property [True] As String
 
   ''' <summary>
   ''' The localized string representation for <see langword="False"/> <see cref="Boolean"/> value.
   ''' </summary>
   Public ReadOnly Property [False] As String
 
   ''' <summary>
   ''' Initializes a new instance of the <see cref="LocalizedBoolean"/> class.
   ''' </summary>
   '''
   ''' <param name="culture">
   ''' The <see cref="CultureInfo"/> that represents the region for the localized strings.
   ''' </param>
   '''
   ''' <param name="trueString">
   ''' The localized string representation for <see langword="True"/> <see cref="Boolean"/> value.
   ''' </param>
   '''
   ''' <param name="falseString">
   ''' The localized string representation for <see langword="False"/> <see cref="Boolean"/> value.
   ''' </param>
   Public Sub New(culture As CultureInfo, trueString As String, falseString As String)
       If culture Is Nothing Then
           Throw New ArgumentNullException(paramName:=NameOf(culture))
       End If
       If String.IsNullOrWhiteSpace(trueString) Then
           Throw New ArgumentNullException(paramName:=NameOf(trueString))
       End If
       If String.IsNullOrWhiteSpace(falseString) Then
           Throw New ArgumentNullException(paramName:=NameOf(falseString))
       End If
 
       Me.Culture = culture
       Me.True = trueString
       Me.False = falseString
   End Sub
 
   ''' <summary>
   ''' Prevents a default instance of the <see cref="LocalizedBoolean"/> class from being created.
   ''' </summary>
   Private Sub New()
   End Sub
 
End Class

LocalizableBooleanAttribute.vb
Código:

''' <summary>
''' Specifies that a <see cref="Boolean"/> property can display localized string representations
''' for <see langword="True"/> and <see langword="False"/> values.
''' </summary>
<AttributeUsage(AttributeTargets.Property, AllowMultiple:=False, Inherited:=True)>
<DebuggerStepThrough>
Public NotInheritable Class LocalizableBooleanAttribute : Inherits Attribute

    ''' <summary>
    ''' Gets the localized boolean representations.
    ''' <para></para>
    ''' The dictionary Key is the ISO 639-1 two-letter code for the language.
    ''' </summary>
    Public ReadOnly Property Localizations As Dictionary(Of String, LocalizedBoolean)

    ''' <summary>
    ''' Initializes a new instance of the <see cref="LocalizedBoolean"/> class.
    ''' </summary>
    Public Sub New()
        Me.Localizations = New Dictionary(Of String, LocalizedBoolean)(StringComparison.OrdinalIgnoreCase) From {
            {"af", New LocalizedBoolean(CultureInfo.GetCultureInfo("af"), "Ja", "Nee")}, ' Afrikaans
            {"am", New LocalizedBoolean(CultureInfo.GetCultureInfo("am"), "እወዳለሁ", "አይደለሁ")}, ' Amharic
            {"ar", New LocalizedBoolean(CultureInfo.GetCultureInfo("ar"), "نعم", "لا")}, ' Arabic
            {"az", New LocalizedBoolean(CultureInfo.GetCultureInfo("az"), "Bəli", "Xeyr")}, ' Azerbaijani
            {"be", New LocalizedBoolean(CultureInfo.GetCultureInfo("be"), "Так", "Не")}, ' Belarusian
            {"bg", New LocalizedBoolean(CultureInfo.GetCultureInfo("bg"), "Да", "Не")}, ' Bulgarian
            {"bn", New LocalizedBoolean(CultureInfo.GetCultureInfo("bn"), "হ্যাঁ", "না")}, ' Bengali
            {"ca", New LocalizedBoolean(CultureInfo.GetCultureInfo("ca"), "Sí", "No")}, ' Catalan
            {"cs", New LocalizedBoolean(CultureInfo.GetCultureInfo("cs"), "Ano", "Ne")}, ' Czech
            {"cy", New LocalizedBoolean(CultureInfo.GetCultureInfo("cy"), "Ie", "Na")}, ' Welsh
            {"da", New LocalizedBoolean(CultureInfo.GetCultureInfo("da"), "Ja", "Nej")}, ' Danish
            {"de", New LocalizedBoolean(CultureInfo.GetCultureInfo("de"), "Ja", "Nein")}, ' German
            {"el", New LocalizedBoolean(CultureInfo.GetCultureInfo("el"), "Ναι", "Όχι")}, ' Greek
            {"en", New LocalizedBoolean(CultureInfo.GetCultureInfo("en"), "Yes", "No")}, ' English
            {"es", New LocalizedBoolean(CultureInfo.GetCultureInfo("es"), "Sí", "No")}, ' Spanish
            {"et", New LocalizedBoolean(CultureInfo.GetCultureInfo("et"), "Jah", "Ei")}, ' Estonian
            {"eu", New LocalizedBoolean(CultureInfo.GetCultureInfo("eu"), "Bai", "Ez")}, ' Basque
            {"fa", New LocalizedBoolean(CultureInfo.GetCultureInfo("fa"), "بله", "خیر")}, ' Persian
            {"fi", New LocalizedBoolean(CultureInfo.GetCultureInfo("fi"), "Kyllä", "Ei")}, ' Finnish
            {"fr", New LocalizedBoolean(CultureInfo.GetCultureInfo("fr"), "Oui", "Non")}, ' French
            {"ga", New LocalizedBoolean(CultureInfo.GetCultureInfo("ga"), "Tá", "Níl")}, ' Irish
            {"gd", New LocalizedBoolean(CultureInfo.GetCultureInfo("gd"), "Tha", "Chan eil")}, ' Scottish Gaelic
            {"gl", New LocalizedBoolean(CultureInfo.GetCultureInfo("gl"), "Si", "Non")}, ' Galician
            {"gu", New LocalizedBoolean(CultureInfo.GetCultureInfo("gu"), "હા", "ના")}, ' Gujarati
            {"hi", New LocalizedBoolean(CultureInfo.GetCultureInfo("hi"), "हाँ", "नहीं")}, ' Hindi
            {"hr", New LocalizedBoolean(CultureInfo.GetCultureInfo("hr"), "Da", "Ne")}, ' Croatian
            {"ht", New LocalizedBoolean(CultureInfo.GetCultureInfo("ht"), "Wi", "Pa")}, ' Haitian Creole
            {"hu", New LocalizedBoolean(CultureInfo.GetCultureInfo("hu"), "Igen", "Nem")}, ' Hungarian
            {"id", New LocalizedBoolean(CultureInfo.GetCultureInfo("id"), "Ya", "Tidak")}, ' Indonesian
            {"ig", New LocalizedBoolean(CultureInfo.GetCultureInfo("ig"), "Ee", "Mba")}, ' Igbo
            {"is", New LocalizedBoolean(CultureInfo.GetCultureInfo("is"), "Já", "Nei")}, ' Icelandic
            {"it", New LocalizedBoolean(CultureInfo.GetCultureInfo("it"), "Sì", "No")}, ' Italian
            {"ja", New LocalizedBoolean(CultureInfo.GetCultureInfo("ja"), "はい", "いいえ")}, ' Japanese
            {"jv", New LocalizedBoolean(CultureInfo.GetCultureInfo("jv"), "Iya", "Ora")}, ' Javanese
            {"kk", New LocalizedBoolean(CultureInfo.GetCultureInfo("kk"), "Иә", "Жоқ")}, ' Kazakh
            {"km", New LocalizedBoolean(CultureInfo.GetCultureInfo("km"), "បាទ/ចាស", "ទេ")}, ' Khmer
            {"kn", New LocalizedBoolean(CultureInfo.GetCultureInfo("kn"), "ಹೌದು", "ಇಲ್ಲ")}, ' Kannada
            {"ko", New LocalizedBoolean(CultureInfo.GetCultureInfo("ko"), "예", "아니오")}, ' Korean
            {"ku", New LocalizedBoolean(CultureInfo.GetCultureInfo("ku"), "Belê", "Na")}, ' Kurdish (Kurmanji)
            {"ky", New LocalizedBoolean(CultureInfo.GetCultureInfo("ky"), "Ооба", "Жок")}, ' Kyrgyz
            {"la", New LocalizedBoolean(CultureInfo.GetCultureInfo("la"), "Ita", "Non")}, ' Latin
            {"lg", New LocalizedBoolean(CultureInfo.GetCultureInfo("lg"), "Yee", "Nedda")}, ' Luganda
            {"lt", New LocalizedBoolean(CultureInfo.GetCultureInfo("lt"), "Taip", "Ne")}, ' Lithuanian
            {"lv", New LocalizedBoolean(CultureInfo.GetCultureInfo("lv"), "Jā", "Nē")}, ' Latvian
            {"mg", New LocalizedBoolean(CultureInfo.GetCultureInfo("mg"), "Eny", "Tsia")}, ' Malagasy
            {"mi", New LocalizedBoolean(CultureInfo.GetCultureInfo("mi"), "Āe", "Kāo")}, ' Maori
            {"mk", New LocalizedBoolean(CultureInfo.GetCultureInfo("mk"), "Да", "Не")}, ' Macedonian
            {"ml", New LocalizedBoolean(CultureInfo.GetCultureInfo("ml"), "അതെ", "ഇല്ല")}, ' Malayalam
            {"mn", New LocalizedBoolean(CultureInfo.GetCultureInfo("mn"), "Тийм", "Үгүй")}, ' Mongolian
            {"mr", New LocalizedBoolean(CultureInfo.GetCultureInfo("mr"), "होय", "नाही")}, ' Marathi
            {"ms", New LocalizedBoolean(CultureInfo.GetCultureInfo("ms"), "Ya", "Tidak")}, ' Malay
            {"mt", New LocalizedBoolean(CultureInfo.GetCultureInfo("mt"), "Iva", "Le")}, ' Maltese
            {"my", New LocalizedBoolean(CultureInfo.GetCultureInfo("my"), "ဟုတ်ကဲ့", "မဟုတ်ဘူး")}, ' Burmese
            {"ne", New LocalizedBoolean(CultureInfo.GetCultureInfo("ne"), "हो", "होइन")}, ' Nepali
            {"nl", New LocalizedBoolean(CultureInfo.GetCultureInfo("nl"), "Ja", "Nee")}, ' Dutch
            {"no", New LocalizedBoolean(CultureInfo.GetCultureInfo("no"), "Ja", "Nei")}, ' Norwegian
            {"ny", New LocalizedBoolean(CultureInfo.GetCultureInfo("ny"), "Yewo", "Ayawo")}, ' Chichewa
            {"pa", New LocalizedBoolean(CultureInfo.GetCultureInfo("pa"), "ਹਾਂ", "ਨਹੀਂ")}, ' Punjabi
            {"pl", New LocalizedBoolean(CultureInfo.GetCultureInfo("pl"), "Tak", "Nie")}, ' Polish
            {"ps", New LocalizedBoolean(CultureInfo.GetCultureInfo("ps"), "هو", "نه")}, ' Pashto
            {"pt", New LocalizedBoolean(CultureInfo.GetCultureInfo("pt"), "Sim", "Não")}, ' Portuguese
            {"rm", New LocalizedBoolean(CultureInfo.GetCultureInfo("rm"), "Gia", "Betg")}, ' Romansh
            {"ro", New LocalizedBoolean(CultureInfo.GetCultureInfo("ro"), "Da", "Nu")}, ' Romanian
            {"ru", New LocalizedBoolean(CultureInfo.GetCultureInfo("ru"), "Да", "Нет")}, ' Russian
            {"sd", New LocalizedBoolean(CultureInfo.GetCultureInfo("sd"), "هاڻي", "نه")}, ' Sindhi
            {"si", New LocalizedBoolean(CultureInfo.GetCultureInfo("si"), "ඔව්", "නැත")}, ' Sinhala
            {"sk", New LocalizedBoolean(CultureInfo.GetCultureInfo("sk"), "Áno", "Nie")}, ' Slovak
            {"sl", New LocalizedBoolean(CultureInfo.GetCultureInfo("sl"), "Da", "Ne")}, ' Slovenian
            {"sm", New LocalizedBoolean(CultureInfo.GetCultureInfo("sm"), "Ioe", "Leai")}, ' Samoan
            {"sn", New LocalizedBoolean(CultureInfo.GetCultureInfo("sn"), "Yebo", "Cha")}, ' Shona
            {"so", New LocalizedBoolean(CultureInfo.GetCultureInfo("so"), "Haa", "Maya")}, ' Somali
            {"sq", New LocalizedBoolean(CultureInfo.GetCultureInfo("sq"), "Po", "Jo")}, ' Albanian
            {"sr", New LocalizedBoolean(CultureInfo.GetCultureInfo("sr"), "Да", "Не")}, ' Serbian (Cyrillic)
            {"su", New LocalizedBoolean(CultureInfo.GetCultureInfo("su"), "Iya", "Teu")}, ' Sundanese
            {"sv", New LocalizedBoolean(CultureInfo.GetCultureInfo("sv"), "Ja", "Nej")}, ' Swedish
            {"sw", New LocalizedBoolean(CultureInfo.GetCultureInfo("sw"), "Ndiyo", "Hapana")}, ' Swahili
            {"ta", New LocalizedBoolean(CultureInfo.GetCultureInfo("ta"), "ஆம்", "இல்லை")}, ' Tamil
            {"te", New LocalizedBoolean(CultureInfo.GetCultureInfo("te"), "అవును", "కాదు")}, ' Telugu
            {"tg", New LocalizedBoolean(CultureInfo.GetCultureInfo("tg"), "Ҳа", "Не")}, ' Tajik
            {"th", New LocalizedBoolean(CultureInfo.GetCultureInfo("th"), "ใช่", "ไม่")}, ' Thai
            {"ti", New LocalizedBoolean(CultureInfo.GetCultureInfo("ti"), "እወ", "አይወ")}, ' Tigrinya
            {"tk", New LocalizedBoolean(CultureInfo.GetCultureInfo("tk"), "Hawa", "Ýok")}, ' Turkmen
            {"to", New LocalizedBoolean(CultureInfo.GetCultureInfo("to"), "ʻIo", "ʻEa")}, ' Tongan
            {"tr", New LocalizedBoolean(CultureInfo.GetCultureInfo("tr"), "Evet", "Hayır")}, ' Turkish
            {"tt", New LocalizedBoolean(CultureInfo.GetCultureInfo("tt"), "Әйе", "Юк")}, ' Tatar
            {"ug", New LocalizedBoolean(CultureInfo.GetCultureInfo("ug"), "ھەئە", "ياق")}, ' Uighur
            {"uk", New LocalizedBoolean(CultureInfo.GetCultureInfo("uk"), "Так", "Ні")}, ' Ukrainian
            {"ur", New LocalizedBoolean(CultureInfo.GetCultureInfo("ur"), "جی ہاں", "نہیں")}, ' Urdu
            {"uz", New LocalizedBoolean(CultureInfo.GetCultureInfo("uz"), "Ha", "Yo'q")}, ' Uzbek
            {"vi", New LocalizedBoolean(CultureInfo.GetCultureInfo("vi"), "Có", "Không")}, ' Vietnamese
            {"xh", New LocalizedBoolean(CultureInfo.GetCultureInfo("xh"), "Ewe", "Hayi")}, ' Xhosa
            {"yi", New LocalizedBoolean(CultureInfo.GetCultureInfo("yi"), "יאָ", "ניי")}, ' Yiddish
            {"yo", New LocalizedBoolean(CultureInfo.GetCultureInfo("yo"), "Bẹẹni", "Bẹẹkoo")}, ' Yoruba
            {"zh", New LocalizedBoolean(CultureInfo.GetCultureInfo("zh"), "是", "不")}, ' Chinese (Simplified)
            {"zu", New LocalizedBoolean(CultureInfo.GetCultureInfo("zu"), "Yebo", "Cha")} ' Zulu
        }
    End Sub

    ''' <summary>
    ''' Initializes a new instance of the <see cref="LocalizedBoolean"/> class.
    ''' </summary>
    '''
    ''' <param name="cultureNames">
    ''' A comma-separated value of the ISO 639-1 two-letter code languages (e.g.: "en,es,fr").
    ''' </param>
    '''
    ''' <param name="trueStrings">
    ''' A comma-separated value of the localized string representation for "True" boolean value (e.g.: "Yes,Sí,Oui").
    ''' </param>
    '''
    ''' <param name="falseStrings">
    ''' A comma-separated value of the localized string representation for "False" boolean value (e.g.: "No,No,Non").
    ''' </param>
    Public Sub New(cultureNames As String, trueStrings As String, falseStrings As String)
        Me.New()

        If String.IsNullOrWhiteSpace(cultureNames) Then
            Throw New ArgumentNullException(paramName:=NameOf(cultureNames))
        End If
        If String.IsNullOrWhiteSpace(trueStrings) Then
            Throw New ArgumentNullException(paramName:=NameOf(trueStrings))
        End If
        If String.IsNullOrWhiteSpace(falseStrings) Then
            Throw New ArgumentNullException(paramName:=NameOf(falseStrings))
        End If

        Dim cultureNamesArray As String() = cultureNames.Split({","c}, StringSplitOptions.RemoveEmptyEntries)
        Dim trueStringsArray As String() = trueStrings.Split({","c}, StringSplitOptions.RemoveEmptyEntries)
        Dim falseStringsArray As String() = falseStrings.Split({","c}, StringSplitOptions.RemoveEmptyEntries)

        If cultureNamesArray.Length <> trueStringsArray.Length OrElse cultureNamesArray.Length <> falseStringsArray.Length Then
            Throw New InvalidOperationException("The comma-separated values must have the same amount of tokens.")
        End If

        For i As Integer = 0 To cultureNamesArray.Length - 1
            Dim cultureName As String = cultureNamesArray(i).Trim()
            Dim trueString As String = trueStringsArray(i).Trim()
            Dim falseString As String = falseStringsArray(i).Trim()

            If cultureName.Length <> 2 Then
                Throw New InvalidOperationException("The culture name must be a ISO 639-1 two-letter code.")
            End If

            Dim localizedBoolean As New LocalizedBoolean(CultureInfo.GetCultureInfo(cultureName), trueString, falseString)
            If Me.Localizations.ContainsKey(cultureName) Then
                Me.Localizations(cultureName) = localizedBoolean
            Else
                Me.Localizations.Add(cultureName, localizedBoolean)
            End If
        Next
    End Sub

End Class

LocalizableBooleanConverter.vb
Código
''' <summary>
''' Provides conversion functionality between Boolean values and localized strings representing "True" and "False" boolean values.
''' </summary>
Public Class LocalizableBooleanConverter : Inherits TypeConverter
 
   ''' <summary>
   ''' The localized string representation for "True" boolean value.
   ''' </summary>
   Private trueString As String = "Yes"
 
   ''' <summary>
   ''' The localized string representation for "False" boolean value.
   ''' </summary>
   Private falseString As String = "No"
 
   ''' <summary>
   ''' Returns whether this converter can convert an object of the given type to the type of this converter,
   ''' using the specified context.
   ''' </summary>
   '''
   ''' <param name="context">
   ''' An <see cref="ITypeDescriptorContext"/> that provides a format context.
   ''' </param>
   '''
   ''' <param name="sourceType">
   ''' A <see cref="Type" /> that represents the type you want to convert from.
   ''' </param>
   '''
   ''' <returns>
   ''' <see langword="True"/> if this converter can perform the conversion; otherwise, <see langword="False"/>.
   ''' </returns>
   Public Overrides Function CanConvertFrom(context As ITypeDescriptorContext, sourceType As Type) As Boolean
 
       Return sourceType = GetType(String) OrElse MyBase.CanConvertFrom(context, sourceType)
 
   End Function
 
   ''' <summary>
   ''' Returns whether this converter can convert the object to the specified type, using the specified context.
   ''' </summary>
   '''
   ''' <param name="context">
   ''' An <see cref="ITypeDescriptorContext"/> that provides a format context.
   ''' </param>
   '''
   ''' <param name="destinationType">
   ''' A <see cref="Type"/> that represents the type you want to convert to.
   ''' </param>
   '''
   ''' <returns>
   ''' <see langword="True"/> if this converter can perform the conversion; otherwise, <see langword="False"/>.
   ''' </returns>
   Public Overrides Function CanConvertTo(context As ITypeDescriptorContext, destinationType As Type) As Boolean
 
       Return destinationType = GetType(String) OrElse MyBase.CanConvertTo(context, destinationType)
 
   End Function
 
   ''' <summary>
   ''' Converts the given object to the type of this converter, using the specified context and culture information.
   ''' </summary>
   '''
   ''' <param name="context">
   ''' An <see cref="ITypeDescriptorContext"/> that provides a format context.
   ''' </param>
   '''
   ''' <param name="culture">
   ''' The <see cref="CultureInfo"/> to use as the current culture.
   ''' </param>
   '''
   ''' <param name="value">
   ''' The <see cref="Object"/> to convert.
   ''' </param>
   '''
   ''' <returns>
   ''' An <see cref="Object"/> that represents the converted value.
   ''' </returns>
   <DebuggerStepperBoundary>
   Public Overrides Function ConvertFrom(context As ITypeDescriptorContext, culture As Globalization.CultureInfo, value As Object) As Object
 
       If TypeOf value Is String Then
           Dim stringValue As String = DirectCast(value, String)
           If String.Equals(stringValue, Me.trueString, StringComparison.OrdinalIgnoreCase) Then
               Return True
           ElseIf String.Equals(stringValue, Me.FalseString, StringComparison.OrdinalIgnoreCase) Then
               Return False
           End If
       End If
 
       Return MyBase.ConvertFrom(context, culture, value)
 
   End Function
 
   ''' <summary>
   ''' Converts the given value object to the specified type, using the specified context and culture information.
   ''' </summary>
   '''
   ''' <param name="context">
   ''' An <see cref="ITypeDescriptorContext"/> that provides a format context.
   ''' </param>
   '''
   ''' <param name="culture">
   ''' A <see cref="CultureInfo"/>. If null is passed, the current culture is assumed.
   ''' </param>
   '''
   ''' <param name="value">
   ''' The <see cref="Object"/> to convert.
   ''' </param>
   '''
   ''' <param name="destinationType">
   ''' The <see cref="Type"/> to convert the <paramref name="value"/> parameter to.
   ''' </param>
   '''
   ''' <returns>
   ''' An <see cref="Object"/> that represents the converted value.
   ''' </returns>
   <DebuggerStepperBoundary>
   Public Overrides Function ConvertTo(context As ITypeDescriptorContext, culture As Globalization.CultureInfo, value As Object, destinationType As Type) As Object
 
       If context IsNot Nothing Then
           Dim attributes As IEnumerable(Of LocalizableBooleanAttribute) =
               context.PropertyDescriptor.Attributes.OfType(Of LocalizableBooleanAttribute)
 
           For Each attr As LocalizableBooleanAttribute In attributes
               Dim uiCulture As CultureInfo = My.Application.UICulture
               Dim localizedBoolean As LocalizedBoolean = Nothing
               If attr.Localizations.ContainsKey(uiCulture.TwoLetterISOLanguageName) Then
                   localizedBoolean = attr.Localizations(uiCulture.TwoLetterISOLanguageName)
               End If
 
               If localizedBoolean IsNot Nothing Then
                   Me.trueString = localizedBoolean.True
                   Me.falseString = localizedBoolean.False
               End If
           Next
       End If
 
       If destinationType = GetType(String) Then
           If TypeOf value Is Boolean Then
               Dim boolValue As Boolean = value
               Return If(boolValue, Me.trueString, Me.falseString)
           End If
       End If
 
       Return MyBase.ConvertTo(context, culture, value, destinationType)
 
   End Function
 
   ''' <summary>
   ''' Returns a collection of standard values for the data type this type converter is designed for when provided with a format context.
   ''' </summary>
   '''
   ''' <param name="context">
   ''' An <see cref="ITypeDescriptorContext"/> that provides a format context that can be used to
   ''' extract additional information about the environment from which this converter is invoked.
   ''' <para></para>
   ''' This parameter or properties of this parameter can be null.
   ''' </param>
   '''
   ''' <returns>
   ''' A <see cref="StandardValuesCollection"/> that holds a standard set of valid values,
   ''' or <see langword="null" /> if the data type does not support a standard set of values.
   ''' </returns>
   Public Overrides Function GetStandardValues(context As ITypeDescriptorContext) As StandardValuesCollection
 
       Return New StandardValuesCollection(New Boolean() {True, False})
 
   End Function
 
   ''' <summary>
   ''' Returns whether this object supports a standard set of values that can be picked from a list, using the specified context.
   ''' </summary>
   '''
   ''' <param name="context">
   ''' An <see cref="ITypeDescriptorContext" /> that provides a format context.
   ''' </param>
   '''
   ''' <returns>
   ''' <see langword="True"/> if <see cref="TypeConverter.GetStandardValues"/> should be called to
   ''' find a common set of values the object supports; otherwise, <see langword="False"/>.
   ''' </returns>
   Public Overrides Function GetStandardValuesSupported(context As ITypeDescriptorContext) As Boolean
 
       Return True
 
   End Function
 
End Class

NOTA: El diccionario con los idiomas y sus equivalentes para "Sí" y "No", lo ha generado ChatGPT. Puede haber fallos en las traducciones, o en los códigos ISO 639-1 de dos letras. Además, faltaría añadir muchos más idiomas: https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes

Eleкtro:
Comparto otro type converter, para convertir los nombres de los valores de una Enum, a una representación amistosa para mostrarlos, por ejemplo, en un propertygrid.

Este convertidor está optimizado para nombres de enumeración escritos en upper/lower snake case y upper/lower camel case.

Las palabras se separan con un espacio en blanco convencional, y los guiones bajos se reemplazan por un espacio en blanco unicode.

Ejemplo de uso:

Código
<TypeConverter(GetType(EnumNameFormatterConverter))>
Public Enum TestEnum
   MyUpperCamelCaseName
   myLowerCamelCaseName
   My_Upper_Snake_Case_Name
   my_lower_snake_case_name
 
   MyMixed_value123_WTF456wtf_
 
   ___rare_case_STRANGE_Name___________123_aZ_Az_4_5_6_
End Enum

Código
<DefaultValue(TestEnum.MyUpperCamelCaseName)>
Public Property Test As TestEnum = TestEnum.MyUpperCamelCaseName

Sin formato:


Con formato:



El código:

EnumNameFormatterConverter.vb
Código
Imports System.ComponentModel
Imports System.Globalization
Imports System.Runtime.InteropServices
Imports System.Text
 
''' <summary>
''' Provides conversion functionality between the value names of an <see cref="[Enum]"/> to a friendly string representation.
''' <para></para>
''' This converter is optimized for enum names written in either upper/lower snake case or upper/lower camel case:
''' <list type="bullet">
'''     <item><description>Snake case: Each word is separated by underscores (e.g.: "My_Value").</description></item>
'''     <item><description>Camel case: Each word is separated by a capitalized letter (e.g.: "MyValue").</description></item>
''' </list>
''' </summary>
Public NotInheritable Class EnumNameFormatterConverter : Inherits EnumConverter
 
   ''' <summary>
   ''' Initializes a new instance of the <see cref="EnumNameFormatterConverter"/> class.
   ''' </summary>
   ''' <param name="type">A <see cref="T:System.Type" /> that represents the type of enumeration to associate with this enumeration converter.</param>
   Public Sub New(type As Type)
       MyBase.New(type)
   End Sub
 
   ''' <summary>
   ''' Returns whether this converter can convert an object of the given type to the type of this converter,
   ''' using the specified context.
   ''' </summary>
   '''
   ''' <param name="context">
   ''' An <see cref="ITypeDescriptorContext"/> that provides a format context.
   ''' </param>
   '''
   ''' <param name="sourceType">
   ''' A <see cref="Type" /> that represents the type you want to convert from.
   ''' </param>
   '''
   ''' <returns>
   ''' <see langword="True"/> if this converter can perform the conversion; otherwise, <see langword="False"/>.
   ''' </returns>
   Public Overrides Function CanConvertFrom(context As ITypeDescriptorContext, sourceType As Type) As Boolean
 
       Return sourceType Is GetType(String) OrElse
              MyBase.CanConvertFrom(context, sourceType)
 
   End Function
 
   ''' <summary>
   ''' Returns whether this converter can convert the object to the specified type, using the specified context.
   ''' </summary>
   '''
   ''' <param name="context">
   ''' An <see cref="ITypeDescriptorContext"/> that provides a format context.
   ''' </param>
   '''
   ''' <param name="destinationType">
   ''' A <see cref="Type"/> that represents the type you want to convert to.
   ''' </param>
   '''
   ''' <returns>
   ''' <see langword="True"/> if this converter can perform the conversion; otherwise, <see langword="False"/>.
   ''' </returns>
   Public Overrides Function CanConvertTo(context As ITypeDescriptorContext, destinationType As Type) As Boolean
 
       Return destinationType Is GetType(String) OrElse
              MyBase.CanConvertTo(context, destinationType)
 
   End Function
 
   ''' <summary>
   ''' Converts the given object to the type of this converter, using the specified context and culture information.
   ''' </summary>
   '''
   ''' <param name="context">
   ''' An <see cref="ITypeDescriptorContext"/> that provides a format context.
   ''' </param>
   '''
   ''' <param name="culture">
   ''' The <see cref="CultureInfo"/> to use as the current culture.
   ''' </param>
   '''
   ''' <param name="value">
   ''' The <see cref="Object"/> to convert.
   ''' </param>
   '''
   ''' <returns>
   ''' An <see cref="Object"/> that represents the converted value.
   ''' </returns>
   <DebuggerStepThrough>
   Public Overrides Function ConvertFrom(context As ITypeDescriptorContext, culture As CultureInfo, value As Object) As Object
 
       If TypeOf value Is String Then
           value = DirectCast(value, String).Replace(" ", "").Replace(Convert.ToChar(&H205F), "_"c)
           Return [Enum].Parse(Me.EnumType, value, ignoreCase:=True)
       End If
 
       Return MyBase.ConvertFrom(context, culture, value)
 
   End Function
 
   ''' <summary>
   ''' Converts the given value object to the specified type, using the specified context and culture information.
   ''' </summary>
   '''
   ''' <param name="context">
   ''' An <see cref="ITypeDescriptorContext"/> that provides a format context.
   ''' </param>
   '''
   ''' <param name="culture">
   ''' A <see cref="CultureInfo"/>. If null is passed, the current culture is assumed.
   ''' </param>
   '''
   ''' <param name="value">
   ''' The <see cref="Object"/> to convert.
   ''' </param>
   '''
   ''' <param name="destinationType">
   ''' The <see cref="Type"/> to convert the <paramref name="value"/> parameter to.
   ''' </param>
   '''
   ''' <returns>
   ''' An <see cref="Object"/> that represents the converted value.
   ''' </returns>
   <DebuggerStepThrough>
   Public Overrides Function ConvertTo(context As ITypeDescriptorContext, culture As CultureInfo, value As Object, destinationType As Type) As Object
 
       If destinationType = GetType(String) Then
           Dim name As String = [Enum].GetName(value.GetType(), value)
           If Not String.IsNullOrEmpty(name) Then
               Return Me.FormatName(name)
           End If
       End If
 
       Return MyBase.ConvertTo(context, culture, value, destinationType)
 
   End Function
 
   ''' <summary>
   ''' Formats the name of a <see cref="[Enum]"/> value to a friendly name.
   ''' </summary>
   '''
   ''' <param name="name">
   ''' <see cref="[Enum]"/> value name.
   ''' </param>
   '''
   ''' <returns>
   ''' The resulting friendly name.
   ''' </returns>
   <DebuggerStepThrough>
   Private Function FormatName(name As String) As String
       Dim sb As New StringBuilder()
       Dim previousChar As Char
       Dim previousCharIsWhiteSpace As Boolean
       Dim previousCharIsUpperLetter As Boolean
       Dim previousCharIsDigit As Boolean
       Dim lastParsedCharIsUnderscore As Boolean
       Dim firstCapitalizedLetterIsAdded As Boolean
 
       For i As Integer = 0 To name.Length - 1
           Dim c As Char = name(i)
           If i = 0 Then
               If c.Equals("_"c) Then
                   sb.Append(Convert.ToChar(Convert.ToChar(&H205F)))
                   lastParsedCharIsUnderscore = True
               Else
                   sb.Append(Char.ToUpper(c))
                   firstCapitalizedLetterIsAdded = True
               End If
               Continue For
           End If
 
           previousChar = sb.Chars(sb.Length - 1)
           previousCharIsWhiteSpace = previousChar.Equals(" "c) OrElse previousChar.Equals(Convert.ToChar(&H205F))
           previousCharIsUpperLetter = Char.IsUpper(previousChar)
           previousCharIsDigit = Char.IsDigit(previousChar)
 
           If Char.IsLetter(c) Then
               If previousCharIsDigit AndAlso Not previousCharIsWhiteSpace Then
                   sb.Append(" "c)
               End If
 
               If Char.IsUpper(c) Then
                   If previousCharIsUpperLetter Then
                       sb.Append(c)
                   ElseIf Not previousCharIsWhiteSpace Then
                       sb.Append(" "c)
                       sb.Append(c)
                   Else
                       sb.Append(c)
                   End If
                   firstCapitalizedLetterIsAdded = True
 
               Else
                   If Not firstCapitalizedLetterIsAdded Then
                       sb.Append(Char.ToUpper(c))
                       firstCapitalizedLetterIsAdded = True
                   Else
                       sb.Append(c)
                   End If
 
               End If
 
           ElseIf Char.IsDigit(c) Then
               If Not previousCharIsDigit AndAlso Not previousCharIsWhiteSpace Then
                   sb.Append(" "c)
               End If
               sb.Append(c)
 
           ElseIf c.Equals("_"c) Then
               If lastParsedCharIsUnderscore OrElse Not previousCharIsWhiteSpace Then
                   sb.Append(Convert.ToChar(&H205F)) ' Unicode white-space: "&#8195;"
                   lastParsedCharIsUnderscore = True
               End If
 
           Else
               sb.Append(c)
               lastParsedCharIsUnderscore = False
 
           End If
 
       Next i
 
       Return sb.ToString()
   End Function
 
End Class
 

Eleкtro:
Comparto un enfoque y uso alternativo al código que he publicado arriba. Este enfoque nos permite atribuir nombres específicos a una enumeración para mostrarlos en un property grid.

Modo de empleo:

Código
Imports System.Componentmodel
 
<TypeConverter(GetType(EnumDescriptionConverter))>
Public Enum TestEnum
   <Description("My Upper Camel Case Name")> MyUpperCamelCaseName = 1
   <Description("My Lower Camel Case Name")> myLowerCamelCaseName = 2
   <Description("My Upper Snake Case Name")> My_Upper_Snake_Case_Name = 3
   <Description("My lower snake case Name")> my_lower_snake_case_Name = 4
   <Description("My Mixed value 123 QWERTY 456 wtf_")> MyMixed_value123_QWERTY456wtf_ = 5
   <Description("Rare case STRANGE Name 123 aZ Az 456")> ___rare_case_STRANGE_Name___________123_aZ_Az_4_5_6_ = 6
End Enum

Código
<DefaultValue(TestEnum.MyUpperCamelCaseName)>
Public Property Test As TestEnum = TestEnum.MyUpperCamelCaseName

El código:

Código
Imports System.ComponentModel
Imports System.Globalization
Imports System.Reflection
 
Public NotInheritable Class EnumDescriptionConverter : Inherits EnumConverter
 
''' <summary>
''' Initializes a new instance of the <see cref="EnumDescriptionConverter"/> class.
''' </summary>
''' <param name="type">A <see cref="T:System.Type" /> that represents the type of enumeration to associate with this enumeration converter.</param>
Public Sub New(type As Type)
MyBase.New(type)
End Sub
 
''' <summary>
''' Returns whether this converter can convert the object to the specified type, using the specified context.
''' </summary>
'''
''' <param name="context">
''' An <see cref="ITypeDescriptorContext"/> that provides a format context.
''' </param>
'''
''' <param name="destinationType">
''' A <see cref="Type"/> that represents the type you want to convert to.
''' </param>
'''
''' <returns>
''' <see langword="True"/> if this converter can perform the conversion; otherwise, <see langword="False"/>.
''' </returns>
Public Overrides Function CanConvertTo(context As ITypeDescriptorContext, destinationType As Type) As Boolean
 
Return destinationType Is GetType(String) OrElse
  MyBase.CanConvertTo(context, destinationType)
 
End Function
 
''' <summary>
''' Returns whether this converter can convert an object of the given type to the type of this converter,
''' using the specified context.
''' </summary>
'''
''' <param name="context">
''' An <see cref="ITypeDescriptorContext"/> that provides a format context.
''' </param>
'''
''' <param name="sourceType">
''' A <see cref="Type" /> that represents the type you want to convert from.
''' </param>
'''
''' <returns>
''' <see langword="True"/> if this converter can perform the conversion; otherwise, <see langword="False"/>.
''' </returns>
Public Overrides Function CanConvertFrom(context As ITypeDescriptorContext, sourceType As Type) As Boolean
 
Return sourceType Is GetType(String) OrElse
  MyBase.CanConvertFrom(context, sourceType)
 
End Function
 
''' <summary>
''' Converts the given value object to the specified type, using the specified context and culture information.
''' </summary>
'''
''' <param name="context">
''' An <see cref="ITypeDescriptorContext"/> that provides a format context.
''' </param>
'''
''' <param name="culture">
''' A <see cref="CultureInfo"/>. If null is passed, the current culture is assumed.
''' </param>
'''
''' <param name="value">
''' The <see cref="Object"/> to convert.
''' </param>
'''
''' <param name="destinationType">
''' The <see cref="Type"/> to convert the <paramref name="value"/> parameter to.
''' </param>
'''
''' <returns>
''' An <see cref="Object"/> that represents the converted value.
''' </returns>
<DebuggerStepThrough>
Public Overrides Function ConvertTo(context As ITypeDescriptorContext, culture As CultureInfo, value As Object, destinationType As Type) As Object
 
Dim fi As FieldInfo = Me.EnumType.GetField([Enum].GetName(Me.EnumType, value))
Dim dna As DescriptionAttribute = CType(Attribute.GetCustomAttribute(fi, GetType(DescriptionAttribute)), DescriptionAttribute)
Return If(dna IsNot Nothing, dna.Description, value.ToString())
 
End Function
 
''' <summary>
''' Converts the given object to the type of this converter, using the specified context and culture information.
''' </summary>
'''
''' <param name="context">
''' An <see cref="ITypeDescriptorContext"/> that provides a format context.
''' </param>
'''
''' <param name="culture">
''' The <see cref="CultureInfo"/> to use as the current culture.
''' </param>
'''
''' <param name="value">
''' The <see cref="Object"/> to convert.
''' </param>
'''
''' <returns>
''' An <see cref="Object"/> that represents the converted value.
''' </returns>
<DebuggerStepThrough>
Public Overrides Function ConvertFrom(context As ITypeDescriptorContext, culture As CultureInfo, value As Object) As Object
 
For Each fi As FieldInfo In Me.EnumType.GetFields()
Dim dna As DescriptionAttribute = CType(Attribute.GetCustomAttribute(fi, GetType(DescriptionAttribute)), DescriptionAttribute)
If (dna IsNot Nothing) AndAlso DirectCast(value, String) = dna.Description Then
Return [Enum].Parse(Me.EnumType, fi.Name, ignoreCase:=False)
End If
Next fi
 
Return [Enum].Parse(Me.EnumType, DirectCast(value, String))
 
End Function
 
End Class
 

Navegación

[0] Índice de Mensajes

[*] Página Anterior