Im trying to get the name of a variable in runtime using reflection, i read a lot and find in other question a code in c# for that, now in vb.net the code looks like this
Public Shared Function GetParameterName(Of T As Class)(item As T) As String
If item Is Nothing Then
Return String.Empty
End If
Return GetType(T).GetProperties()(0).Name
End Function
the problems is when i try to call the function in c# i would be like this where test is the variable
GetParameterName(new {test});
but in Visual im cant call like that and if try like this
GetParameterName({test})
or
GetParameterName(New Object() {test})
the generic method doesn't recognize the variable the properties like name just say "Length" and the value "1"
maybe is a simple thing but i really appreciate your help
Regards
Update, Here is the original C# code that works no matter type
public static string GetParameterName<T>(T item) where T : class
{
if (item == null)
return string.Empty;
return typeof(T).GetProperties()[0].Name;
}
Update 3
yes i also dint notice the anonymous type, for other person who have a similar scenario here is te function to get the name and information of a variable
Public Shared Function GetWatch(Of T1)(item1 As T1) As String
Dim info As String = ""
Try
If GetType(T1).GetProperties()(0).GetValue(item1, Nothing) Is Nothing And GetType(T1).GetProperties()(0).Name <> "Not" Then
info &= "--------"
info &= vbCrLf
info &= "Nombre de la variable: " & GetType(T1).GetProperties()(0).Name
info &= vbCrLf
info &= "Tipo de la variable: " & GetType(T1).GetProperties()(0).PropertyType.Name
info &= vbCrLf
info &= "Valor de la variable: Nothing"
info &= vbCrLf
Else
If GetType(T1).GetProperties()(0).GetValue(item1, Nothing).ToString = "Not" Then
info &= ""
Else
info &= vbCrLf
info &= "--------"
info &= vbCrLf
info &= "Nombre de la variable: " & GetType(T1).GetProperties()(0).Name
info &= vbCrLf
info &= "Tipo de la variable: " & GetType(T1).GetProperties()(0).PropertyType.Name
info &= vbCrLf
info &= "Valor de la variable: " & GetType(T1).GetProperties()(0).GetValue(item1, Nothing).ToString
info &= vbCrLf
info &= "--------"
info &= vbCrLf
End If
End If
Return info
Catch e As Exception
Return ""
End Try
End Function
Thanks a lot to everybody for the help!!