Posts
17
Comments
3945
Trackbacks
21
A little help from Reflection...
Today a friend of mine asked me how to define enums containing white spaces, after my initial response (”Why the *** you want to do that?”) i ended up with this code that demonstrates how 'flexible' is .NET (in this case using VB but, as you know, language is just a detail...)

Created a custom attribute...

<AttributeUsage(AttributeTargets.Field)> _
Public NotInheritable Class EnumDisplay : Inherits
Attribute
Private mText As
String
Public Sub New(ByVal text As String
)
 mText = text
End
Sub
 Public ReadOnly Property Text() As
String
 Get
   
Return
mText
 End
Get
 End
Property
End
Class

...and a simple helper function that uses both Generics and Reflection...

Public Class EnumHelper
  Public Shared Function EnumToString(Of T As Structure)(ByVal value As T) As
String
  Dim sourceEnum As Type = value.GetType()
  Dim name As String = [Enum].GetName(sourceEnum, value)
  Dim fi As FieldInfo = sourceEnum.GetField(name)
  Dim att As EnumDisplay = TryCast(Attribute.GetCustomAttribute(fi, GetType(EnumDisplay)), EnumDisplay)
  If (att IsNot Nothing) Then Return att.Text
  Return name
  End
Function
End
Class

Now you can define your own Enum and decorate it with EnumDisplay where needed...

Public Enum TempState
 Low
 Middle
 <EnumDisplay("Hi Temperature"
)> HiTemperature
 TooHigh
End
Enum

And get a string representation of enum field using:

Console.WriteLine(EnumHelper.EnumToString(TempState.HiTemperature)) ' Hi Temperature

Cool!

posted on giovedì 9 febbraio 2006 00:47 Print
Comments
Gravatar
# re: A little help from Reflection...
Adrian Florea
17/02/2006 19:18
> how to define enums containing white spaces

in IL si può, però li deve poi utilizzare sempre da IL :-)
Gravatar
# re: A little help from Reflection...
written research papers
09/02/2013 14:28
Do you realize that expert writers at custom papers writing firms have an entree to the the best sources. Thence, it has to be worth to buy research papers.
Gravatar
# re: A little help from Reflection...
custom writing
01/03/2013 03:45
At any time you don't have any idea how to generate your papers for college, visit this Web site bestcustompapers.com. Everyone who wants to buy essay from this agency will be granted fair treatment and support with academic writing .
Gravatar
# re: A little help from Reflection...
Narrative essay
05/04/2013 11:15
I think that you do know how difficult can the customized essays writing be. But, you should not be frustrated, because the writing services offer the essay papers and there’s not a big problem to buy research paper (exclusivepapers.com) and be satisfied.
Gravatar
# re: A little help from Reflection...
dissertation service
08/04/2013 23:11
Thesis and dissertation "topdissertations.com" should be acceptable for students. And you write very smart dissertation contents page just about this topic.
Gravatar
# re: A little help from Reflection...
Vietnam tours
17/04/2013 14:08
A post good and useful. I like it.
Gravatar
# re: A little help from Reflection...
this Web site
01/05/2013 11:16
Drupal Development organization is wise choice to achieve your business goals. Contact SoftGroup and skillful designers make your reveries become a reality.
Gravatar
# re: A little help from Reflection...
Internet site
05/06/2013 16:41
Yeah doubtless very improving for the elocutionists it was pleasant to read about this post! If you need to get a great job firstofall you need resume writing services exclusiveresume.com. Study and don't forget - if you have to work and study at the same time, there areold pros who are ready to assist you with your resume when you under time heaviness and looking for a great job.
Gravatar
# re: A little help from Reflection...
here
09/08/2013 12:06
You don’t need to ponder over how to write a resume any longer. Expert resume writers are closer than you think. Check out Prime-Resume company so you can check resume templates or buy resumes. Be confident that you will find quality resume services.
Gravatar
# re: A little help from Reflection...
Coursework writing services
13/02/2017 07:34
Really unbelievable, is not as easy as you can see, the posts difficulty and challenge waiting for you and that's the main difficulties that make us interesting.
Gravatar
# re: A little help from Reflection...
dissertation writing services
16/02/2017 08:00
Thanks for sharing post. It is really good one which provide enough information to explore about given topic. Good luck.
Gravatar
# re: A little help from Reflection...
wikipedia
23/03/2017 16:07
Gunung semeru merupakan gunung paling tinggi di tanah Jawa, dengan nama puncaknya yaitu Puncak Mahameru. Ketinggian puncak gunung semeru ini 3676 Mdpl. Gunung semeru ini merupakan gunung favorit bagi para petualan atau para pendaki yang tertantang dan terpacu adrenalinnya untuk menaklukkan puncak mahameru gunung semeru. Untuk mendaki ke puncak mahameru dibutuhkan waktu kurang lebih 4 hari perjalanan pulang pergi dengan jalan kaki. Anda akan menemukan kindahan alam yang sesungguhnya dan kebesaran Tuhan sangat nampak saat berada di atas puncak mahameru gunung semeru. Informasi estimasi waktu dan biaya mendaki ke puncak mahameru di gunung semeru baca artikel terkait.
Gravatar
# re: A little help from Reflection...
dfhmnbv
26/03/2017 14:05
نمک</br>
جوش شیرین</br>
آهک</br>
بنتونیت</br>
پرلیت</br>
کود گاوی</br>
هیدروکلریک اسید</br>
اسید بوریک</br>
اسید هیومیک</br>
اسید سیتریک</br>
آب ژاول</br>
مشاوره ازدواج</br>
مشاوره خانواده</br>
مشاوره روانشناسی</br>
دوربین مدار بسته</br>
سئو
Gravatar
# re: A little help from Reflection...
homedepot.com/survey
31/07/2018 01:13
NJMCDirect provides the NJ Ticket Prefix Code and you can use this code to pay your ticket fines online on its official site.
Gravatar
# re: A little help from Reflection...
Essay Writing Services
01/08/2018 11:29
Many thanks for provide here a little help from Reflection which Create a custom attribute.
Comments have been closed on this topic.
News

This is my personal blog.
These postings are provided "AS IS" with no warranties, and confer no rights.

Disciple