Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / November 2007

Tip: Looking for answers? Try searching our database.

Get Const Name and Values with Reflection?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
coconet - 30 Nov 2007 20:03 GMT
I am trying to enumerate all of the name/value info from a struct with
Reflection. It looks like this:

public struct MyStruct
{
    public const string ValueOne = "something";
    public const string ValueTwo = "somethingelse";
}

In another class, I would like to enumerate it into MemberInfo[] so I
can use the parts. How to do that in C# 2.0?

Thanks.
Morten Wennevik [C# MVP] - 30 Nov 2007 20:44 GMT
Hi Coconet

MemberInfo won't tell you much other than what kind of members are available.  You would then need to obtain a MethodInfo, PropertyInfo or FieldInfo to get the actual values.  In your case this code piece should list the string

            MyStruct f = new MyStruct();
            Type t = f.GetType();
            FieldInfo[] fields = f.GetType().GetFields();

            List<string> values = new List<string>();
            foreach (FieldInfo fi in fields)
                values.Add(fi.GetValue(fi).ToString());

> I am trying to enumerate all of the name/value info from a struct with
> Reflection. It looks like this:
[quoted text clipped - 9 lines]
>
> Thanks.

Signature

Happy coding!
Morten Wennevik [C# MVP]

coconet - 30 Nov 2007 21:31 GMT
That is gold!

Thanks.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.