前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >.NET Core 如何禁止.resx文件自动生成Designer.cs

.NET Core 如何禁止.resx文件自动生成Designer.cs

作者头像
Edi Wang
发布2019-07-09 10:12:49
1.1K0
发布2019-07-09 10:12:49
举报
文章被收录于专栏:汪宇杰博客汪宇杰博客

在 Visual Studio 中,如果我们在一个 .NET Core 工程里加入了一个资源文件(.resx),那么你会发现有个对应的 .Designer.cs 文件被自动生成了,每次资源文件的内容有变化,这个设计器文件都会刷新。它本质上就是对应资源文件里的键值对,自动生成访问这些资源的方法。

生成的代码就像这样:

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;

}

}

对于资源文件里的每个Key,都会有个方法来读它的值

/// <summary>

/// Looks up a localized string similar to {&quot;Name&quot;:&quot;Admin&quot;,&quot;Description&quot;:&quot;Moonglade

Admin&quot;,&quot;ShortDescription&quot;:&quot;Moonglade Admin&quot;,&quot;AvatarBase64&quot;:&quot;&quot;}.

/// </summary>

internal static string BlogOwnerSettings {

get {

return ResourceManager.GetString("BlogOwnerSettings", resourceCulture);

}

}

但是,我不希望使用这些代码来读取资源文件。因此我需要禁用自动生成Desinger.cs文件。

事实上,这个Designer.cs文件的生产方式是通过CustomTool生成的,就像EF4-6时候通过T4模板生成代码一样,也是一种CustomTool。给资源文件(.resx)生成对应的 .Designer.cs 文件的CustomTool叫做ResXFileCodeGenerator

Visual Studio 中,你可以在RESX文件的属性窗口里将它设置为 <reset to default> 从而关闭这货

如果你用的是 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>

那么现在,我们如何从资源文件里读取字符串呢?很简单:

ResourceManager rm = new ResourceManager("Moonglade.Setup.Data.DataResource", Assembly.GetExecutingAssembly());

rm.GetString("Your_Resource_Key");

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-05-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 汪宇杰博客 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档