Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Getting property value using reflection

Getting property value using reflection

Scheduled Pinned Locked Moved C#
question
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    Uncle Monkey
    wrote on last edited by
    #1

    I am trying to retrieve a property from the current executing assembly using the following but am getting a TargetException int MyInt = Convert.ToInt32(Type.GetType(Page.GetType().BaseType.FullName).GetProperty("MyIntProperty").GetValue(null,null)); Anybody know what I'm doing wrong? Thanks monkey say, monkey doo Uncle Monkey

    H 1 Reply Last reply
    0
    • U Uncle Monkey

      I am trying to retrieve a property from the current executing assembly using the following but am getting a TargetException int MyInt = Convert.ToInt32(Type.GetType(Page.GetType().BaseType.FullName).GetProperty("MyIntProperty").GetValue(null,null)); Anybody know what I'm doing wrong? Thanks monkey say, monkey doo Uncle Monkey

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Wow! You really should split your lines up into distinct pieces - it greatly helps during debugging your application. You're doing a few things that aren't necessary and would hamper performance, like Type.GetType(Page.GetType().BaseType.FullName). Instead, you already have your Type using just Page.GetType().BaseType. Also, when you call GetValue you must pass the instance of the object (presumably Page) otherwise you would get an exception since you're trying to retrieve the value of a null reference. Also, if you flatten your instance members, you really don't need to refer to the base Type to get the property where it's declared. Unless BindingFlags.DeclaredOnly is specified, all instance properties - both declared and inheritted - are reflected (not so with statics, unless you specify BindingFlags.FlattenHierarchy). In ASP.NET, which I assume you're using since you have a Page object - though this could be anything - there is actually a large hierarchy of pages that extend from the Page class, such as your .aspx file : your code-behind page class : Page.

      Type t = Page.GetType();
      PropertyInfo prop = t.GetProperty("MyIntProperty", BindingFlags.Instance |
      BindingFlags.Public | BindingFlags.NonPublic);
      int MyInt;
      if (prop != null) MyInt = (int)prop.GetValue(Page, null);

      Microsoft MVP, Visual C# My Articles

      U 1 Reply Last reply
      0
      • H Heath Stewart

        Wow! You really should split your lines up into distinct pieces - it greatly helps during debugging your application. You're doing a few things that aren't necessary and would hamper performance, like Type.GetType(Page.GetType().BaseType.FullName). Instead, you already have your Type using just Page.GetType().BaseType. Also, when you call GetValue you must pass the instance of the object (presumably Page) otherwise you would get an exception since you're trying to retrieve the value of a null reference. Also, if you flatten your instance members, you really don't need to refer to the base Type to get the property where it's declared. Unless BindingFlags.DeclaredOnly is specified, all instance properties - both declared and inheritted - are reflected (not so with statics, unless you specify BindingFlags.FlattenHierarchy). In ASP.NET, which I assume you're using since you have a Page object - though this could be anything - there is actually a large hierarchy of pages that extend from the Page class, such as your .aspx file : your code-behind page class : Page.

        Type t = Page.GetType();
        PropertyInfo prop = t.GetProperty("MyIntProperty", BindingFlags.Instance |
        BindingFlags.Public | BindingFlags.NonPublic);
        int MyInt;
        if (prop != null) MyInt = (int)prop.GetValue(Page, null);

        Microsoft MVP, Visual C# My Articles

        U Offline
        U Offline
        Uncle Monkey
        wrote on last edited by
        #3

        Thanks, worked a treat monkey say, monkey doo Uncle Monkey

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups