C# 从列表中获取对象名称
是否有方法检索列表中存储的对象的名称 我想做的是在打印出矩阵的属性之前,添加一个对象名——在这个特殊情况下是矩阵的名称C# 从列表中获取对象名称,c#,list,object,identifier,C#,List,Object,Identifier,是否有方法检索列表中存储的对象的名称 我想做的是在打印出矩阵的属性之前,添加一个对象名——在这个特殊情况下是矩阵的名称 internal class Program { private static void Main(string[] args) { //Create a collection to help iterate later List<Matrix> list_matrix = new List<Matrix>
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());
}
}
}
写出特定对象-矩阵的名称的方法。变量名作为属性
无法检索“曾经用于”声明矩阵的对象引用名称。我能想到的最佳选择是向矩阵中添加字符串属性Name
,并将其设置为适当的值
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表达式在代码中获取变量名。这里是一个小单元测试,它展示了如何在代码中应用它
[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;
}
[测试]
public void CreateMatrix()
{
var-matrixVariableName=新矩阵(新[,]{{1,2,3,},{1,2,3}});
AreEqual(“matrixVariableName”,GetVariableName(()=>matrixVariableName));
}
静态字符串GetVariableName(表达式expr)
{
var body=(MemberExpression)expr.body;
返回body.Member.Name;
}
注意:注意他关于性能惩罚的警告。变量名作为属性
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());
}
无法检索“曾经用于”声明矩阵的对象引用名称。我能想到的最佳选择是向矩阵中添加字符串属性Name
,并将其设置为适当的值
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表达式在代码中获取变量名。这里是一个小单元测试,它展示了如何在代码中应用它
[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;
}
[测试]
public void CreateMatrix()
{
var-matrixVariableName=新矩阵(新[,]{{1,2,3,},{1,2,3}});
AreEqual(“matrixVariableName”,GetVariableName(()=>matrixVariableName));
}
静态字符串GetVariableName(表达式expr)
{
var body=(MemberExpression)expr.body;
返回body.Member.Name;
}
PS:请注意他关于性能惩罚的警告。为什么不创建一个字符串名称属性,并为其指定一个有意义的名称,而不是mat01、mat02等?感谢您的评论,但我不认为这与您暗示的问题重复。从我的观点来看,获取变量名和对象名是不一样的,而且我不确定他们的方法是否可以应用于我的问题。嗯,你可能想。尝试一下链接。似乎是一种方式。我试一下(baby需要瓶子:))为什么不创建一个string name属性并给它一个有意义的名称,而不是mat01、mat02等?谢谢你的评论,但我不认为这是你暗示的问题的重复。从我的观点来看,获取变量名和对象名是不一样的,而且我不确定他们的方法是否可以应用于我的问题。嗯,你可能想。尝试一下链接。似乎是一种方式。我试一下(宝宝需要奶瓶:)太棒了:)这么简单,但我没想到。然而,它只在矩阵名称按数字顺序排列的情况下起作用。精彩:)如此简单,但我没有想到。不过,它只在矩阵名称按数字顺序排列的情况下才有效。谢谢,因为这似乎是我这样做的唯一正确方法。@nenad我更新了答案,还有另一种使用lambda表达式的方法。工作起来很有魅力。很酷:)。我明天就试试,现在累了。但我不明白这句话“PS:不要警告他关于表现的处罚。”@nenad typo:)not=note。如果性能非常重要(根据他的解释),则不应使用这种构造。如果您对创建新类犹豫不决,也可以使用匿名类型。谢谢,因为这似乎是我这样做的唯一正确方法。@nenad我更新了答案,还有另一种使用lambda表达式的方法。工作起来很有魅力。很酷:)。我明天就试试,现在累了。但我不明白这句话“PS:不要警告他关于表现的处罚。”@nenad typo:)not=note。如果性能非常重要(根据他的解释),则不应使用这种构造。如果您对创建新类犹豫不决,也可以使用匿名类型。
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());
}