Results 1 to 5 of 5

Thread: [RESOLVED] Any change of getting an object property not yet created?

  1. #1

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Resolved [RESOLVED] Any change of getting an object property not yet created?

    I'm writing this here because the thread has not yet been moved from codebank as I notified.
    So that there in codebank should be deleted. I don't know if I can do that I'll try in a while...

    On VB you can do object.property1 = "x" on an object with unknown properties.
    I cant do that on C# as it complains that it's not contains a definition.
    So how can I go about?
    The object is returned from a json string that it then build it's properties at runtime.
    I believe I have done something similar in the past by using GetValue , so I crafted this:
    Code:
    public class propertygetter
    {
    public object this[string propertyName]
    {
    get { return this.GetType().GetProperty(propertyName).GetValue(this, null); }
    }

    }
    And call it:
    var x1 = mresult.JSonDeserializedObjectReturn.GetType().GetProperty("ErrorDescription");

    However I end up with another object containing thousand of properties that non reflect to the value. That is a simple null or somestring.

    So how can I simulate this vb call?
    mistaresult.JSonDeserializedObjectReturn.ErrorDescription ?
    Thanks.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  2. #2
    Fanatic Member
    Join Date
    Jun 2019
    Posts
    579

    Re: Any change of getting an object property not yet created?

    Quick hint: dynamic objects, expando object
    Link: https://learn.microsoft.com/en-us/do...ynamic-objects

  3. #3

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Any change of getting an object property not yet created?

    OMG!
    Nop, I'm not gonna write a dozen a pages of declarations to do what VB does in a fraction of a line.
    If it does it inside the gettype then fine, else, I'll let it go. Also the object is out of my reach if it needs to be created in a "specific" way.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Any change of getting an object property not yet created?

    That's late binding. The dynamic type was added to C# some time ago to support basically the same thing.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Any change of getting an object property not yet created?

    Perfect!
    Thanks.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width