obtaining type using reflection

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

In this moment I'm writing a new SQLAdapter class to persist my objects.
the idea is to receive an object, and using reflection obtain the field
name, the field value and the field type.
At this moment, name and value was obtained, but the value type of the field
always returns object instead of string or int or datetime.
Any idea regarding this problem?
TIA
 
Can you post a short but complete sample of code that demonstrates what you are doing?

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.framework/<[email protected]>

In this moment I'm writing a new SQLAdapter class to persist my objects.
the idea is to receive an object, and using reflection obtain the field
name, the field value and the field type.
At this moment, name and value was obtained, but the value type of the field
always returns object instead of string or int or datetime.
Any idea regarding this problem?
TIA
 
My code:

Public Sub persistMsgDetails(ByRef theSObject As Object, _
ByRef theClassName As String, _
ByRef theTransaction As SqlTransaction)
Try
' Get the type of the class.
Dim myType As Type = Type.GetType(theClassName)
' Get the members associated with the class.
Dim myMembers As MemberInfo() = myType.GetProperties()
Dim myValueMembers As MemberInfo() =
myType.GetMembers(BindingFlags.DeclaredOnly)
Dim InfoProperty As System.Reflection.PropertyInfo
Dim theType As System.Type
Dim SId As Integer
Dim theName, theValue As String
Tipo = theSObject.GetType
InfoProperty = Tipo.GetProperty("S_ID",
BindingFlags.IgnoreCase Or BindingFlags.Instance Or BindingFlags.Public)
' Display the attributes for each of the members of the class.
Dim i As Integer
For i = 0 To myMembers.Length - 1
theName = myMembers(i).Name()
InfoProperty = theType.GetProperty(myMembers(i).Name,
BindingFlags.IgnoreCase Or BindingFlags.Instance Or BindingFlags.Public)
theValue = InfoPropiedad.GetValue(theSObject, Nothing)
If theName.StartsWith("F") And theValue.Length > 0 Then
'Inserts into DataBase
MyBase.insertField(SwiftId,
InfoProperty.GetValue(theSObject, Nothing), theName, 0, theTransaction)
End If
Next i
Catch e As Exception
Console.WriteLine(("Exception Caught! " + e.Message))
End Try
End Sub

Can you help me?
 
Back
Top