Field not found: System.Text.RegularExpressions.Regex.internalMatchTimeout

We encountered the problem described here: field-not-found-exception-with-net-4-4-5-compiled-regexes

In short: I tried to create a .Net 4.0 assembly on a machine that was upgraded to .Net 4.5. Therefore, I am targeting .Net 4.0. When trying to run this assembly on a machine that has .Net 4.0 installed, the following exception appears: Field not found: "System.Text.RegularExpressions.Regex.internalMatchTimeout.

If I create the same assembly on a machine that has not been upgraded to .Net 4.5, I can run the generated assembly on a .NET 4.0 machine without any problems. In other words: the .Net 4.0 assembly created on the .Net 4.5 machine does not match the one created on the .NET 4.0 machine.

The assembly contains precompiled regular expressions.

I can solve this problem:

  • Build an assembly on a .NET 4.0 system.
  • Upgrading the target machine to .Net 4.5.

However, there are problems with both solutions:

  • We collect assemblies for various purposes, including WinRT. Now we are faced with the problem that we cannot use one machine to create them, which complicates the assembly / testing process.
  • Produced assemblies are sent to customers. They will not like everything if we tell them to switch to .Net 4.5 in order to use assembly 4.0.

Do any of you know of a better solution than getting rid of precompiled regular expressions?

+4
source share
2 answers

Apparently this is a backward compatibility bug of .NET 4.5 itself or Visual Studio. And it is quite old , judging by the date of the first version and is not allowed by the date (do you have all the versions of .NET and VS installed on the 4.5 system?). If it has not been fixed so far, I would go with solution 1 (I cannot expect MS to fix the error quickly).

0
source

It’s not clear from your question, but I assume that you call Regex.CompileToAssembly, and in this case what happens underneath it is an assembly that is closely related to the runtime at which it was created, so if you run this on .NET 4.5 it will have links to the new field "internalMatchTimeout". In addition to creating this assembly in the .NET 4.0 runtime (solution 1), did you consider compiling them the first time you use and cache them?

RegEx re = new Regex(pattern, RegExOptions.Compiled); 

then you can just cache the whole RegEx you need in your application. While this will give a little performance on first use, it will prevent many jumps through hoops in your assemblies.

+1
source

Source: https://habr.com/ru/post/1493144/


All Articles