Uncommon ways of object access

  • Thread starter Thread starter Peter Duniho
  • Start date Start date
P

Peter Duniho

[...]
foreach($myObject as $oField => $oValue)
{
printf("%s = %s\n", $oField, $oValue);
/* That gives me an output of MyObject like:
myObjVar = 123
anotherVar = hello world!
*/
}

Is such loop possible in C#, anyhow? (Loop, or rather an object access
mode)

The second question is, directly connected with first one: Is it
possible to access an C# Object such way (PHP sample):

$varName = "myObjVar";

echo $myObject->{$varName};
// That will print out "123"

Anyway to do it in C#?

Both of your questions can be answered in C# using reflection. You'll
want to look at things like the Type class, Type.GetFields() or
Type.GetProperties() (depending on what exactly you're trying to
extract from the type), and the FieldInfo and/or PropertyInfo classes
(again, depending).

Pete
 
Dear Group,

I'm curious, whether is possible such trick in C#:

Basically speaking, in PHP what I do, is I assign an object variables
using simple loop:

class MyObject {
private $myObjVar;
private $anotherVar;
public function MyObject($a, $b) { $this->myObjVar = $a; $this-
anotherVar = $b; };
}

$myObject = new MyObject(123, "hello world!");

foreach($myObject as $oField => $oValue)
{
printf("%s = %s\n", $oField, $oValue);
/* That gives me an output of MyObject like:
myObjVar = 123
anotherVar = hello world!
*/
}

Is such loop possible in C#, anyhow? (Loop, or rather an object access
mode)

The second question is, directly connected with first one: Is it
possible to access an C# Object such way (PHP sample):

$varName = "myObjVar";

echo $myObject->{$varName};
// That will print out "123"

Anyway to do it in C#?

Thank you in advanced for any suggestions.

All the best,
Przemek M. Zawada
 
[...]
foreach($myObject as $oField => $oValue)
{
printf("%s = %s\n", $oField, $oValue);
/* That gives me an output of MyObject like:
myObjVar = 123
anotherVar = hello world!
*/
}
Is such loop possible in C#, anyhow? (Loop, or rather an object access
mode)
The second question is, directly connected with first one: Is it
possible to access an C# Object such way (PHP sample):
$varName = "myObjVar";
echo $myObject->{$varName};
// That will print out "123"
Anyway to do it in C#?

Both of your questions can be answered in C# using reflection. You'll
want to look at things like the Type class, Type.GetFields() or
Type.GetProperties() (depending on what exactly you're trying to
extract from the type), and the FieldInfo and/or PropertyInfo classes
(again, depending).

Pete

Thank you Pete for such fast answer.
That's correct, I receive through GetProperties() PropertyInfo object,
which gives me object details. Another case selecting data from object
I've fetched properties?

CustomerObject c = new CustomerObject();
foreach(PropertyInfo pi in c.GetType().GetProperties())
{
string varName = pi.Name;
c.{varName} = "my newValue"; // <<<
Console.WriteLine(c.{varName}.ToString()); // <<<
}

How shall I write those '<<<' lines correctly? If it's possible of
course..

All the best,
Przemek
 
Thank you Pete for such fast answer.
That's correct, I receive through GetProperties() PropertyInfo object,
which gives me object details. Another case selecting data from object
I've fetched properties?

CustomerObject c = new CustomerObject();
foreach(PropertyInfo pi in c.GetType().GetProperties())
{
string varName = pi.Name;
c.{varName} = "my newValue"; // <<<
Console.WriteLine(c.{varName}.ToString()); // <<<

}

How shall I write those '<<<' lines correctly? If it's possible of
course..

pi.SetValue(c, "my newValue", null);
Console.WriteLine (pi.GetValue(c, null));

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top