1

XNA ゲームでデバッグ ウィンドウをプログラミングしています。それはただのStringBuilderものを出力するだけです。しかし、私はそれをもっとインタラクティブにしたいと思っています。たとえば、Playerクラス:

public class Player
{
   public Vector2 Position { get; set; }
   public int Health { get; set; }
}

呼び出すプロパティ (またはメソッド) のリストが必要です。次のようなものです:

list.Add(player1.Position);
list.Add(player1.Health);
list.Add(enemy2.Position);

したがって、更新のたびに、リストはこれらのプロパティを呼び出し、その値を出力します。これを行う方法はありますか?リフレクションを使用する必要がありますか? プロパティの代わりにメソッドを使用すると、大きく異なるでしょうか?

編集:これは私が現在持っているものですhttps://github.com/nanexcool/xna-debug-window/blob/master/DebugWindow.cs

オブジェクトとプロパティ名を文字列として指定することで機能しますが、おそらくもっと良い方法があります。

4

3 に答える 3

1

多分誰かがこれも便利だと思うでしょう:

/// <summary>
/// Represents your player class.
/// </summary>
class Player
{
    /// <summary>
    /// Gets or sets the health of the player.
    /// </summary>
    [DebugExtensions.DebugMePlease()]
    public int Health { get; set; }

    /// <summary>
    /// Gets or sets the name of the player.
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// Gets a string representation of the player object.
    /// </summary>
    /// <returns>The player object as string representation.</returns>
    public override string ToString()
    {
        return this.Name;
    }
}

/// <summary>
/// Contains some extensions useful for debugging.
/// </summary>
public static class DebugExtensions
{
    /// <summary>
    /// Represents our custom attribute called "DebugMePlease".
    /// Properties marked with this attribute will be printed automatically.
    /// </summary>
    public class DebugMePlease : Attribute
    {
    }

    /// <summary>
    /// Continas all objects that shall be monitored.
    /// </summary>
    public static List<object> DebugList
    {
        get;
        set;
    }

    /// <summary>
    /// Initializes static members of the <see cref="DebugExtensions"/> class.
    /// </summary>
    static DebugExtensions()
    {
        DebugList = new List<object>();
    }

    /// <summary>
    /// Prints the values of all objects in the debugList.
    /// </summary>
    public static void Print()
    {
        foreach (object o in DebugList)
        {
            var members = from member in o.GetType().GetProperties()
                          from attribute in member.GetCustomAttributes(typeof(DebugMePlease), true)
                          select member;

            foreach (var z in members)
            {
                Console.WriteLine(string.Format("{0}, {1}: {2}", o.ToString(), z.Name, z.GetValue(o)));
            }
        }
    }
}

/// <summary>
/// Contains the entry point of our application.
/// </summary>
public class Program
{
    /// <summary>
    /// The entry point of our application. 
    /// </summary>
    /// <param name="args">Possibly specified command line arguments.</param>
    public static void Main(string[] args)
    {
        Player t = new Player();
        t.Name = "Chuck Norris";
        t.Health = int.MaxValue; // Becaus it's Chuck Norris ;-)

        // Add the object to the debug list.
        DebugExtensions.DebugList.Add(t);

        // Print all properties marked with the "DebugMePlease" attribute.
        DebugExtensions.Print();

        // Change something.
        t.Health = 0;

        // Print again and hopefully be happy.
        DebugExtensions.Print();

        Console.ReadLine();
    }
}
于 2013-10-29T01:33:47.927 に答える
0

たぶん私は何かを誤解していますが、参照を含むリストが既にある場合は、.ToString() を使用してそれらを印刷することはできませんか? (もちろん、独自のクラスで ToString() をオーバーライドする必要があります。)

于 2013-10-28T22:38:09.583 に答える