8

Title asks it all, actually, but still, for completeness sake:

Hi, I'm writing a small post-compiling tool in the .NET platform, and while trying to optimize it, I've encountered a question I can-not easily find an answer to from the ECMA standards for the Common Language Infrastructure (CLI).

What is the max # of methods a single class can have? Is there a limit?

Edit:

Thanks to Kelsey for pointing out to a real-life test. Although I still would care about what the actual limit is, for my actual real-life purposes, I wanted to know if it is 2^16 / 2^32 -or- 2^31-1, as he pointed out, it appears to be clearly above 64K methods per class..

4

3 に答える 3

6

興味深い質問ですが、実際に限界に達する理由がわからないため、数が多いため、答えはそれほど役に立たない可能性があります。

私はこのスレッドを見つけました。誰かが次のテストを作成して、関数の量を増やしてクラスを実際に作成し、ブレークポイントがどこにあるかを確認しました。

namespace MethodCountLimitFinder
{
    class Program
    {
        [System.STAThreadAttribute]
        static void Main ( string [] args )
        {
            Microsoft.CSharp.CSharpCodeProvider provider = 
                new Microsoft.CSharp.CSharpCodeProvider() ;
            System.CodeDom.Compiler.CompilerParameters cp = 
                new System.CodeDom.Compiler.CompilerParameters() ;
            cp.GenerateExecutable = false ;
            cp.GenerateInMemory = true ; 
            System.CodeDom.Compiler.CompilerResults cr = null ;
            System.Text.StringBuilder inner = 
               new System.Text.StringBuilder ( "namespace Tester { class Test {" ) ;

            int methodCount = 1000000 ; 
            while ( true )
            {
                System.Console.WriteLine ( methodCount ) ;

                for ( int i = methodCount ; i > 0 ; i-- )
                {
                    inner.AppendFormat ( "void M{0}(){{}}\n" , methodCount++ ) ;
                }    
                inner.Append ( "}}" ) ;                
                cr = provider.CompileAssemblyFromSource ( cp , inner.ToString() ) ;
                if ( cr.Errors.Count > 0 )
                {
                    break;
                }                
                inner.Remove ( inner.Length - 2 , 2 ) ;
            } 
            foreach (  System.CodeDom.Compiler.CompilerError ce in cr.Errors )
            {
                System.Console.WriteLine ( ce.ToString() ) ;
            }
        }
    }
}

結果に基づくと、リソースに依存しているように見えます。32/ 64ビットのインデックス参照に結び付けない限り、厳密に定義されていない可能性が高い仕様ではありません。とにかく、おそらく最初にリソース制限。

リソース不足のために失敗する前に、テストは200k以上になりました。

繰り返しになりますが、IMOの情報は興味深いものですが、それほど有用ではありません。

于 2011-06-09T15:39:57.937 に答える
3

I think you'll need to look through the unmanaged interfaces into the CLR for definitions of the metadata structures to find limits on the size of those structures. This answer (and comments) already notes this is ~16 million (24bit index) methods per assembly (not type).

Alternatively consider that reflection methods return arrays, and there is a limit of 1GB per object in the current .NET runtime, so the maximum would be 1024^3/sizeof(MethodIndo).

于 2011-06-09T15:51:51.700 に答える
2

あなたに役立つかもしれないいくつかのリンクが見つかりました。

Javaの制限は65535です

SOfor.NETでの同様の質問

同様の投稿

神オブジェクト

于 2011-06-09T15:41:12.450 に答える