Check an enum for an attribute

  • Thread starter Ray Cassick \(Home\)
  • Start date
R

Ray Cassick \(Home\)

I have a function that takes a value is as System.Enum and I want to be able
to look at that value and determine if it is a regular enum or an enum that
has a <Flag()> attribute set on it.

I am trying to write a wrapper function that takes an enum and an integer
value and validates if the value is actually part of the enum. Just trying
to use .IsDefined will not return accurate results (as far as I can find) if
the enum is a flag so if it is I want to jump to some other code and try to
validate another way.

I have been looking all over and can't find a way (probably using
reflection) to inspect this and look for any applied attributes.


--
Raymond R Cassick
CEO / CSA
Enterprocity Inc.
www.enterprocity.com
3380 Sheridan Drive, #143
Amherst, NY 14227
V: 716-316-5973
Blog: http://spaces.msn.com/members/rcassick/
 
C

Carlos J. Quintero [VB MVP]

Hi Raymond,

See it this helps:

<Flags()> _
Public Enum MyEnumWithFlags
A = 1
B = 2
End Enum

Public Enum MyEnumWithoutFlags
A = 1
B = 2
End Enum

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Call f(MyEnumWithFlags.A)
Call f(MyEnumWithoutFlags.A)
End Sub

Sub f(ByVal e As System.Enum)
Dim objType As Type

objType = e.GetType
If objType.GetCustomAttributes(GetType(FlagsAttribute),
True).GetLength(0) > 0 Then
MsgBox("It has Flags attribute")
Else
MsgBox("It does not have Flags attribute")
End If

End Sub

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 
R

Ray Cassick \(Home\)

That is VERY close to what I ended up coming up with :) Spent some more time
working on it last night after I posted and ended up getting it.

Thanks for the post.

I am always in awe of the way people help others here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top