Good evening!

I've got an assembly compiled by C# 3.0 compiler targeting to .NET Framework 2.0.
I'd like to get C# 2.0 compliant code, but Reflector gives only C# 3.0 code. How can I overcome this problem?

For example, given this C# 3.0 source code (“Func†and “Where†are custom, not from FCL 3.5):
// Type inference, anonymous types.
var person = new { Quote = "Cogito ergo sum." };

// Lambdas, closures.
Func<string, string> addCopyright = s => String.Concat(person.Quote, " ©");

// LinqBridge.
var names = new[] { "Alice", "Ann", "Bob", "Mallory" };
var namesStartingWithA = names.Where(n => n.StartsWith("A"));
I build an assembly targeting .NET Framework 2.0.

Then I'd like to disassemble to C# 2.0 code, a kind of:
// Ok, neither anonymous types, nor type inference, plain C# 2.0.
SomeGeneratedType person = new SomeGeneratedType();
person.Quote = "Cogito ergo sum.";

// Ok, no lambdas, just old-time delegate (anonymous method to be precise).
Func<string, string> func = delegate (string s) { return person.Quote + " \x00a9"; };

string[] names = new string[] { "Alice", "Ann", "Bob", "Mallory" };
IEnumerable<string> enumerable =
  new string[] { "Alice", "Anfice", "Bob", "Mallory" }.Where<string>(delegate (string n) { return n.StartsWith("A"); });

But I've got something like this:
// Fail, C# 3.0 features remain.
var person = new { Quote = "Cogito ergo sum." };
...

Do I need an older version of Reflector, which doesn't generate C# 3.0 code, limiting with C# 2.0 one?

Thanks in advance.
——
Victor
Victor86
0

Comments

5 comments

  • Clive Tong
    Reflector won't do the translation that you want for the anonymous type. In the IL the anonymous type is constructed using a constructor argument.
    var foo = new  {A = "jajaj2"};
    
    is generated as a call
    AnonymousType0<string> type = new AnonymousType0<string>("jajaj2");
    
    so you'd have to do analysis to determine that the constructor argument is referring to the property.

    Reflector recognises the anonymous types, causing it to emit the 3.0 syntax, by looking for the prefix "<>f__" on the type name. I managed to get the latter form of the construction by using ildasm, removing the prefix and then using ilasm to reconstruct the assembly but that trick doesn't get you all the way to what you want.
    Clive Tong
    0
  • Victor86
    Well, Clive, thank you very much for the answer!

    Оne more question.

    How to manage extension methods? I want Reflector to generate old-style call:
    IEnumerable&lt;string&gt; namesStartingWithA = Enumerable.Where&#40;
        names, delegate&#40;string n&#41; &#123; return n.StartsWith&#40;"A"&#41;; &#125;&#41;;
    
    instead of 3.0 syntax:
    var namesStartingWithA = names.Where&#40;name =&gt; name.StartsWith&#40;"A"&#41;&#41;;
    

    May be, it's worth to add two different items (C# 2.0 and C# 3.0) insted of the only one (C#) in language choise combobox?
    Victor86
    0
  • Clive Tong
    I think you're looking for the View/Options/Disassembler/Optimization combobox.

    In Reflector 5.1.5, setting this optimization level to 3.5 makes some code appear as
       string&#91;&#93; names = new string&#91;&#93; &#123; "a" &#125;;
        IEnumerable&lt;string&gt; namesStartingWithA = from n in names
            where n.StartsWith&#40;"A"&#41;
            select n;
    

    Changing the level to .NET 2.0 gives
       IEnumerable&lt;string&gt; namesStartingWithA = new string&#91;&#93; &#123; "a" &#125;.Where&lt;string&gt;&#40;delegate &#40;string n&#41; &#123;
            return n.StartsWith&#40;"A"&#41;;
    
    Clive Tong
    0
  • Victor86
    Clive Tong wrote:
       IEnumerable&lt;string&gt; namesStartingWithA = new string&#91;&#93; &#123; "a" &#125;.Where&lt;string&gt;&#40;delegate &#40;string n&#41; &#123; ...
    

    Here we rid of (I'm not sure how it should be in English) type inference and lambdas, but extension method (Where) still remain. I need this transform:
    IEnumerable&lt;string&gt; namesStartingWithA = Enumerable.Where&lt;string&gt;&#40;new string&#91;&#93; &#123; "a" &#125;, delegate &#40;string n&#41; &#123; ...
    
    Victor86
    0
  • sjjoseph
    Hi all,
    I am creating Window Application using c#.net.
    I want to show message box with error icon on button click.
    For showing message box the code is

    MessageBox.Show("Error");

    But What to do for including error icon?
    sjjoseph
    0

Add comment

Please sign in to leave a comment.