getting the source code from a CLR assembly

Hi Peeps,

Is there a way of getting the source code from a CLR assembly?

We have a database created by another department that has a CLR assembly used in a SP, and we need to change the CLR because it is referencing a column that doesn't exist anymore and we need to update it.

Can we update the CLR without the source code, or is there a way to get the source code from the assembly.

more ▼

asked May 08, 2011 at 06:59 PM in Default

avatar image

Daniel Ross
2.9k 11 15 18

(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

I haven't done with a SQL CLR assembly, but .NET Reflector from Red-Gate works very well to reverse engineer .NET assemblies.

more ▼

answered May 08, 2011 at 07:51 PM

avatar image

25k 3 13 20

I didn't even think of using reflector, thanks.

I used the script from this link to save the dll to the hard drive. http://serverfault.com/questions/139703/extracting-a-sqlclr-assembly

May 08, 2011 at 10:11 PM Daniel Ross
(comments are locked)
10|1200 characters needed characters left

I'll add an answer in here...

1 - The script to extract the assembly from the database at http://serverfault.com/questions/139703/extracting-a-sqlclr-assembly is not correct. SELECT @IMG_PATH = content FROM sys.assembly_files WHERE assembly_id = 65536 will return an arbitrary file from the assembly, and there is no requirement that the assembly only has one file. If you look at the MSDN reference for sys.assembly_files when you will see that you want to limit it WHERE file_id = 1 to obtain the assembly DLL.

2 - .NET reflector does not always decompile things correctly. I have had instances in the past where I have been decompiling something in order to recompile it, and the recompiled version has been subtly different. An example of this was when trying to decompile and recompile a third party T-SQL script parsing component - it looked like it worked, but then fell over on some of the less obvious code paths - so definitely tread with caution.

more ▼

answered May 09, 2011 at 01:07 AM

avatar image

Matt Whitfield ♦♦
29.5k 62 66 88

Great point that seems to hold true for many decompilers

May 09, 2011 at 05:26 AM KenJ

+1, very true...

May 09, 2011 at 07:57 AM Matt Whitfield ♦♦

@Matt Whitfield I would like to ask about another possibility. Usually, the reflectors are designed to use instances/static methods of the System.Reflection types to reflect into the source types querying for methods/properties with different combination of the flags (thus the name - reflector). The other alternative is to use the ildasm tool provided by .NET Framework. This tool gives a complete copy of the source assembly in MSIL language unless the latter was compiled with ngen (it makes no assumptions/changes whatsoever). This is why they say to never include any sensitive info into the binary :)

While MSIL is not trivial to understand, it still shows the source in plain text, so if all that is needed to be changed is limited to removing the column from the embedded query then it appears that it could be suffifient to edit the MSIL by hand and then compile it back to DLL. This way, the logic of the code will certainly remain the same. This way it would be possible to implement the change even if the source was treated by the Obfuscator which usually makes the refectors scratch their heads.

Do I make any sense or I simply need more coffee?

May 09, 2011 at 08:21 AM Oleg

@Oleg - you definitely make sense - but editing MSIL by hand is certainly not for the feint hearted - it's not a route I'd take unless it was an absolute emergency... :)

May 10, 2011 at 01:21 AM Matt Whitfield ♦♦

@Oleg - also, congrats on the 10k sir! :)

May 10, 2011 at 08:10 AM Matt Whitfield ♦♦
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.



asked: May 08, 2011 at 06:59 PM

Seen: 6205 times

Last Updated: May 09, 2011 at 12:53 AM

Copyright 2018 Redgate Software. Privacy Policy