Home All Groups Group Topic Archive Search About

Enum and FlagsAttribute

Author
9 Mar 2006 6:59 PM
Nicolas
How do I set a FlagsAttribute to a run time created enum
I really need a FlagsAttribute for the enum as it will be use to trigger multiple choice
Thanks for the help
Function BuildEnum(ByVal EnumName As String, ByVal args As Hashtable) As Type
Dim ad As AppDomain
Dim an As New AssemblyName
Dim ab As AssemblyBuilder
Dim mb As ModuleBuilder
Dim eb As EnumBuilder
Dim t As Type
Dim ie As IDictionaryEnumerator = args.GetEnumerator

Try
  ad = AppDomain.CurrentDomain
  an.Name = EnumName
  ab = ad.DefineDynamicAssembly(an, AssemblyBuilderAccess.RunAndSave)
  mb = ab.DefineDynamicModule(EnumName, EnumName & ".dll")
  eb = mb.DefineEnum(EnumName, TypeAttributes.Public, GetType(Integer))

  While ie.MoveNext
   eb.DefineLiteral(ie.Value, CInt(ie.Key))
  End While

  t = eb.CreateType()
  Return t

Catch ex As Exception
  ''do nothing
  Return Nothing

Finally

End Try
End Function

Author
9 Mar 2006 7:58 PM
Mattias Sjögren
>How do I set a FlagsAttribute to a run time created enum

Something like this:

eb.SetCustomAttribute(New
CustomAttributeBuilder(GetType(FlagsAttribute).GetConstructor(Type.EmptyTypes),
New Object() {}))


Mattias

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
9 Mar 2006 8:29 PM
Nicolas
You'r my man thanks a lot :)

"Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
news:%23hft5P7QGHA.5808@TK2MSFTNGP12.phx.gbl...
> >How do I set a FlagsAttribute to a run time created enum
>
> Something like this:
>
> eb.SetCustomAttribute(New
>
CustomAttributeBuilder(GetType(FlagsAttribute).GetConstructor(Type.EmptyType
s),
Show quoteHide quote
> New Object() {}))
>
>
> Mattias
>
> --
> Mattias Sjögren [C# MVP]  mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.