.NET Core 如何禁止.resx文件自动生成Designer.cs
點(diǎn)擊上方藍(lán)字關(guān)注“汪宇杰博客”
在 Visual Studio 中,如果我們在一個(gè) .NET Core 工程里加入了一個(gè)資源文件(.resx),那么你會(huì)發(fā)現(xiàn)有個(gè)對應(yīng)的 .Designer.cs 文件被自動(dòng)生成了,每次資源文件的內(nèi)容有變化,這個(gè)設(shè)計(jì)器文件都會(huì)刷新。它本質(zhì)上就是對應(yīng)資源文件里的鍵值對,自動(dòng)生成訪問這些資源的方法。
生成的代碼就像這樣:
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal DataResource() {
}
/// <summary>
///? ?Returns the cached ResourceManager instance used by this class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {
? ? get {
? ? ? ? if (object.ReferenceEquals(resourceMan, null)) {
? ? ? ? ? ? global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Moonglade.Setup.Data.DataResource", typeof(DataResource).Assembly);
? ? ? ? ? ? resourceMan = temp;
? ? ? ? }
? ? ? ? return resourceMan;
? ? }
}
/// <summary>
///? ?Overrides the current thread's CurrentUICulture property for all
///? ?resource lookups using this strongly typed resource class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture {
? ? get {
? ? ? ? return resourceCulture;
? ? }
? ? set {
? ? ? ? resourceCulture = value;
? ? }
}
對于資源文件里的每個(gè)Key,都會(huì)有個(gè)方法來讀它的值
/// <summary>
///? ?Looks up a localized string similar to {"Name":"Admin","Description":"Moonglade
Admin","ShortDescription":"Moonglade Admin","AvatarBase64":""}.
/// </summary>
internal static string BlogOwnerSettings {
? ? get {
? ? ? ? return ResourceManager.GetString("BlogOwnerSettings", resourceCulture);
? ? }
}
但是,我不希望使用這些代碼來讀取資源文件。因此我需要禁用自動(dòng)生成Desinger.cs文件。
事實(shí)上,這個(gè)Designer.cs文件的生產(chǎn)方式是通過CustomTool生成的,就像EF4-6時(shí)候通過T4模板生成代碼一樣,也是一種CustomTool。給資源文件(.resx)生成對應(yīng)的 .Designer.cs 文件的CustomTool叫做ResXFileCodeGenerator
在 Visual Studio 中,你可以在RESX文件的屬性窗口里將它設(shè)置為 <reset to default> 從而關(guān)閉這貨
如果你用的是 Visual Studio Code,可以手工編輯csproj文件,刪除這段:
<ItemGroup>
? <Compile Update="Data\DataResource.Designer.cs">
? ? <DesignTime>True</DesignTime>
? ? <AutoGen>True</AutoGen>
? ? <DependentUpon>DataResource.resx</DependentUpon>
? </Compile>
</ItemGroup>
<ItemGroup>
? <EmbeddedResource Update="Data\DataResource.resx">
? ? <Generator>ResXFileCodeGenerator</Generator>
? ? <LastGenOutput>DataResource.Designer.cs</LastGenOutput>
? </EmbeddedResource>
</ItemGroup>
那么現(xiàn)在,我們?nèi)绾螐馁Y源文件里讀取字符串呢?很簡單:
ResourceManager rm = new ResourceManager("Moonglade.Setup.Data.DataResource", Assembly.GetExecutingAssembly());
rm.GetString("Your_Resource_Key");
總結(jié)
以上是生活随笔為你收集整理的.NET Core 如何禁止.resx文件自动生成Designer.cs的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: EventStore文件存储设计
- 下一篇: .net core百万设备连接服务和硬件