每个 .NET 类的最大方法数是多少

发布于 2024-11-14 20:37:43 字数 396 浏览 6 评论 0原文

标题实际上问了这一切,但仍然是为了完整性:

嗨,我正在 .NET 平台中编写一个小型后编译工具,在尝试优化它时,我遇到了一个我不能轻易解决的问题从公共语言基础设施的 ECMA 标准中找到答案 (CLI)。

单个类最多可以拥有多少个方法?有限制吗?

编辑

感谢凯尔西指出现实生活中的测试。 尽管我仍然会关心实际的限制是多少,但出于我实际的现实生活目的, 我想知道它是 2^16 / 2^32 -还是 - 2^31-1,正如他指出的那样,它似乎明显高于每个类 64K 方法。

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..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

以为你会在 2024-11-21 20:37:43

有趣的问题,但不知道为什么你会在现实中达到极限,所以答案可能没那么有用,因为它是一个很高的数字。

我发现这个 线程< /a> 有人编写了以下测试来实际创建一个函数数量不断增加的类,以查看断点在哪里:

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+ 的测试。

同样,在我看来,这些信息很有趣,但并不是那么有用。

Interesting question but no clue why you would ever hit the limit in reality so the answer might not be that useful because it is a high number.

I found this thread where someone wrote the following test to actually create a class with increasing amounts of functions to see where the breaking point was:

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() ) ;
            }
        }
    }
}

Based on the results it looks like it is resource dependent, not the spec which is most likely not hard defined unless you tie it to a 32/64-bit index reference or something which I don't think is realistic since you will hit a resource limit probably first anyways.

The test got over 200k+ before failing due to lack of resources.

Again, interesting but not all that useful of information IMO.

快乐很简单 2024-11-21 20:37:43

我认为您需要查看 CLR 中的非托管接口以查找元数据结构的定义,以找到这些结构的大小限制。这个答案(和评论)已经指出这是每个程序集(非类型)约 1600 万(24 位索引)方法。

或者考虑反射方法返回数组,并且当前 .NET 运行时中每个对象的大小限制为 1GB,因此最大值为 1024^3/sizeof(MethodIndo)

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).

你的心境我的脸 2024-11-21 20:37:43

找到了一些可能对您有帮助的链接。

Java 的限制为 65535

.NET 中的类似问题

类似帖子

上帝对象

Found few links which may be helpful for you.

Limit For Java is 65535

Similar Question in SO for .NET

Similar Post

God Object

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文