English 中文(简体)
C# Generic Expansionmeth Compiles But Unusable?
原标题:C# Generic Extension Method Compiles But Unusable?

我正试图撰写一种通用推广方法,在“弹性”矩阵中增加一个固定的矩阵。 推广方法汇编和(我假定)其法典以固定方法进行罚款。 了解情况 我很想把这一职能用于各种类型的工作,而不要与帮派援助相提并论:

    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?
    }
最佳回答
问题回答

你撰写了一种推广方法,AddMatrix<T>需要静态。

推广方法必须是静态的。

推广方法必须是静态的。

Change it to: public static void AddMatrix(this List MyList, T[,] Matrix)

并且确保这一类别也是静态的。

每个人都能迅速指出,推广方法必须固定不变。

当我试图重复你的错误时,我发现一个汇编错误,即“扩展方法必须是静态的”,因此,我感到奇怪的是,你报告说,你的法典汇编了你们的法典。 当你试图汇编该清单时,请看Error清单,看看看该清单是否确实是空的。 我怀疑你会发现你以前没有注意到的错误信息。 如果你能够看到汇编者错误,你这种性质的错误将很容易查明和确定。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签