sameerm's WebLog

  • Dumping objects fields and methods to console

    The classes in the Reflection namespace give you the ability to view a class(objects) fields and methods. Overriding the ToString method provides a quick way to spew the objects current state to the console. You don't need to write code to access each field individually. This is particularly helpful if the class contains a lot of mebers.

    The UsingGetValue method below provides some interesting insight on how FieldInfo::GetValue method works. The parameter to get value is the object instance for which to get the value. The call to get value in the code below is  myClass1.UsingGetValue(myClass2);

    // This will  Print 10, World and not 5, Hello
    // Although we are calling this on myClass1 instance the method implementation calls GetVaue as
    // val = f.GetValue(target).ToString();
    //The target in the call below is  myClass2
    //myClass1.UsingGetValue(myClass2);
    // As a result the members of target i.e. myClass2 will be returned and not myClass1.

     

    #region Using directives

    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.Reflection;

    #endregion

    namespace Main

    {

    class MyClass

    {

    public int someInteger = 10; //Some fields

    public string someMsg; // Some method.

    public MyClass(int i, string str)

    {

    this.someInteger = i;

    this.someMsg = str;

    }

    static void Main(string[] args)

    {

    MyClass myClass1 = new MyClass(5, "Hello");

    myClass1.ToString();

     

    Console.WriteLine();

    Console.WriteLine();

    // The code below show how the GeValue method works.

    MyClass myClass2 = new MyClass(10, "World");

    myClass2 with member values 10 and World.

    Console.WriteLine("Calling myClass1.UsingGetValue(myClass2);");

    // This will  Print 10, World and not 5, Hello
    // Although we are calling this on myClass1 instance the method implementation calls GetVaue as
    // val = f.GetValue(target).ToString();
    //The target in the call below is
    myClass1.UsingGetValue(myClass2);

    Console.WriteLine("Press any key ...");

    Console.Read();

    }

    // Override to string to dump the objects field and method before calling the default base class implementation

    public override string ToString()

    {

    string val;

    Type t = typeof(MyClass); // Get the type of the class we are interested..in this case ourself

    // Specifies flags that control binding and the way in which the search for members and types is conducted by reflection.

    BindingFlags bf = BindingFlags.DeclaredOnly | BindingFlags.Public |

    BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Instance;

    Console.WriteLine("Filed Info:");

    FieldInfo[] fields = t.GetFields(bf); // Get the fileds into an array.

    foreach (FieldInfo f in fields)

    {

    Console.Write("\nField Name: " + f.Name + " Value: ");

    try

    {

    // The parameter to get value is the object instance for which to get the value

    val = f.GetValue(this).ToString();

    System.Console.Write(val);

    }

    catch (FieldAccessException excp)

    {

    Console.WriteLine("FieldAccessException : " + excp.Message);

    }

    catch (TargetException excp)

    {

    Console.WriteLine("TargetException : " + excp.Message);

    }

    catch (ExecutionEngineException excp)

    {

    Console.WriteLine("ExecutionEngineException : " + excp.Message);

    }

    catch (MemberAccessException excp)

    {

    Console.WriteLine("MemberAccessException : " + excp.Message);

    }

    catch (Exception excp)

    {

    Console.WriteLine("Exception : " + excp.Message);

    }

    }

    Console.WriteLine("");

    Console.WriteLine("");

    Console.WriteLine("Method Info:");

    MethodInfo[] methodInfo = t.GetMethods(bf);

    foreach (MethodInfo mi in methodInfo)

    {

    Console.Write("\nField Name: " + mi.Name + " Value: ");

    try

    {

    val = mi.Name;

    System.Console.Write(val);

    }

    catch (MethodAccessException excp)

    {

    Console.WriteLine("MethodAccessException : " + excp.Message);

    }

    catch (TargetException excp)

    {

    Console.WriteLine("TargetException : " + excp.Message);

    }

    catch (ExecutionEngineException excp)

    {

    Console.WriteLine("ExecutionEngineException : " + excp.Message);

    }

    catch (MemberAccessException excp)

    {

    Console.WriteLine("MemberAccessException : " + excp.Message);

    }

    catch (Exception excp)

    {

    Console.WriteLine("Exception : " + excp.Message);

    }

    }

    return base.ToString();

    }

     

    public void UsingGetValue(MyClass target)

    {

    string val;

    Type t = typeof(MyClass); // Get the type of the class we are interested..in this case ourself

    // Specifies flags that control binding and the way in which the search for members and types is conducted by reflection.

    BindingFlags bf = BindingFlags.DeclaredOnly | BindingFlags.Public |

    BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Instance;

    Console.WriteLine("Filed Info:");

    FieldInfo[] fields = t.GetFields(bf); // Get the fileds into an array.

    foreach (FieldInfo f in fields)

    {

    Console.Write("\nField Name: " + f.Name + " Value: ");

    try

    {

    // The parameter to get value is the object instance for which to get the value

    val = f.GetValue(target).ToString();

    System.Console.Write(val);

    }

    catch (FieldAccessException excp)

    {

    Console.WriteLine("FieldAccessException : " + excp.Message);

    }

    catch (TargetException excp)

    {

    Console.WriteLine("TargetException : " + excp.Message);

    }

    catch (ExecutionEngineException excp)

    {

    Console.WriteLine("ExecutionEngineException : " + excp.Message);

    }

    catch (MemberAccessException excp)

    {

    Console.WriteLine("MemberAccessException : " + excp.Message);

    }

    catch (Exception excp)

    {

    Console.WriteLine("Exception : " + excp.Message);

    }

    }

    }

    }

    }

  • Connecting to Console session on a Remote server.

    When you use Remote Desktop Connetion (Start->Programs->Accessories->Communications->Remote Desktop Connection) to remote into an XP machine you get connected to the console session. I could not find a way to connect to the console session of a Win2K3 server using the Remote Desktop Connection UI Options button.

    The trick is to use the command line with the /console argument. 

    mstsc /v:servername /console

     

    Here is the help from MSDN.

     

    Mstsc

    Creates connections to terminal servers or other remote computers, edits an existing Remote Desktop Connection (.rdp) configuration file, and migrates legacy connection files that were created with Client Connection Manager to new .rdp connection files.

    Syntax

    mstsc.exe {ConnectionFile | /v:ServerName[:Port]} [/console] [/f] [/w:Width /h:Height]

    mstsc.exe /edit"ConnectionFile"

    mstsc.exe /migrate

    Parameters

    ConnectionFile
    Specifies the name of an .rdp file for the connection.
    /v:ServerName[:Port]
    Specifies the remote computer and, optionally, the port number to which you want to connect.
    /console
    Connects to the console session of the specified Windows Server 2003 family operating system.
    /f
    Starts Remote Desktop connection in full-screen mode.
    /w:Width /h:Height
    Specifies the dimensions of the Remote Desktop screen.
    /edit"ConnectionFile"
    Opens the specified .rdp file for editing.
    /migrate
    Migrates legacy connection files that were created with Client Connection Manager to new .rdp connection files.

    Remarks

    • You must be an administrator on the server to which you are connecting to create a remote console connection.
    • default.rdp is stored for each user as a hidden file in My Documents. User created .rdp files are stored by default in My Documents but can be moved anywhere.

    Examples

    To connect to the console session of a server, type:

    mstsc /console

    To open a file called filename.rdp for editing, type:

    mstsc /edit filename.rdp

    Formatting legend

    Format Meaning
    Italic Information that the user must supply
    Bold Elements that the user must type exactly as shown
    Ellipsis (...) Parameter that can be repeated several times in a command line
    Between brackets ([]) Optional items
    Between braces ({}); choices separated by pipe (|). Example: {even|odd} Set of choices from which the user must choose only one
    Courier font Code or program output

    Related Topics

     

This Blog

Syndication

Tags

No tags have been created or used yet.

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker