Click here to Skip to main content
Click here to Skip to main content
Go to top

Replace Reflector(ed) Code

, 20 Sep 2005
Rate this:
Please Sign up or sign in to vote.
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:

void IDocumentView.add_DocumentChanged(EventHandler value)
{
}

For the CanDeactivate property:

bool IDocumentView.get_CanDeactivate()
{
    return true;
}

The right code

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

For the event:

public event EventHandler DocumentChanged;

For the property:

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.

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.

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)

Share

About the Author

murugan_gs
Web Developer
India India
No Biography provided

Comments and Discussions

 
QuestionAsk Lutz to fix? PinmemberFrank Hileman27-Sep-05 11:45 
AnswerRe: Ask Lutz to fix? Pinmembermurugan_gs28-Sep-05 19:50 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 20 Sep 2005
Article Copyright 2005 by murugan_gs
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid