【问题标题】:Getting object name from list [duplicate]从列表中获取对象名称[重复]
【发布时间】:2013-03-16 19:16:37
【问题描述】:

有没有办法检索存储在列表中的对象的名称?

我想要做的是在打印出该矩阵的属性之前添加一个对象名称 - 在这种特殊情况下是矩阵的名称。

internal class Program
{
    private static void Main(string[] args)
    {
        //Create a collection to help iterate later
        List<Matrix> list_matrix = new List<Matrix>();

        //Test if all overloads work as they should.
        Matrix mat01 = new Matrix();
        list_matrix.Add(mat01);

        Matrix mat02 = new Matrix(3);
        list_matrix.Add(mat02);

        Matrix mat03 = new Matrix(2, 3);
        list_matrix.Add(mat03);

        Matrix mat04 = new Matrix(new[,] { { 1, 1, 3, }, { 4, 5, 6 } });
        list_matrix.Add(mat04);

        //Test if all methods work as they should.     
        foreach (Matrix mat in list_matrix) 
        {
            //Invoking counter of rows & columns
            //HERE IS what I need - instead of XXXX there should be mat01, mat02...
            Console.WriteLine("Matrix XXXX has {0} Rows and {1} Columns", mat.countRows(), mat.countColumns()); 
        }
    }
}

总之我需要这里

Console.WriteLine("Matrix XXXX has {0} Rows and {1} Columns",
                  mat.countRows(),
                  mat.countColumns());

一种写出特定对象名称的方法 - 矩阵。

【问题讨论】:

  • 为什么不直接创建一个字符串名称属性并给它一个有意义的名称而不是 mat01、mat02 等?
  • 感谢您的评论,但我不认为这是您暗示的问题的重复。从我的角度来看,变量名和对象名是不一样的,我不确定他们的方法是否可以应用于我的问题。
  • 嗯,您可能想尝试一下链接。好像有办法。我会尝试一下(宝宝需要奶瓶:))

标签: c# list object identifier


【解决方案1】:

变量名作为属性

您无法检索“曾经用于”声明矩阵的对象引用名称。我能想到的最佳选择是将字符串属性 Name 添加到 Matrix 并将其设置为适当的值。

    Matrix mat01 = new Matrix();
    mat01.Name = "mat01";
    list_matrix.Add(mat01);

    Matrix mat02 = new Matrix(3);
    mat02.Name = "mat02";
    list_matrix.Add(mat02);

这样你就可以输出矩阵的名称

foreach (Matrix mat in list_matrix)
{
    Console.WriteLine("Matrix {0} has {1} Rows and {2} Columns", 
        mat.Name, 
        mat.countRows(), 
        mat.countColumns());
}

使用 Lambda 表达式的替代方法

正如 Bryan Crosby 所提到的,有一种方法可以使用 lambda 表达式在代码中获取变量名称,explained in this post。这是一个小型单元测试,展示了如何在代码中应用它。

    [Test]
    public void CreateMatrix()
    {
        var matrixVariableName = new Matrix(new [,] {{1, 2, 3,}, {1, 2, 3}});
        Assert.AreEqual("matrixVariableName", GetVariableName(() => matrixVariableName));
    }

    static string GetVariableName<T>(Expression<Func<T>> expr)
    {
        var body = (MemberExpression)expr.Body;

        return body.Member.Name;
    }

PS:请注意他对性能惩罚的警告。

【讨论】:

  • 谢谢,看来这是我这样做的唯一正确方法。
  • @nenad 我更新了答案,还有另一种使用 lambda 表达式的方法。奇迹般有效。很酷:)。
  • 我明天试试,现在累了。但是我不明白这句话“PS:不要对他的性能惩罚发出警告。”
  • @nenad 错字:) 不是 = 注意。如果性能非常重要(根据他的解释),则不应使用这种结构
  • 另外,如果您对创建新类犹豫不决,您可以使用匿名类型。 msdn.microsoft.com/en-us/library/vstudio/bb397696.aspx
【解决方案2】:
int i=0;
foreach (Matrix mat in list_matrix) 
{
 i++;
Console.WriteLine("Matrix mat{0} has {1} Rows and {2} Columns", i, mat.countRows(), mat.countColumns()); 
 }

【讨论】:

  • Brilliant :) 如此简单,但我并没有想到。但是,它仅适用于矩阵名称按数字顺序排列的情况。
猜你喜欢
  • 1970-01-01
  • 2018-12-13
  • 2017-05-21
  • 2017-04-06
  • 1970-01-01
  • 2019-07-30
  • 2020-11-23
  • 2015-08-07
  • 2012-02-21
相关资源
最近更新 更多