FileExtension–plugins–part 3
What I have more in mind is that the people can add easy their code for extensions.
That means:
- Have an easy class to add the magic number in the header for an extension
- Have other people add their own implementations, not just the header
- Have an easy way to add the project with their code
First item it is easy – just create a class RecognizeFirstBytes and pass into constructor the first bytes
As an example , see the sln implementation
public class RecognizeSln : RecognizeFirstBytes { public RecognizeSln() : base("EFBBBF0D0A4D6963726F736F66742056697375616C2053747564696F20536F6C7574696F6E2046696C65") { Extension = new string[1] { "sln" }; } }
The second item can be implemented with an interface IRecognize
public interface IRecognize: IEquatable<IRecognize> { Result InfoNeeded(byte[] b=null); string[] Extension { get; set; } }
The third item can be made by making a third project that either references all other projects, either loading others as plugins . Because of the experience, I choose the most interesting one – plugins. I need first https://github.com/natemcmaster/DotNetCorePlugins . Second I need the plugins to be copied – I use a Target
<Target Name="PreBuild" BeforeTargets="PreBuildEvent"> <Exec Command="xcopy "$(ProjectDir)../plugins/*.dll" "$(ProjectDir)plugins" /E /F /I /Y /R" /> </Target>
And last I need this plugin loader , when packed as nuget at https://www.nuget.org/packages/FileExtension/ , to have also the plugins – for this I use the csproj
<ItemGroup> <Content Include="plugins\**\*.dll"> <PackagePath>build\net5.0\plugins\</PackagePath> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> </ItemGroup>
Leave a Reply