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. dynamic acces to class variables?

dynamic acces to class variables?

Scheduled Pinned Locked Moved C#
question
4 Posts 3 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.
  • T Offline
    T Offline
    TyronX
    wrote on last edited by
    #1

    Is there any possiblities to access all of the variables from a class dynamically (something like MyFriends.GetVariable("Name"); )? fsadfasdfsadfasdfasdf

    T H 2 Replies Last reply
    0
    • T TyronX

      Is there any possiblities to access all of the variables from a class dynamically (something like MyFriends.GetVariable("Name"); )? fsadfasdfsadfasdfasdf

      T Offline
      T Offline
      TyronX
      wrote on last edited by
      #2

      And is there some Information available for Regexps in C#? I wasn't able to extract strings out of an other string with Regex.Match (e.g. /^([^=]+)=(.+)$/ - should extract key and value of a "key=value" pair...) (I just edited my account options)

      D 1 Reply Last reply
      0
      • T TyronX

        Is there any possiblities to access all of the variables from a class dynamically (something like MyFriends.GetVariable("Name"); )? fsadfasdfsadfasdfasdf

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

        You can use reflection to enumerate types and their members (properties, fields, and methods). Read Discovering Type Information at Runtime[^] in the .NET Framework SDK. Note that these aren't "variables". Variables - in practically any language - are temporary memory addresses to store information in a particular function or method (methods are functions declared for a class or other structure). You can also have global variables. To enumerate properties on a class, for example (since you typically shouldn't expose fields publicly; you have little control over what gets assigned and can't validate the data):

        using System;
        using System.Reflection;
         
        class Person
        {
        static void Main()
        {
        Person p = new Person("Heath", DateTime.Parse("08/07/1978"));
        Reflect(p);
        }
         
        static void Reflect(Person p)
        {
        PropertyInfo[] props = p.GetType().GetProperties(
        BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
        if (props != null)
        {
        foreach (PropertyInfo prop in props)
        {
        object value = prop.GetValue(p, null);
        Console.WriteLine("{0} ({1}) = {2}", prop.Name, prop.PropertyType,
        value);
        }
        }
        }
         
        Person(string name, DateTime birthday)
        {
        this.name = name;
        this.birthday = birthday;
        }
         
        string name;
        DateTime birthday;
         
        public string Name
        {
        get { return name; }
        set
        {
        if (value == null) throw new ArgumentNullException();
        name = value;
        }
        }
         
        public DateTime Birthday
        {
        get { return birthday; }
        set { birthday = value; }
        }
        }

        This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

        1 Reply Last reply
        0
        • T TyronX

          And is there some Information available for Regexps in C#? I wasn't able to extract strings out of an other string with Regex.Match (e.g. /^([^=]+)=(.+)$/ - should extract key and value of a "key=value" pair...) (I just edited my account options)

          D Offline
          D Offline
          DavidNohejl
          wrote on last edited by
          #4

          Here on MSDN[^] are some information about regular expressions. As for your example, I would use stg like (?<key>.*?)=(?<value>.*) TyronX wrote: (I just edited my account options) :) best regards, David 'DNH' Nohejl Never forget: "Stay kul and happy" (I.A.)

          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