For Programmers: Free Programming Magazines  


Home > Archive > C# > December 2005 > Retrieve Class and Property information at runtime.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Retrieve Class and Property information at runtime.
Rémy Samulski

2005-12-11, 8:10 am

Dear readers,


I'm trying to retrieve Class and Property information at runtime. For
example the
Reflector application written by Lutz Roeder retrieves the following
documentation from the System.Web.UI.WebControls.Button class:
"Displays a push button control on the Web page.". Furthermore I like
to retrieve the Categories of the properties (Appearance for
BackColor/BorderColor/BorderStyle/etc.).
Here is the reason why: I want to retrieve this information: I'm sort
of
writing a programming/designing environment for a CMS.


Many thanks for any hints and especially code examples,
R=E9my Samulski

agapeton@gmail.com

2005-12-11, 7:14 pm

This is called reflection (ergo the application name)

System.Reflection.Assembly reflectionLibrary =
System.Reflection.Assembly.LoadFrom(@"D:\Documents and Settings\David
Betz\My Documents\Visual Studio
2005\Projects\Reflection\ReflectionTest\
bin\Debug\ReflectionTest.dll");
foreach (Type type in reflectionLibrary.GetTypes( )) {
MessageBox.Show("Type: " + type.ToString( ));
foreach (System.Reflection.MethodInfo methodInfo in
type.GetMethods( )) {
MessageBox.Show("MethodInfo: " +
methodInfo.ToString( ));
}
}


--
David Betz

Bruce Wood

2005-12-12, 4:13 am

Read up on Reflection namespace. It contains everything you need to
examine the class hierarchy at runtime.

Rémy Samulski

2005-12-15, 7:57 am

Both Betz and Wood thanks for your reply. I have found some information
I needed (see code below yes sorry it's in VB.net (Commodore 64 child))
similar to the code presented by David. But I still can't find the
overall (and maybe there is none) description of a class.

Public Structure PropertyDefinition
Dim Name As String
Dim Description As String
Dim Category As String
Dim ValueType As Type
End Structure

Public Shared Function GetPropertyNames( _
ByRef objObject As Object, _
Optional ByVal blnCheckCanRead As Boolean = False, _
Optional ByVal blnCheckCanWrite As Boolean = False _
) As String()
Dim stringPropertyNames() As String

Dim typObjectType As Type
typObjectType = objObject.GetType

Dim priObjectProperties() As System.Reflection.PropertyInfo
= typObjectType.GetProperties((System.Reflection.BindingFlags.Public Or
System.Reflection.BindingFlags.Instance))
Dim priObjectProperty As System.Reflection.PropertyInfo

For Each priObjectProperty In priObjectProperties
Dim blnReturn As Boolean = True
If blnCheckCanRead = True AndAlso
priObjectProperty.CanRead = False Then
blnReturn = False
End If
If blnCheckCanWrite = True AndAlso
priObjectProperty.CanWrite = False Then
blnReturn = False
End If

If blnReturn = True Then

ArrayFunctions.StringArray1DimAdd(stringPropertyNames,
priObjectProperty.Name)
End If
Next

Return stringPropertyNames
End Function

Public Shared Function GetPropertyDefinitions( _
ByRef objObject As Object, _
Optional ByVal blnCheckCanRead As Boolean = False, _
Optional ByVal blnCheckCanWrite As Boolean = False _
) As PropertyDefinition()
Dim _stringPropertyNames() As String

_stringPropertyNames = GetPropertyNames(objObject,
blnCheckCanRead, blnCheckCanWrite)

If IsNothing(_stringPropertyNames) = False Then
Dim
_PropertyDefinitions(_stringPropertyName
s.GetLength(0) - 1) As
PropertyDefinition

Dim _stringPropertyName As String
Dim _PropertyInfo As System.Reflection.PropertyInfo
Dim _integerCounter As Integer = 0
For Each _stringPropertyName In _stringPropertyNames
With _PropertyDefinitions(_integerCounter)
.Name = _stringPropertyName

_PropertyInfo = GetProperty(objObject,
_stringPropertyName)
Dim _objects() As Object
_objects =
_PropertyInfo.GetCustomAttributes(True)

Dim _object As Object
For Each _object In _objects
If TypeOf _object Is
System.ComponentModel.DefaultValueAttribute Then
Dim _objectChgType As
System.ComponentModel.DefaultValueAttribute
_objectChgType = CType(_object,
System.ComponentModel.DefaultValueAttribute)
.ValueType =
_objectChgType.Value.GetType
End If
If TypeOf _object Is
System.ComponentModel.DescriptionAttribute Then
Dim _objectChgType As
System.ComponentModel.DescriptionAttribute
_objectChgType = CType(_object,
System.ComponentModel.DescriptionAttribute)
.Description =
_objectChgType.Description
End If
If TypeOf _object Is
System.ComponentModel.CategoryAttribute Then
Dim _objectChgType As
System.ComponentModel.CategoryAttribute
_objectChgType = CType(_object,
System.ComponentModel.CategoryAttribute)
.Category = _objectChgType.Category
End If
Next
If IsNothing(.ValueType) = True Then
Try
.ValueType =
LiQuick_net.Utilities.ClassMemberFunctions.PropertyGet(objObject,
_stringPropertyName).GetType
Catch ex As Exception

End Try
End If
End With
_integerCounter += 1
Next
Else
Return Nothing
End If
End Function

Public Shared Function GetProperty( _
ByRef objObject As Object, _
ByVal strPropertyName As String _
) As System.Reflection.PropertyInfo
Dim typObjectType As Type
typObjectType = objObject.GetType

Dim priObjectProperties() As System.Reflection.PropertyInfo
= typObjectType.GetProperties((System.Reflection.BindingFlags.Public Or
System.Reflection.BindingFlags.Instance))

Dim priObjectProperty As System.Reflection.PropertyInfo
For Each priObjectProperty In priObjectProperties
If priObjectProperty.Name = strPropertyName Then
Return priObjectProperty
End If
Next
Return Nothing
End Function

Public Shared Function PropertyGet( _
ByRef objObject As Object, _
ByVal strPropertyName As String _
) As Object
Dim priObjectProperty As System.Reflection.PropertyInfo

priObjectProperty = GetProperty(objObject, strPropertyName)
If IsNothing(priObjectProperty) = False Then
Return priObjectProperty.GetValue(objObject, Nothing)
Else
Return Nothing
End If
End Function

Bruce Wood

2005-12-15, 7:01 pm

You want to look at the "Attributes" properties of PropertyInfo,
MethodInfo, etc.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com