Is it possible to get the "this" reference from the generic argument type?

  • Thread starter Thread starter E-John
  • Start date Start date
E

E-John

Guest
Dear All,

I would like to extract the method to base class or base method(that is for all WinForms multilingual text load), and generic parameter T type is used in this method to do multilingual text load,

like below, snippet 1 and snippet 2, my question is

is it possible to get the "this" reference from the generic argument T?

Or the only way is to change the method to snippet 3? (I have tested it, it is OK)


Thanks and Best regards,

E-John


TYMultilinguist linguist = TYMultilinguist.GetInstance();
string neutralCulture = linguist.GetAppUISavedNeutralCulture();
MultiLangChangeContrlTextLanguage(neutralCulture, this);

snippet 1


private void MultiLangChangeControlTextRecursive(ComponentResourceManager resources, CultureInfo cult, Control control)
{
if (control.Controls.Count != 0)
{
foreach (Control ctrl in control.Controls)
{
resources.ApplyResources(ctrl, ctrl.Name, cult);

MultiLangChangeControlTextRecursive(resources, cult, ctrl);
}
}
}
private void MultiLangChangeContrlTextLanguage<T>(string neutralCulture, T type)
{
ComponentResourceManager resources = new ComponentResourceManager(typeof(T));
CultureInfo cult = new CultureInfo(neutralCulture);

// change Win Form title

// Is it possible to get "this" from type???
resources.ApplyResources(this, "$this", cult);

// Buttons are embedded in the GroupBox
// e.g., this.Controls.Add(this.groupBoxPassword);
// and this.groupBoxPassword.Controls.Add(this.maskedTextBoxPassword);
foreach (Control ctrl in this.Controls)
{
resources.ApplyResources(ctrl, ctrl.Name, cult);

MultiLangChangeControlTextRecursive(resources, cult, ctrl);
}
}



snippet 2


private void MultiLangChangeContrlTextLanguage(string neutralCulture, Form form)
{
ComponentResourceManager resources = new ComponentResourceManager(form.GetType());
CultureInfo cult = new CultureInfo(neutralCulture);

// change Win Form title
resources.ApplyResources(form, "$this", cult);

foreach (Control ctrl in form.Controls)
{
resources.ApplyResources(ctrl, ctrl.Name, cult);

MultiLangChangeControlTextRecursive(resources, cult, ctrl);
}
}

snippet 3

Continue reading...
 
Back
Top