Can I mark an assembly attribute (like [assembly: AssemblyMetadata("key", "value")]
) as private/internal to make it only accessible from within the assembly it is applied to?
Background: I have just tested a technique to get some build information in code (e.g. the solution directory) via the AssemblyMetadata
attribute and a custom msbuild target by adding the following class to the project and msbuild target to the *.csproj file:
static class BuildEnvironment
{
static string _solutionDir;
public static string SolutionDirectory
{
get
{
if (_solutionDir == null)
_solutionDir = Initialize();
return _solutionDir;
string Initialize()
{
var metadata = typeof(BuildEnvironment).GetTypeInfo().Assembly.GetCustomAttributes<AssemblyMetadataAttribute>();
return metadata.FirstOrDefault((x) => x.Key == "SolutionDir")?.Value ?? string.Empty;
}
}
}
}
<Target Name="GenerateBuildEnvironment" BeforeTargets="CoreCompile">
<ItemGroup>
<AssemblyAttributes Include="AssemblyMetadata">
<_Parameter1>SolutionDir</_Parameter1>
<_Parameter2>$(SolutionDir)</_Parameter2>
</AssemblyAttributes>
</ItemGroup>
<WriteCodeFragment AssemblyAttributes="@(AssemblyAttributes)" Language="C#" OutputDirectory="$(IntermediateOutputPath)">
<Output TaskParameter="OutputFile" ItemName="Compile" />
</WriteCodeFragment>
</Target>
Aucun commentaire:
Enregistrer un commentaire