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. Regarding Reflections

Regarding Reflections

Scheduled Pinned Locked Moved C#
questiontutorial
5 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.
  • P Offline
    P Offline
    ParagGupta
    wrote on last edited by
    #1

    How to gate the values of some properties in the class Like I have a Class ... Class1 in which some properties are defined say FName, LName.. Now what I have to recieve is the values that are stores in these Properties and then write them in comma separated form in a text file How can I do this? Parag Gupta

    A E 2 Replies Last reply
    0
    • P ParagGupta

      How to gate the values of some properties in the class Like I have a Class ... Class1 in which some properties are defined say FName, LName.. Now what I have to recieve is the values that are stores in these Properties and then write them in comma separated form in a text file How can I do this? Parag Gupta

      A Offline
      A Offline
      Anthony Mushrow
      wrote on last edited by
      #2

      define these variables as public static string FName; inside the class, outside any functions. Then you should be able to just get the value from anywhere by going: Class1.FName Im not sure if this is a good way of doing it or whatever, but i do it all the time, and i've never had any problems :)

      1 Reply Last reply
      0
      • P ParagGupta

        How to gate the values of some properties in the class Like I have a Class ... Class1 in which some properties are defined say FName, LName.. Now what I have to recieve is the values that are stores in these Properties and then write them in comma separated form in a text file How can I do this? Parag Gupta

        E Offline
        E Offline
        Ed Poore
        wrote on last edited by
        #3

        If you want to use reflection (as by your title) then object value = instanceOfClass.GetType().GetProperty("FName").GetValue(instanceOfClass, null); That doesn't do anything different than object value = instanceOfClass.FName; However if you want a generic routine for saving all properties then you can do something like the following:

        foreach (PropertyInfo pi in instanceOfClass.GetType().GetProperties())
        {
            object value = pi.GetValue(instanceOfClass, null);
            // Do what you want with value - e.g. save it to a file
        }


        If you're stuck in a rut: 1) Consult the documentation* 2) Google it 3) Ask a sensible question 4) Try an ancient ritualistic knowledge summoning (:badger::badger::badger:) dance :jig: 5) Phone :bob: * - If the documentation is MSDN > 6.0 then forget it!

        P 1 Reply Last reply
        0
        • E Ed Poore

          If you want to use reflection (as by your title) then object value = instanceOfClass.GetType().GetProperty("FName").GetValue(instanceOfClass, null); That doesn't do anything different than object value = instanceOfClass.FName; However if you want a generic routine for saving all properties then you can do something like the following:

          foreach (PropertyInfo pi in instanceOfClass.GetType().GetProperties())
          {
              object value = pi.GetValue(instanceOfClass, null);
              // Do what you want with value - e.g. save it to a file
          }


          If you're stuck in a rut: 1) Consult the documentation* 2) Google it 3) Ask a sensible question 4) Try an ancient ritualistic knowledge summoning (:badger::badger::badger:) dance :jig: 5) Phone :bob: * - If the documentation is MSDN > 6.0 then forget it!

          P Offline
          P Offline
          ParagGupta
          wrote on last edited by
          #4

          Sorry, but that is not what exactly I want What I want is that I want to apply this thing to many classes of which I don't know the properties that it contains as in.... in class1 it may be FName or LName but what is in class 2 I don't know I want to have some common code that will apply for all the classes I want. the problem with this one is that here I have to call the values of all the properties individually Please reply. Thanks in Advance

          E 1 Reply Last reply
          0
          • P ParagGupta

            Sorry, but that is not what exactly I want What I want is that I want to apply this thing to many classes of which I don't know the properties that it contains as in.... in class1 it may be FName or LName but what is in class 2 I don't know I want to have some common code that will apply for all the classes I want. the problem with this one is that here I have to call the values of all the properties individually Please reply. Thanks in Advance

            E Offline
            E Offline
            Ed Poore
            wrote on last edited by
            #5

            Simple sample method:

            public void SaveObject(object value)
            {
                PropertyInfo[] properties = value.GetType().GetProperties();
                foreach (PropertyInfo property in properties)
                {
                   SaveValue(propertyInfo.GetValue(value, null));
                }
            }

            Now you just call SaveObject with whatever object you want to save.  Note that SaveValue will have to inspect the object to see whether it's a primitive type or a complex (e.g. TextBox) and decide how to save it.  There is a property named IsPrimitive on the Type object I think which you can use to decide what to. do.


            If you're stuck in a rut: 1) Consult the documentation* 2) Google it 3) Ask a sensible question 4) Try an ancient ritualistic knowledge summoning (:badger::badger::badger:) dance :jig: 5) Phone :bob: * - If the documentation is MSDN > 6.0 then forget it!

            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