locked
GetCustomAttribute no funciona en Visual Basic .NET RRS feed

  • Pregunta

  • Hola, 

    Estoy intentando usar el metodo GetCustomAttribute en un proyecto de Visual Basic .Net que emplea el Framework 4, sin embargo me encuentro con que en este Framework dicho metodo no existe, ya que es una extensión que se implemento apartir del 4.5.

    Estoy intentando crear esa extensión pero nada que me funciona para Visual Basic, para C# si funciona, Ejemplo:

    public static Attribute GetCustomAttribute(this MemberInfo element, Type attributeType)
    {
    	return Attribute.GetCustomAttribute(element, attributeType);
    }
    public static T GetCustomAttribute<T>(this MemberInfo element) where T : Attribute
    {
    	return (T)GetCustomAttribute(element, typeof(T));
    }		

    La implementación:

    public static T MapFrom<T>(DataRow row) where T : new()
    {
    	PropertyInfo[] properties = typeof(T).GetProperties();
    	MapsTo[] mapsList = new MapsTo[properties.Length];
    	for (int i = 0; i < properties.Length; i++)
    	{
    		mapsList[i] = properties[i].GetCustomAttribute<MapsTo>();
    	}
    	return MapFrom<T>(row, properties, mapsList);
    }

    Pero en Visual Basic este es el código (ojo no soy muy bueno en Visual Basic, quiza es error mio en la conversión de C# a VB):

    Public Class CustomAttributeExtensions Public Shared Function GetCustomAttribute(element As MemberInfo, attributeType As Type) As Attribute Return Attribute.GetCustomAttribute(element, attributeType) End Function Public Shared Function GetCustomAttribute(Of T As Attribute)(element As MemberInfo) As T Return DirectCast(GetCustomAttribute(element, GetType(T)), T) End Function

    Public Shared Function MapFrom(Of T As New)(row As DataRow) As T
            Dim properties As PropertyInfo() = GetType(T).GetProperties()
            Dim mapsList As MapsTo() = New MapsTo(properties.Length - 1) {}
            For i As Integer = 0 To properties.Length - 1
                mapsList(i) = properties(i).GetCustomAttribute(Of MapsTo)()
            Next
            Return MapFrom(Of T)(row, properties, mapsList)
        End Function End Class

    El error surge en la linea "mapsList(i) = properties(i).GetCustomAttribute(Of MapsTo)()" y dice:

    "'GetCustomAttribute' is not a member of 'System.Reflection.PropertyInfo"

    Espero que me puedan ayudar:

    Esta en teoría es la extensión que se implementa en apartir de la 4.5
    http://referencesource.microsoft.com/#mscorlib/system/reflection/CustomAttributeExtensions.cs


    • Editado AdyIr lunes, 2 de octubre de 2017 14:37
    lunes, 2 de octubre de 2017 14:22

Respuestas

  • Hola,

    La solución es no crear una clase si no un Módulo, el código correcto el cual hace lo que necesito es:

    Public Module CustomAttributeExtensions
        <System.Runtime.CompilerServices.Extension> _
        Public Function GetCustomAttribute(element As MemberInfo, attributeType As Type) As Attribute
            Return Attribute.GetCustomAttribute(element, attributeType)
        End Function
        <System.Runtime.CompilerServices.Extension> _
        Public Function GetCustomAttribute(Of T As Attribute)(element As MemberInfo) As T
            Return DirectCast(GetCustomAttribute(element, GetType(T)), T)
        End Function
    End Module

    Saludos

    martes, 3 de octubre de 2017 13:17

Todas las respuestas

  • Te falta ponerle el atributo <Extension> a la version en VB (ese es el equivalente al this... en la lista de parametros de C#, es lo que le indica que es un metodo de extension).
    lunes, 2 de octubre de 2017 14:28
  • Hola,

    La solución es no crear una clase si no un Módulo, el código correcto el cual hace lo que necesito es:

    Public Module CustomAttributeExtensions
        <System.Runtime.CompilerServices.Extension> _
        Public Function GetCustomAttribute(element As MemberInfo, attributeType As Type) As Attribute
            Return Attribute.GetCustomAttribute(element, attributeType)
        End Function
        <System.Runtime.CompilerServices.Extension> _
        Public Function GetCustomAttribute(Of T As Attribute)(element As MemberInfo) As T
            Return DirectCast(GetCustomAttribute(element, GetType(T)), T)
        End Function
    End Module

    Saludos

    martes, 3 de octubre de 2017 13:17