dynamically created object properties

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Neil Chambers

    dynamically created object properties

    I would like to pass an array of strings to an object and have the object
    members built using the array items as property names. How would I go about
    doing this?

    Example:

    public class myDynamicObject
    {
    public myDynamicObject (string[] memberNames)
    {
    foreach (string s in memberNames)
    {
    //make s a public property of the object
    //assign a value to s
    }
    }
    }

    Many thanks!
    neil

  • Jon Skeet [C# MVP]

    #2
    Re: dynamically created object properties

    On Sep 25, 2:59 pm, "Neil Chambers" <nos...@nospam. comwrote:
    I would like to pass an array of strings to an object and have the object
    members built using the array items as property names. How would I go about
    doing this?
    You can't - types don't change on the fly; you can't add a property to
    an object.

    What's the use case here? Is there anything that a simple
    Dictionary<stri ng, objectwouldn't solve?

    Jon

    Comment

    • =?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?=

      #3
      RE: dynamically created object properties

      This isnt a simple thing to do in C# as it isnt a dynamic language. You would
      need to use the classes in the Reflection.Emit language to build the class.
      This is complex stuff and I havent found a need to ever do it.
      Google Reflection.Emit and you should come up with some answers
      If you want, let us know what you are trying to acheive in terms of the big
      picture as there might be and easier way

      --
      Ciaran O''Donnell
      try{ Life(); } catch (TooDifficultException) { throw Toys(); }



      "Neil Chambers" wrote:
      I would like to pass an array of strings to an object and have the object
      members built using the array items as property names. How would I go about
      doing this?
      >
      Example:
      >
      public class myDynamicObject
      {
      public myDynamicObject (string[] memberNames)
      {
      foreach (string s in memberNames)
      {
      //make s a public property of the object
      //assign a value to s
      }
      }
      }
      >
      Many thanks!
      neil
      >
      >

      Comment

      • Marc Gravell

        #4
        Re: dynamically created object properties


        Can you clarify what you want to do?

        If you want to assign values to already-existing properties, then
        reflection is your friend; or System.Componen tModel:

        TypeDescriptor. GetProperties(o bj)["PropName"].SetValue(obj, newValue);

        is broadly equivalent to:
        obj.PropName = newValue;

        If the properties don't already exist and you want runtime-only
        properties, then you are into a more complex area of custom type
        descriptors... it can work, but is mainly useful for data-binding to UIs
        etc. I can provide more info, but I'd prefer to know that this is what
        you meant, first...

        Marc

        Comment

        • Ignacio Machin ( .NET/ C# MVP )

          #5
          Re: dynamically created object properties

          On Sep 25, 9:59 am, "Neil Chambers" <nos...@nospam. comwrote:
          I would like to pass an array of strings to an object and have the object
          members built using the array items as property names. How would I go about
          doing this?
          >
          Example:
          >
          public class myDynamicObject
          {
          public myDynamicObject (string[] memberNames)
          {
          foreach (string s in memberNames)
          {
          //make s a public property of the object
          //assign a value to s
          }
          }
          >
          }
          >
          Many thanks!
          neil
          You cannot.
          What you can do is having one property or one method taht return the
          value you want.
          you could have
          string GetPropertyValu e(string propertyName)
          or if you do not mind using an indexer you can have:
          string this[string propertyName]
          n this case you access it by using
          myInstance["propertyNa me"]

          You can hold your property/values pairs in a dictionary.



          Comment

          Working...