|
-
Jul 4th, 2023, 02:01 AM
#1
[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.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jul 4th, 2023, 02:03 AM
#2
Re: Any change of getting an object property not yet created?
-
Jul 4th, 2023, 03:39 AM
#3
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.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jul 4th, 2023, 04:22 AM
#4
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.
-
Jul 4th, 2023, 04:51 AM
#5
Re: Any change of getting an object property not yet created?
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|