שתף באמצעות


Type.GetProperties() does not return all public properties

Question

Monday, August 27, 2012 6:31 PM

Hello,

I am new to dbml and reflection. I have done some research online but do not find answer to my problem.  I am working in MS VS 2008. There is data layer project in ASP.NET VB.NET solution which has dbml file. There is User class in dbml file (corresponds to real db table "Users"). Columns in real "Users" table in DB are represented as Public Properties in designer.vb file in dbml.  Then there is anothe project in the same solution that needs to retrieve list of those public properties in "User" class in dbml. Here is the code snippet:

Dim obj As Object

Using dc As MyDataContext = New MyDatacontext (ConnectionString)

obj = dc.Users

Dim type As Type = obj.GetType()

Dim propList As PropertyInfo() = type.GetProperties(BindingFlags.Instance Or BindingFlags.Public)

End Using

in DBML  designer.vb  (all generated)  has:

Partial

Public Class MyDataContext    Inherits System.Data.Linq.DataContext

Public

ReadOnly Property Users() As System.Data.Linq.Table(Of User)

Get

Return Me.GetTable(Of User)

End Get

End Property

<Table(Name:="MyDB.Users")>  _
Partial Public Class User

<Column(Storage:="_FirstName", DbType:="VarChar(35) NOT NULL", CanBeNull:=false)>  _
 Public Property FirstName() As String
  Get
   Return Me._FirstName
  End Get
  Set
   If (String.Equals(Me._FirstName, value) = false) Then
    Me.OnFirstNameChanging(value)
    Me.SendPropertyChanging
    Me._FirstName = value
    Me.SendPropertyChanged("FirstName")
    Me.OnFirstNameChanged
   End If
  End Set
 End Property

End Class

But propList does not have FirstName property. Info about only two properties returned by GetProperties() : "Context"  and "IsReadOnly".  What is wrong in the code ?  Thank you all fro replies.

All replies (4)

Monday, August 27, 2012 8:41 PM ✅Answered

You are reflecting a generic type, so you initially only see the members defined on the generic type itself.  You must then further examine the type parameter and reflect the properties of that type. In other words, Linq.Table(Of T) only exposes Context and IsReadOnly; the MyDB.Users object exposes a FirstName property.

You might want to review these links for more information:

http://msdn.microsoft.com/en-us/library/ms172334.aspx

http://msdn.microsoft.com/en-us/library/b8ytshk6.aspx

Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


Monday, August 27, 2012 5:39 PM

Hello,

I am new to dbml and reflection. I have done some research online but do not find answer to my problem.  I am working in MS VS 2008. There is data layer project in ASP.NET VB.NET solution which has dbml file. There is User class in dbml file (corresponds to real db table "Users"). Columns in real "Users" table in DB are represented as Public Properties in designer.vb file in dbml.  Then there is anothe project in the same solution that needs to retrieve list of those public properties in "User" class in dbml. Here is the code snippet:

Dim obj As Object

Using dc As MyDataContext = New MyDatacontext (ConnectionString)

obj = dc.Users

Dim type As Type = obj.GetType()

Dim propList As PropertyInfo() = type.GetProperties(BindingFlags.Instance Or BindingFlags.Public)

End Using

in DBML  designer.vb  (all generated)  has:

Partial

Public Class MyDataContext    Inherits System.Data.Linq.DataContext

Public

ReadOnly Property Users() As System.Data.Linq.Table(Of User)

Get

Return Me.GetTable(Of User)

End Get

End Property

<Table(Name:="MyDB.Users")>  _
Partial Public Class User

<Column(Storage:="_FirstName", DbType:="VarChar(35) NOT NULL", CanBeNull:=false)>  _
 Public Property FirstName() As String
  Get
   Return Me._FirstName
  End Get
  Set
   If (String.Equals(Me._FirstName, value) = false) Then
    Me.OnFirstNameChanging(value)
    Me.SendPropertyChanging
    Me._FirstName = value
    Me.SendPropertyChanged("FirstName")
    Me.OnFirstNameChanged
   End If
  End Set
 End Property

End Class

But propList does not have FirstName property. Info about only two properties returned by GetProperties() : "Context"  and "IsReadOnly".  What is wrong in the code ?  Thank you all fro replies.


Monday, August 27, 2012 5:52 PM

This is C# forum btw.

Anyway, to get properties of some class try like:

Dim mc As MyClass = New MyClass
Dim t As Type = mc.GetType
Dim properties() As PropertyInfo = t.GetProperties

Mitja


Monday, August 27, 2012 6:27 PM

I have fixed missing line about GetType in my code , and I Did select Visual Basic Forum.  I will try again to reach VB Forum.