This article tells you about the Resharper family (JetBrains) an open source Refasmer tool. The function of this tool is to export all public members from the existing DLL or EXE assembly and re type a new DLL or EXE assembly. At this time, the methods in the new assembly do not include implementation, but are only defined. Such an assembly is turned into an assembly only for reference, that is Reference assemblies means that Refasmer is named from
This Refasmer tool is a dotnet tool, which is very easy to install. Use the following code to install it
dotnet tool install -g JetBrains.Refasmer.CliTool
Then let's use an existing DLL to test its use. For example, I have one called lindexi DLL file, I use the following command line to convert it into an assembly for reference only
refasmer Lindexi.dll
After entering the above code, lindexi will be generated in the same folder dll. refasm.dll file, this file is a reference file. You don't need to use dnspy. You can see the difference between the two DLLs only by the size of the file. You can see refasm on dnspy DLL contains only method definitions and attribute definitions, but there is no specific implementation
Such assemblies for reference only can be easily used in some plug-in development SDK s or some unsupported versions for construction purposes. Or it is used on some super large projects to support the independent construction of a single project in a super large solution
Unlike the productonlyreferenceassembly provided with dotnet, Refasmer can export assemblies for reference only from the DLL. However, the produceonlyreference assembly needs to be generated from the source code. For more information about the produceonlyreference assembly, see msbuild uses the ProduceOnlyReferenceAssembly to create a public member only assembly as a reference
In addition to the above default commands, you can also add the following parameters to refasmer to realize more functions
Specify export folder
The default command exports refasm DLL file. Obviously, such a file is not suitable for republishing as NuGet, otherwise the runtime will fail because the corresponding DLL cannot be found. However, if the file name is the same as the previous DLL, the existing file will be overwritten
The solution is to give the output folder through the - O parameter, as shown in the following code
refasmer Lindexi.dll -O foo
After running the above code, you can find lindexi in the foo folder dll file. The dll file in this foo folder is an assembly for reference only
In addition, if you only want to output a single file, you can use the - o parameter, which is used to reset the file name, as shown in the following command
refasmer Lindexi.dll -o LindexiDoubi.dll
Executing the above code will output lindexidouble DLL assembly
Quiet operation
The default refasmer has no output. If you don't want any output, please add the - q parameter, as shown in the following command
refasmer Lindexi.dll -O foo -q
Output debugging information
Different from quiet operation, the - v parameter is added to output more information
refasmer Lindexi.dll -O foo -v
List file information
Add the - l parameter or -- list parameter to output the exported file information, such as the following command
refasmer Lindexi.dll -l
The output is as follows
<?xml version="1.0" encoding="Codepage - 936"?> <FileList> <File AssemblyName="Lindexi" Version="1.1.0.0" Culture="neutral" PublicKeyToken="0902d2af90156091" InGac="false" ProcessorArchitecture="MSIL" /> </FileList>
For more commands and usage, see JetBrains/Refasmer: The tool to create reference assembly from common assembly.
custom
If you find that the current command line does not meet the requirements, you can also write code yourself
Create one first NET console project, and then install jetbrains.com through NuGet Refasmer Library
<ItemGroup> <PackageReference Include="JetBrains.Refasmer" Version="1.0.12" /> </ItemGroup>
The following is an example of an assembly that creates this console project for reference only. The code is as follows
static void Main(string[] args) { var file = Assembly.GetExecutingAssembly().Location; var output = file + Path.GetRandomFileName(); ToReferenceAssembly(file, output); } private static void ToReferenceAssembly(string file, string output) { using var peReader = new PEReader(new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read)); var metaReader = peReader.GetMetadataReader(); if (metaReader.IsAssembly) { var result = MetadataImporter.MakeRefasm(metaReader, peReader, new LoggerBase(new VerySimpleLogger(Console.Out))); File.WriteAllBytes(output, result); } }
The above code gives the DLL assembly and the desired output file path by passing in the ToReferenceAssembly method, and then will execute the method provided by the refaster library to read the public members from the file and write them to the output file
Metareader is added to the above code Isassembly is used to determine whether the current DLL is a dotnet assembly
The PEReader of the above code is the default mechanism provided by dotnet. The core of the Refasmer library is to read the contents of the assembly
Call MetadataImporter The output of the makerefasm method is a byte array, which can be written to a file
The VerySimpleLogger in the above code is a self-defined log class, which is ignored by the code. You can learn how to get the source code of this article from below
All the code in this article github and gitee Welcome to visit
You can obtain the source code of this article in the following ways. First create an empty folder, then use the command line cd command to enter this empty folder, and enter the following code on the command line to obtain the code of this article
git init git remote add origin https://gitee.com/lindexi/lindexi_gd.git git pull origin 1b9b1a70f93fe065db216472d96a095eb7d39983
The above uses the source of gitee. If gitee cannot access it, please replace it with the source of github
git remote remove origin git remote add origin https://github.com/lindexi/lindexi_gd.git
After getting the code, go to the rainwayjallwhayderelaqea folder
For more about Roslyn, see Teach you to write Roslyn modification and compilation