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
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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.