C# 程序使用的用户字符串的组合长度超过了允许的限制.尝试减少使用字符串文字 [英] C# Combined length of user strings used by the program exceeds allowed limit. Try to decrease use of string literals

查看:76
本文介绍了C# 程序使用的用户字符串的组合长度超过了允许的限制.尝试减少使用字符串文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我向我的类添加一个返回长字符串的静态字符串属性时,我收到此错误.

When I add a static string property to my class that returns a long string I get this error.

在同一个项目中,我添加了许多超过 20k 个字符的字符串属性,但是当我创建另一个返回字符串的属性并构建项目时,我收到此错误.

In the same project I have added lots of string properties over 20k chars, but when I create another property returning a string and build the project I get this error.

我怎样才能增加这个限制?我尝试使用 Stringbuilder,但发生了同样的情况.

How can I increase that limit? I tried to use Stringbuilder but same happens.

推荐答案

只是想把我最近的经验带到桌面上.

Just want to bring my recent experience to the table.

如果您在网上搜索 CS8103 错误,则没有太多信息.而且大多数信息并不完全准确.

There is not much information around if you search the net for CS8103 error. And most information is not entirely precise.

我们昨天在一个大型 C# 解决方案中遇到了这个问题.

We ran into this yesterday, in a large C# solution.

据我了解这个编译错误,问题是所有编译时定义的字符串连接的总长度.

As I understand this compile error, the problem is the total length of all compile time defined strings concatenated.

我将其解释为编译器将所有字符串放入大量数据块中,然后当引用给定字符串时,它会索引到此(或类似)中.

I interprets this as the compiler puts all strings in a massive datablock, and then when a given string is referenced, it indexes into this (or similar).

因此,请尝试将其理解为在 cs 文件中定义的所有字符串 - 连接为一个.如果这个字符串变得太大 - 我们会冒运行时错误的风险,正如上面 Roslyn 问题所解释的那样.

So try to understand it as all your strings defined in cs files - concatenated to one. If this string becomes too large - we risk runtime errors as the Roslyn issue above explains.

代码中的所有字符串,所有在双引号内的都算数.

All strings in code, all that is inside double quotes count.

public void Main() {
    Console.WriteLine("test");
    string test = "AnotherTest" + " " + "run";
}

像上面这样的程序有 4 + 11 + 1+ 3 = 19 => "testAnotherTest run"

A program like above has 4 + 11 + 1+ 3 = 19 => "testAnotherTest run"

昨天我们的程序集在将功能分支合并到 master 时达到了限制.那是使杯子滚过的著名滴.

Our assembly reached the limit yesterday when merging a feature branch into master. That was the famous drop that makes the cup run over.

似乎没有解决方法,只能将大型程序集拆分为较小的部分,或者将大块字符串移动到资源或其他外部文件中.

There seems to be no workaround, but to split your large assembly into smaller pieces, or move large block of strings into resources or other external files.

我们通过注释掉大部分遗留代码来验证这一点 - 我们可以再次编译.

We verified this by commenting out a large portion of legacy code - and we could compile again.

我们的解决方案是重构大型程序集中的特定区域,并为此创建一个新程序集.

Our solution was to refactor a specific area out of the large assembly, and create a new assembly for this.

我知道,文字太多了 - 但我认为这值得一个彻底的解释,因为很难找出导致问题的原因.

I know, soo much text - but thought this deserved a thorough explanation, as it was quite hard to figure out what caused the problem.

鉴于其他地方的稀疏信息,这不是一个常见问题.但是它会停止生产线,并且在解决方案确定之前没有人可以编译.

And given the sparse information else where, this is not a common problem. But it stops the production line, and no one can compile until a solution is fixed.

这篇关于C# 程序使用的用户字符串的组合长度超过了允许的限制.尝试减少使用字符串文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆