我是否可以将程序集属性(如[assembly: AssemblyMetadata("key", "value")])标记为私有/内部属性,使其仅从应用到的程序集中访问?
背景:我刚刚测试了一种技术,通过AssemblyMetadata属性在代码中获取一些构建信息(例如解决方案目录),并通过将以下类添加到项目并将msbuild目标添加到*.csproj文件中,从而获得自定义的msbuild目标:
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>发布于 2017-03-14 10:10:55
我是否可以将程序集属性(如
[assembly: AssemblyMetadata("key", "value")])标记为私有/内部属性,使其仅从应用到的程序集中访问?
不是的。元数据的要点是,它需要从.NET本身或外部工具(如Reflector或JetBrains DotPeek )中读取。根据定义,元数据是公开的。把它藏起来就违背了目的。
如果您希望这些信息是私有的,请不要使用.NET程序集属性。您可以将其存储为资源,并使用您喜爱的保护方法对其进行保护。
https://stackoverflow.com/questions/42782729
复制相似问题