I am working with a partner where we are trying to move a complex driver from the .NET platform to the .NET MicroFramework.
The problem is that the .NET MF doesnt support Generics and when we try to build the application the last "link" operation exits with the error code "CLR_E_PARSER_UNSUPPORTED_GENERICS". There is however no information on WHERE (module, code-line).
As far as we know nobody has intentionally inserted Generics and they have been really looking over the code to identify what the problem is with no luck.
So my question is: Is there some way to turn off support for Generics in VS2010 so that the compiler will flag the offending line ?
Is there some way to turn off support for Generics in VS2010 so that the compiler will flag the offending line ?
Yes, but it 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 turns off all features that were not in C# 1.0, which might be more features than you want to turn off.
Note that the switch is not intended to be a "emulate C# 1.0 in the C# 2.0 compiler" switch; if you want to run the C# 1.0 compiler, just run it. The switch is intended to identify features that were not present in the particular version and disallow them.
Note that the switch also possibly does not do everything you need it to do. All it does is disallow uses of generic syntax. If you are using a generic type without actually using generic syntax, the switch doesn't catch it.
There's no such specific compiler switch.
You can take a look at the source code (or the decompiled source) and search for usages of generics. Generics can be declared in your project or generic constructs (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 usage of generics, you need to look into the IL instructions as well. A library like Mono.Cecil can help you with this.
UPDATE
Turns out (with Eric Lippert help, of course) you can compile your code for the C# 1.0 spec with this switch:
Apart from generics, you'll also miss on a few things that were added to C# 2.0 and later.
SAMPLE CODE
With Mono.Cecil, you can load an 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 against 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)));
来源:https://stackoverflow.com/questions/5236852/is-there-a-compiler-switch-to-turn-off-support-for-generics-in-c