Is there a compiler switch to disable Generics support in C #?

I work with a partner where we are trying to move a complex driver from the .NET platform to the .NET MicroFramework.

The problem is that .NET MF does not support Generics, and when we try to create the application, the last link operation fails with the error code CLR_E_PARSER_UNSUPPORTED_GENERICS. However, there is no information about WHERE (module, code line).

As far as we know, no one intentionally introduced Generics, and they really looked at the code to determine what the problem was, no luck.

So my question is: is there a way to disable Generics support in VS2010 so that the compiler will indicate a violation line?

+4
source share
2 answers

Is there any way to turn off Generics support in VS2010 so that the compiler will indicate a violation line?

Yes, but this is a "nuclear" option:

using System.Collections.Generic; class Test { static void Main() { IEnumerable<int> x = null; } } 

 C:\> csc /langversion:ISO-1 \foo.cs Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1 Copyright (C) Microsoft Corporation. All rights reserved. c:\foo.cs(9,12): error CS1644: Feature 'generics' cannot be used because it is not part of the ISO-1 C# language specification 

The ISO-1 switch disables all functions that were not in C # 1.0 , which may be more features than you want to disable.

Note that the switch is not intended to "emulate C # 1.0 in the C # 2.0 compiler"; if you want to run the C # 1.0 compiler, just run it. The switch is designed to identify functions that were not present in a particular version, and to prohibit them.

Please note that the switch also may not do everything you need for this. Everything he does prohibits the use of general syntax. If you use a generic type without actually using generic syntax, the switch will not catch it.

+18
source

There is no such specific compiler.

You can take a look at the source code (or the decompiled source) and look for ways to use generics. Generics can be declared in your project or general constructions (classes, methods, variables, ...) can be used by your project.

You might want to use reflection to look for generic declarations (classes, methods, fields, ... but not variables) in your assembly. To look for the use of generics, you also need to study the IL instructions. A library like Mono.Cecil can help you with this.

UPDATE

It turns out (with Eric Lippert, of course) you can compile your code for the C # 1.0 specification with this switch:

/ langversion: ISO-1

Besides generics, you will also miss a few things that were added in C # 2.0 and later.

SAMPLE CODE

With Mono.Cecil, you can download the assembly and get all its types:

 using Mono.Cecil; using Mono.Cecil.Rocks; ... var asm = AssemblyDefinition.ReadAssembly("MyAssembly.dll"); var types = asm.MainModule.GetAllTypes(); 

And then start making interesting queries with them:

 var genericTypes = types.Where(type => type.HasGenericParameters); var genericMethods = types. Select(type => type.Methods.Where(method => method.HasGenericParameters)); var genericFields = types. Select(type => type.Fields.Where(field => field.DeclaringType.HasGenericParameters)); var genericMethodInstructions = types.Select(type => type.Methods.Where(method => method.HasBody). Select(method => method.Body.Instructions. Where(instruction => instruction.Operand is MethodReference). Select(instruction => (MethodReference)instruction.Operand). Where(methodRef => methodRef.Resolve().HasGenericParameters))); 
+6
source

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


All Articles