[dba-VB] Iterating class properties

Shamil Salakhetdinov shamil at users.mns.ru
Tue Nov 13 14:53:39 CST 2007


Hello John,

The answer is System.Reflection namespace:

Imports System.Reflection

Module Module1

    Public Class Test
        Public ReadOnly Property One() As String
            Get
                Return "test"
            End Get
        End Property

        Private _prp2 As String
        Public WriteOnly Property Two() As String
            Set(ByVal value As String)
                _prp2 = value
            End Set
        End Property
        Public ReadOnly Property Three() As Double
            Get
                Return One + _prp2
            End Get
        End Property

    End Class

    Sub Main()
        Dim a As Assembly =
System.Reflection.Assembly.GetExecutingAssembly()

        For Each t As Type In _
          System.Reflection.Assembly.GetExecutingAssembly().GetTypes()
            If t.ToString.EndsWith("Test") Then
                Console.WriteLine(t.ToString())
                For Each m As MemberInfo In t.GetMembers()
                    If m.MemberType = MemberTypes.Property Then
                        Console.WriteLine(" " + m.Name)
                    End If
                Next
            End If
        Next
    End Sub

End Module

HTH.

--
Shamil
 
-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of jwcolby
Sent: Tuesday, November 13, 2007 8:46 PM
To: dba-vb at databaseadvisors.com
Subject: Re: [dba-VB] Iterating class properties

Well, I asked you this question earlier and didn't really receive a usable
response.  If I have a class and want to persist it to a table, how do I do
so?  I can persist it to an XML file, though even there I still do not know
how to get the data type tags to embed in the XML file.

So to persist a class to a table I would have to persist it to an XML file,
discover how to write the data type tags out to the XML file, then read the
data back in to SQL Server.  Why would I do that if I could read the class
properties and data types directly then use a data adapter or write SQL to
build the table.  

In fact I got so far as to export to XML and then pull back in to a data
adapter but the adapter threw an error when I tried to save to SQL Server
because the table did not already exist.  Even in this case I was missing
the data type information because no one could point me to how to export the
data type to XML as well. 

I am importing files from CSV and flat files.  In many cases I get a "spec"
that tells me the field names and values, but in some cases I don't even get
that, just the name and "starts at / ends at" (fixed width files).  People
throw anything into these files and it is my job to recover the data.

John W. Colby
Colby Consulting
www.ColbyConsulting.com 
-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of Charlotte Foust
Sent: Tuesday, November 13, 2007 12:25 PM
To: dba-vb at databaseadvisors.com
Subject: Re: [dba-VB] Iterating class properties

John,

I think you may be reinventing the wheel, or at least the roller.
DataAdapters will handle all or most of this for you.  The Adapter/typed
dataset KNOWS what the fields are, what the field types are, whether it
allows nulls, whether it's a key field, whether the field or record is
read only, etc.   Obviously, I'm missing something here because I work
with tables that already exist or with xml data sources.  We use SQL scripts
to build new databases/tables in SQL Server.  You can build an adapter for a
non-existent table and then use it to write the table as well.

Charlotte Foust

-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of jwcolby
Sent: Tuesday, November 13, 2007 9:14 AM
To: dba-vb at databaseadvisors.com
Subject: Re: [dba-VB] Iterating class properties

Charlotte, Drew,

What I am trying to accomplish is to be able to dynamically, at run time,
get a list of all properties of a class.  This allows me to compare values
between any two (or more) class instances of any class.
Suppose (for
example) I wanted to merge 4 records in a table into a single table.  I
don't know what the field names are but I want to load the table records
into classes and then go comparing one record against the next.

Suppose I want to know the data type of a property of a class at run time?
I have a class I want to persist to a table.  The table does not exist yet.
I need to find out what data the table holds, build a table with fields to
hold that data.  I need to discover what the properties of the class are
(what data is read / writeable) and what types of data do they hold?
I can then build SQL statements to build a table to match the class, right
then, on the fly.  Pass in a class, pop out a table in SQL Server.

I have found the answer it seems.  I am not sure I understand it yet though.

John W. Colby
Colby Consulting
www.ColbyConsulting.com
-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of Drew Wutka
Sent: Tuesday, November 13, 2007 11:55 AM
To: dba-vb at databaseadvisors.com
Subject: Re: [dba-VB] Iterating class properties

What are you trying to accomplish?  I don't use .Net very often, still do
most of my stuff in VB 6.  But I wanted a way to dynamically capture my
class properties.

For example, let's say I had Widget, and it had Height and Width properties.
I wanted to be able to do this:

Dim wd as Widget
Set wd=new Widget
Wd.Properties("Height")=10

Or

Dim wd as Widget
Dim I as long
Set wd=new Widget
For i=1 to wd.Properties.Count
	Wd.Properties(i)=15
Next i

I don't know if .Net has this capability, VB 6 didn't natively, but I was
able to 'spoof' the ability.  I created an Add-in that let's me create a
Properties property for a class.  When you run the Add-in, it goes through
all of the properties in your class, and creates statements to simulate the
abilities above.  It's adding a lot of extra code to do this, but in some
situations I have run into, it's worth it...plus, all the properties are
left alone, so I can use them like I normally would.

Drew

-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of jwcolby
Sent: Tuesday, November 13, 2007 6:43 AM
To: dba-vb at databaseadvisors.com
Subject: [dba-VB] Iterating class properties

Does anyone know a way to iterate the properties of a class in VB.Net or
C#.Net (or in .Net in general)?  There is built-in functionality to store
classes out to XML files and this stuff does exactly that, apparently using
only the Get/Let statements, i.e. if there is not both a get and let
statement for a property then the process fails to convert that property to
XML.  

So if MS does it, is that capability publicly available?  Is there a
property iterator buried in the innards of the class object?

John W. Colby
Colby Consulting
www.ColbyConsulting.com 

_______________________________________________
dba-VB mailing list
dba-VB at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/dba-vb
http://www.databaseadvisors.com

The information contained in this transmission is intended only for the
person or entity to which it is addressed and may contain II-VI Proprietary
and/or II-VI BusinessSensitve material. If you are not the intended
recipient, please contact the sender immediately and destroy the material in
its entirety, whether electronic or hard copy. You are notified that any
review, retransmission, copying, disclosure, dissemination, or other use of,
or taking of any action in reliance upon this information by persons or
entities other than the intended recipient is prohibited.


_______________________________________________
dba-VB mailing list
dba-VB at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/dba-vb
http://www.databaseadvisors.com

_______________________________________________
dba-VB mailing list
dba-VB at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/dba-vb
http://www.databaseadvisors.com


_______________________________________________
dba-VB mailing list
dba-VB at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/dba-vb
http://www.databaseadvisors.com

_______________________________________________
dba-VB mailing list
dba-VB at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/dba-vb
http://www.databaseadvisors.com




More information about the dba-VB mailing list