我想写一个通用的扩展方法,将一个固定矩阵添加到“弹性”矩阵。扩展方法编译和(我认为)它的代码在常规方法中工作正常。知道我将使用这个功能很多各种类型,我更希望算出这个问题出来了,而不是用创可贴一起一瘸一拐:C#通用扩展方法编译但不可用?
public void AddMatrix<T>(this List<T[]> MyList, T[,] Matrix)
{
if (MyList == null) throw new ArgumentNullException("MyList");
if (Matrix == null) throw new ArgumentNullException("Matrix");
for (int i = 0; i < Matrix.GetLength(0); i++)
{
T[] aLine = new T[Matrix.GetLength(1)];
for (int j = 0; j < Matrix.GetLength(1); j++)
aLine[j] = Matrix[i, j];
MyList.Add(aLine);
}
}
public void testAddMatrix()
{
List<string[]> aMyBigMatrix = new List<string[]>();
string[,] aSmallerMatrix =
{
{
"foo",
"bar",
"what"
}
};
aMyBigMatrix.AddMatrix(aSmallerMatrix); // .AddMatrix is not showing up here in Intellisense?
}
当你去使用它时会发生什么? – 2012-04-13 18:22:15