Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

listing members of a class

VanilleBertVanilleBert Posts: 29Member
Hi,

I ask for a methode to listing members of a class. The names, types and values of a member of a class should be readable.

Is there any possibility to read this information at runtime and put it into a string?

Thx for response

Bert

PS:
Example:
[code]
class MyClass
{
public int Member1;
public OtherClass Member2;
public string Member3;

public MyClass();
public int Member4(int arg);
}
...
Console.WriteLine(ListMembersOf(MyClass));
[/code]
Wished Result:
[code]
int Member1 = 3
System.FavouriteNamespace.OtherClass Member2 = xyz
string Member3 = String of Member3
MyClass()
int Member4(int arg)
[/code]

Comments

  • iwilld0itiwilld0it Posts: 1,134Member
    Look into the Systsem.Reflection namespace.

    As a hint, all objects inherit from the base System.Object class which has a GetType() function.

    Type typ = myObj.GetType();

    This "Type" class has many functions for displaying the list of functions, properties, etc ... that an object supports.


    : Hi,
    :
    : I ask for a methode to listing members of a class. The names, types and values of a member of a class should be readable.
    :
    : Is there any possibility to read this information at runtime and put it into a string?
    :
    : Thx for response
    :
    : Bert
    :
    : PS:
    : Example:
    : [code]
    : class MyClass
    : {
    : public int Member1;
    : public OtherClass Member2;
    : public string Member3;
    :
    : public MyClass();
    : public int Member4(int arg);
    : }
    : ...
    : Console.WriteLine(ListMembersOf(MyClass));
    : [/code]
    : Wished Result:
    : [code]
    : int Member1 = 3
    : System.FavouriteNamespace.OtherClass Member2 = xyz
    : string Member3 = String of Member3
    : MyClass()
    : int Member4(int arg)
    : [/code]
    :

  • VanilleBertVanilleBert Posts: 29Member
    : Look into the System.Reflection namespace.
    :
    : As a hint, all objects inherit from the base System.Object class which has a GetType() function.
    :
    : Type typ = myObj.GetType();
    :
    : This "Type" class has many functions for displaying the list of functions, properties, etc ... that an object supports.

    Thx for that simple way.
Sign In or Register to comment.