What are the challenges you face when working across database platforms? Take the survey

Disassembly generates illegal identifiers

Using .NET Reflector version 9, when disassembling the following method:
public string[] WriteProject(IIntermediateAssembly assembly, string rootFolder, string extension = ".cs")
    List<string> fileTracker = new List<string>();
    Dictionary<IIntermediateAssembly, string> fileNameLookup = new Dictionary<IIntermediateAssembly, string>();
    this.nameProvider = new NameProvider(this, fileNameLookup);
    var information =
        (from part in new[] { assembly }.Concat(assembly.Parts)
            let partFile = CSharpProjectTranslator.AssemblyFileVisitor.GetFileInfo(part, this, fileTracker)
            where partFile.YieldsFile
            let fnTrans = GetTranslatorAndFinalName(rootFolder, extension, partFile.FileName, part, fileNameLookup)
            select new { Part = part, FileName = partFile.FileName, FinalName = fnTrans.Item2, Translator = fnTrans.Item1 }).ToArray();
    Parallel.ForEach(information, async assemblyFileInfo =>
        CodeTranslator translator = assemblyFileInfo.Translator;
        var fileStream = new FileStream(assemblyFileInfo.FinalName, FileMode.Create, FileAccess.Write, FileShare.Read);
        var streamWriter = new StreamWriter(fileStream);
        //streamWriter.AutoFlush = true;
        var itw = new IndentedTextWriter(streamWriter, new string(' ', options.IndentationSpaceCount));
        translator.Target = itw;
        await streamWriter.FlushAsync();
    return information.Select(anon1 => anon1.FileName).ToArray();
The Parallel.ForEach segment yields a result that's unlikely to be allowed:
Parallel.ForEach(source, async delegate (<>f__AnonymousType2<IIntermediateAssembly, string, string, CodeTranslator> assemblyFileInfo) {
If there is a better place to post this, or you would prefer to avoid topics associated to illegal names, let me know.
To Note: I'm using .NET 4.5 / C# 5 / VB 11, there doesn't appear to be any difference changing to the latest version of the framework/compilers.


  • Options
    Jessica RJessica R Posts: 1,319 Rose Gold 4
    Hi and thanks for your post!

    Regarding this issue, I'm afraid that the code regeneration is not perfect and illegal names can be expected, especially when compiler-generated code is involved. :/ These names were actually generated by the compiler itself and Reflector is just reconstructing the code based on the IL the compiler created.

    If you're looking to recompile the code and find and any illegal names or compile errors, I'm afraid you'll have to manually edit the code to remove them. So sorry for any inconvenience this causes! Please let me know if you have any further questions at the moment.

    Jessica Ramos | Product Support Engineer | Redgate Software

    Have you visited our Help Center?

  • Options
    RichardDRichardD Posts: 50 Bronze 3
    Parallel.ForEach(information, async assemblyFileInfo =>
    Passing an async delegate to Parallel.ForEach won't work as expected. The delegate you pass in will be turned into an async void method, which means the ForEach method can return before the delegate has completed, and exceptions thrown from the delegate won't be handled properly.

    Stephen Toub wrote a ForEachAsync method on his blog back in 2012 which would work better:

    Alternatively, use TPL Dataflow:
Sign In or Register to comment.