By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,278 Members | 1,123 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,278 IT Pros & Developers. It's quick & easy.

Reflection: Determining a Method's Attributes from within the Method

P: n/a
Is it possible to determine a method's own attributes from within the method
without hardcoding the name of the method as a constant within the method?
Please tell me if it is possible and how it is done.

Thank you,
Kelly
Nov 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Kelly,

try this code. It's examining any object and print the members/attributes
into the console/output window.
(It's part of an example for a German book I'm writing, so the translation
is really quick and dirty... :-(

Hope that helps, anyway

Klaus

Sub PrintObjectInfo(ByVal [Object] As Object)
'Get Type of Object to access its Members
Dim locTypeInstanz As Type = [Object].GetType

'Print Non-Custom-Attributes
Console.WriteLine("Class' Attributes:" + locTypeInstanz.FullName)
Console.WriteLine("Standard attributes:")
Console.WriteLine(" *" + locTypeInstanz.Attributes.ToString())
Console.WriteLine()

'Getting Custom-Attributes of Class
'(This way, you can only get the custom Attributes)
Console.WriteLine("Custom Attributes:")
For Each locAttribute As Attribute In
locTypeInstanz.GetCustomAttributes(True)
Console.WriteLine(" * " + locAttribute.ToString())
Next
Console.WriteLine()

'Getting members and their attributes
Dim locMembers() As MemberInfo
locMembers = locTypeInstanz.GetMembers()
Console.WriteLine("Member-Liste:")
For Each locMember As MemberInfo In locMembers
Console.WriteLine(" *" + locMember.Name + ", " _
+ locMember.MemberType.ToString)
If locMember.GetCustomAttributes(True).Length > 0 Then
Console.WriteLine(" " + New String("-"c,
locMember.Name.Length))
For Each locAttribute As Attribute In
locMember.GetCustomAttributes(False)
Console.WriteLine(" * " +
locAttribute.ToString())
For Each locPropertyInfo As PropertyInfo In
locAttribute.GetType.GetProperties
Console.Write(" " + locPropertyInfo.Name)
Console.WriteLine(": " +
locPropertyInfo.GetValue(locAttribute, Nothing).ToString)
Next
Next
End If

If locMember.MemberType = MemberTypes.Property Then
Dim locPropertyInfo As PropertyInfo = CType(locMember,
PropertyInfo)
Console.WriteLine(" Value: " +
locPropertyInfo.GetValue([Object], Nothing).ToString)
End If
Next
End Sub

"Good Enchiladas" <go************@hotmail.com> schrieb im Newsbeitrag
news:6f*****************@fe2.columbus.rr.com...
Is it possible to determine a method's own attributes from within the method without hardcoding the name of the method as a constant within the method?
Please tell me if it is possible and how it is done.

Thank you,
Kelly

Nov 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.