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 / August 2004

Tip: Looking for answers? Try searching our database.

Reflect a constant?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jon Davis - 03 Aug 2004 17:45 GMT
I'm trying to use myclass.GetType().GetField("myconst") to get the constant
of an object, but it doesn't work (returns null). How do I retrieve a
constant field's value of a particular class?

Thanks,
Jon
Jon Davis - 03 Aug 2004 17:51 GMT
BTW my solution requires late binding as such, so don't ask me why I don't
use myclass.myconst.

Jon

> I'm trying to use myclass.GetType().GetField("myconst") to get the constant
> of an object, but it doesn't work (returns null). How do I retrieve a
> constant field's value of a particular class?
>
> Thanks,
> Jon
Jon Skeet [C# MVP] - 03 Aug 2004 18:28 GMT
> I'm trying to use myclass.GetType().GetField("myconst") to get the constant
> of an object, but it doesn't work (returns null). How do I retrieve a
> constant field's value of a particular class?

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

This works, for instance:

using System;

class Test
{
   public const int x=5;
   
   static void Main()
   {
       Console.WriteLine(typeof(Test).GetField("x").GetValue(null));
   }
}

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jon Davis - 03 Aug 2004 19:56 GMT
public class BasePage : System.Web.UI.Page
{
 public string SuperEval() {
  string ret = "Failure";
  System.Reflection.FieldInfo fi = this.GetType().GetField("SuperString");
  if (fi != null) ret = (string)fi.GetValue(null);
  return ret;
 }
}
public class WebForm1 : BasePage
{
 private void Page_Load(object sender, System.EventArgs e)
 {
  Response.Write(SuperEval());
  Response.End();
 }
 public const string SuperString = "Success";
}

> > I'm trying to use myclass.GetType().GetField("myconst") to get the constant
> > of an object, but it doesn't work (returns null). How do I retrieve a
[quoted text clipped - 19 lines]
>     }
> }
Jon Skeet [C# MVP] - 03 Aug 2004 20:09 GMT
>  public class BasePage : System.Web.UI.Page
>  {
[quoted text clipped - 14 lines]
>   public const string SuperString = "Success";
>  }

It would help if you'd use a console app as an example in future - it
makes it much easier to test. Here's your example re-expressed and
fixed up:

using System;
using System.Reflection;

class Base
{
   public const string SuperString = "Success";
}

class Test : Base
{
   public const int x=5;
   
   static void Main()
   {
       Type t = typeof(Test);
       FieldInfo info = t.GetField ("SuperString",
                                    BindingFlags.Public |
                                    BindingFlags.Static |
                                    BindingFlags.FlattenHierarchy);
       Console.WriteLine (info.GetValue(null));
   }
}

You need to specify the binding flags to include FlattenHierarchy -
static members (such as constants) aren't really inherited as such,
they're just available via subtypes. FlattenHierarchy means that static
members up the type hierarchy are returned as well.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jon Davis - 03 Aug 2004 20:18 GMT
I used the web app demo because my console app demo was working and my
actual web app wasn't.

Thanks for the clarification on using BindingFlags.

Jon

> >  public class BasePage : System.Web.UI.Page
> >  {
[quoted text clipped - 46 lines]
> they're just available via subtypes. FlattenHierarchy means that static
> members up the type hierarchy are returned as well.

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.