Click here to Skip to main content
15,892,746 members
Articles / Programming Languages / C#

Replace Reflector(ed) Code

Rate me:
Please Sign up or sign in to vote.
4.00/5 (5 votes)
20 Sep 2005CPOL3 min read 35.3K   828   23   2
A VS.NET add-in which replaces wrongly generated code by Reflector, for interfaces' properties and events.

Sample Image

Introduction

This add-in allows you to rectify the code generated by Reflector, a well known decompiler which converts the MSIL to C# or VB.NET source code. Sometimes the generated code is not compilable because the produced code is not in the form of C# or any other .NET language.

Using this add-in we can make the code compilable by replacing with the correct code for events and properties. Currently this supports the following tasks (it works only for C# code):

  • Replace the (wrong) methods with properties.
  • Replace the (wrong) methods with events.

Background

I was thinking of developing an object browser for JavaScript, HTML, and CSS [shall I call it AJAX object browser?]. I did not want to use the ActiveX web control for this, rather I wanted a lightweight managed control. To have an idea, I wanted to look at the way the MS ASP.NET WebMatrix ClassBrowser is implemented. I used reflector to decompile the code and tried to compile it. I was unlucky, the compiler gave lots of errors saying one of the members of an interface is not implemented. I tried to solve this converting it into proper code but it consumed a lot of time. So I thought I could write an add-in which will do the job for me. The outcome is this add-in.

Where the Reflector makes mistake

When a class implements an interface, while the interface has properties or events, we face a problem using the reflector. For example, TypeView class in Microsoft.Matrix.Packages.ClassView implements the interface IDocumentView, which has a property CanDeactivate and an event DocumentChanged with other members.

The wrongly generated code looks like this:

For the DocumentChanged event:

C#
void IDocumentView.add_DocumentChanged(EventHandler value)
{
}

For the CanDeactivate property:

C#
bool IDocumentView.get_CanDeactivate()
{
    return true;
}

The right code

The following correct code can be produced by the add-in:

For the event:

C#
public event EventHandler DocumentChanged;

For the property:

C#
bool IDocumentView.CanDeactivate
{
    get
    {
    return true;
    }
}

How it works

Sample Image

When you select the (wrong) method and right-click, the 'Replace the Method' menu item will appear. When selecting the menu, it takes the method and decides whether it should be converted either to property or event using these methods.

If get_ or set_ is part of the method, then it is a property.

C#
private bool IsProperty(string signature)
{
    if ((signature.IndexOf("get_") >=0)||
        (signature.IndexOf("set_") >=0)) return true;
    return false;
}

If add_ or remove_ is part of the method, then it is an event.

C#
private bool IsEvent(string signature)
{
    if ((signature.IndexOf("add_") >=0)||
        (signature.IndexOf("remove_") >=0)) return true;
    return false;
}

Based on the result, the property or event will be manipulated and inserted above the current location in the editor. The wrong code will be commented.

Points of Interest

I am doing loot string handling to achieve this. I am not able to get the function body from the Code Function. I have to find out a better way to manipulate and get the function body.

Known Issue

  • Currently it supports only C# source code.
  • While commenting the wrong code, the line indent is changing.
  • The inserted code sometimes is not indented correctly.

History

This is the first version - 1.0.

Credits

The DSWatch and GhostDoc add-ins were very helpful in coding this add-in.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionAsk Lutz to fix? Pin
Frank Hileman27-Sep-05 11:45
Frank Hileman27-Sep-05 11:45 
Did you try asking Lutz to fix it first? He is a pretty nice guy.

check out VG.net: www.vgdotnet.com
An animated vector graphics system integrated in VS.net
AnswerRe: Ask Lutz to fix? Pin
murugan_gs28-Sep-05 19:50
murugan_gs28-Sep-05 19:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.