1

During the process of obfuscating a .NET assembly (using Dotfuscator), I have found myself tweaking how things are renamed. This often involves looking at the assembly in ILDASM and tracing a Type back to the source code file that it is defined in.

Usually this is a simple process. But I have found that locating an Anonymous Type is very difficult -- especially in a large assembly.

If I am trying to find the location of an anonymous type, such as the following line of code:

new { Name = 'Gene', Age = 30 }

Which is compiled as:

<>f__AnonymousType0`2'<'<Name>j__TPar','<Age>j__TPar'>`

And appears as the root of the assembly in the ILDASM tree.

If I want to locate the anonymous type in the source code, I am left without much help:

  • No Namespace
  • No symbols to search on
  • Nothing in the Solution Navigator
  • Nothing in the Class View
  • Nothing in the Object Browser

Am I missing something? Are there any tools to help locate an Anonymous Type in code files?

Gene C
  • 2,010
  • 26
  • 34
  • Now after working with this for a day, I have found that if I provide "Find In Files" the search query something like `new { Name = ` I can find what I am looking for most of the time. – Gene C Sep 27 '11 at 15:37

1 Answers1

3

Obviously the code for the anonymous class itself isn't present in the original source code, but there's source code which leads to the creation of the anonymous class, and I assume that's what you're trying to find. However, There may be multiple source files involved.

For example, in your example, there could be:

Class1.cs:
    var x = new { Name = "Jon", Age = 10 };

Class2.cs:
    var y = new { Name = 100, Age = 10m };

// And potentially many other source files

Both of those would use the same generic type, but with different type arguments. Basically you'd need to find every anonymous-object-creation-expression expression using the same property names in the same order. I wouldn't be surprised if the metadata contained nothing to help you here directly.

EDIT: The generated code will contain a reference to the constructor of the anonymous type, of course - so you could look for that. It's not clear what tools you're using, but searching for the name of the anonymous type within the IL would fine the uses.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • We are clearly interpreting the question differently than each other. – jason Sep 26 '11 at 16:01
  • @Jason: Could be. I was thinking of the source code that is responsible for the anonymous type being generated. The compiler doesn't do this on a whim, after all. The code for the class isn't available in source form, but there *is* source code which causes that class to be generated... and if the names in the anonymous type are changed, it's that source code which would be *effectively* affected. – Jon Skeet Sep 26 '11 at 16:03
  • @Jon @Jason: I will look into clarifying my question. What I want is a tool or technique to find all usages of: `new { Name = ?, Age = ? }` when they exist in an assembly -- What file they are in, what line they are on, etc.. – Gene C Sep 26 '11 at 16:10
  • @Jon Skeet: Yeah, I thought he was looking for the type definition (he says "locating an Anonymous Type" and "I want to locate the anonymous type"). It seems you saw through this and interpreted the question the way he intended. Deleting mine. +1 you. – jason Sep 26 '11 at 16:15