简体   繁体   English

如何将资源文件中的原始 this.text 与覆盖的文本进行比较

[英]How to compare original this.text from resource file to an overwritten text

I have a windows form.我有一个窗体。 Where the title will be overwritten every time a piece of code is hit.每次点击一段代码时标题都会被覆盖。 The title will be by default in resx file of the form until it is overwritten.默认情况下,标题将在表单的 resx 文件中,直到被覆盖。

Form1.resX表单1.resX

 <data name="$this.Text" xml:space="preserve">
    <value>Report</value>
  </data>

Form2.cs表格2.cs

public void Report2()
{
Form1 frm = new Form1();
frm.Text="Report2"
//Some code
}

public void Report3()
{
Form1 frm = new Form1();
frm.Text="Report3"
//Some code
}

Here the this.Text gets overwritten when Report2() and Report3() executes这里this.TextReport2()Report2() Report3()执行时被覆盖

In Form1.cs在 Form1.cs 中

private void Report_ColumnClick(Object eventSender, ColumnClickEventArgs eventArgs)
{
if(this.Text!="Report")
{
//Some code
}
}

So in Form1.cs i am hard coding the resx original value to compare with an overwritten value.因此,在 Form1.cs 中,我对 resx 原始值进行了硬编码以与覆盖的值进行比较。 Is there anyway i can do it dynamically instead of hard coding.无论如何我可以动态而不是硬编码。

I'm not sure if this question is the same as yours?不知道这个问题和你的一样吗?

According to that post it should be as simple as:根据那个帖子,它应该很简单:


ResourceManager rm = new System.Resources.ResourceManager(typeof(Form1));
string formText = rm.GetString("Form1.Text");

OR:或者:

If that doesn't work you can try reading the information from the manifest embedded in the assembly.如果这不起作用,您可以尝试从程序集中嵌入的清单中读取信息。

Get the embedded resource names using reflection typeof(Form1).Assembly.GetManifestResourceNames();使用反射typeof(Form1).Assembly.GetManifestResourceNames();获取嵌入的资源名称typeof(Form1).Assembly.GetManifestResourceNames(); this will give you a list on names if there are any.如果有的话,这会给你一个名字列表。

use the correct name to extract the embedded stream.使用正确的名称来提取嵌入的流。

var resourceStream = typeof(Form1).Assembly.GetManifestResourceStream("Your_NamesPace_Here.Form1.resources");
using (System.Resources.ResourceReader resReader = new ResourceReader(resourceStream))
{
  var dictEnumerator = resReader.GetEnumerator();
  while (dictEnumerator.MoveNext())
  {
    var ent = dictEnumerator.Entry;
    //ent will be a key:value pair, so use it like this                     
    if (ent.Key as string  == "$this.Text")
    {
      string originalFormName = ent.Value as string;
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM