[英]Using application's *.resx from a generic DLL
我正在创建一个通用DLL,该DLL从模型创建文档,并且可以用于winForms或webForms。 它由我用某些参数实例化的主类组成。 我希望我的DLL能够在资源文件中查找,而又不受限于1种技术。
可以这么说,我知道如何在WebForm中访问我的资源文件( *.resx
):
HttpContext.GetGlobalResourceObject("Global", "myLabel")
我有一些限制:
*.resx
重命名为*.resource
因为它们在应用程序中使用了 <%resource(Global,myLabel)%>
这样的标记 我一直在使用ResourceManager
向类传递资源对象,但它永远都无法保存我的*.resx
文件。
有谁知道如何实现最终目标? 要么将资源对象传递给类,要么从类内部拾取资源对象。
好的,我发现了如何做到这一点。 在我的DLL中,我使构造函数过载,从而允许给它一个ResourceManager
对象。 所以这是我所拥有的:
对于模型文件:
\\paragraph [ Style = "Normal" ] { <%resource(lblMontant)%> : <%montant%> }
对于使用ResourceManager
的DLL:
public Reporter(String inputModel, String outputPdf, Dictionary<String, IParameter> parameters, ResourceManager resman) { // Assigne parameters to globals _sourceFile = inputModel; _destinationFile = outputPdf; _parameters = parameters; _rm = resman; Worker(); } private String parseResource(String val) { MatchCollection _matches = _resourceMatcher.Matches(val); foreach (Match _match in _matches) { String _item = _match.Groups["item"].Value; val = val.Replace(_match.Groups[0].Value, String.Format("{0}", _rm.GetObject(_item))); } return val; }
对于调用方,我们使用被视为类的Resources.global
:
ResourceManager _rm = new ResourceManager(typeof(Resources.global)); Reporter _cl = new Reporter(modelFilePath, outputFilePath, _params, _rm);
如果这对其他人有用并且有些人需要更多细节,请随时询问;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.