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

使用属性名称和返回该属性的委托创建一个元组(或字典)列表:

using DebugTuple = Tuple<string, Func<string>>;

class Program
{  
    class Player
    {
        public int x;
        public Player(int y) { x = y; }            
    }

    static void Main(string[] args)
    {    
        Player one = new Player(25);
        Player two = new Player(50);

        List<DebugTuple> calls = new List<DebugTuple>();
        calls.Add(new DebugTuple("Player 1 health", delegate() { return one.x.ToString(); })); 
        calls.Add(new DebugTuple("Player 2 health", delegate() { return two.x.ToString(); })); 

        foreach (DebugTuple c in calls)
            Console.WriteLine(c.Item1 + ": " + c.Item2());

        //Change values and make sure it outputs new values
        one.x = 100;
        two.x = 0;

        foreach (DebugTuple c in calls)
            Console.WriteLine(c.Item1 + ": " + c.Item2());

        Console.ReadLine();

    }

}

输出:

Player 1 health: 25
Player 2 health: 50
Player 1 health: 100
Player 2 health: 0
于 2013-10-29T00:19:50.507 回答
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 回答