|
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Reflection;
using System.Resources;
using System.IO;
namespace TestApp
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label4;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.label1 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.listBox1 = new System.Windows.Forms.ListBox();
this.label4 = new System.Windows.Forms.Label();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label1,
this.label3,
this.label2,
this.listBox1});
this.groupBox1.Location = new System.Drawing.Point(8, 56);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(384, 184);
this.groupBox1.TabIndex = 2;
this.groupBox1.TabStop = false;
//
// label1
//
this.label1.Location = new System.Drawing.Point(12, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(184, 16);
this.label1.TabIndex = 7;
this.label1.Text = "Generated resource access classes";
//
// label3
//
this.label3.Location = new System.Drawing.Point(222, 72);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(144, 88);
this.label3.TabIndex = 6;
//
// label2
//
this.label2.Location = new System.Drawing.Point(212, 48);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(160, 16);
this.label2.TabIndex = 5;
this.label2.Text = "Resource string value:";
//
// listBox1
//
this.listBox1.Location = new System.Drawing.Point(12, 40);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(176, 134);
this.listBox1.Sorted = true;
this.listBox1.TabIndex = 4;
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
//
// label4
//
this.label4.Location = new System.Drawing.Point(8, 6);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(384, 40);
this.label4.TabIndex = 3;
this.label4.Text = "The listbox contains the classes generated for each of the string resources in Ba" +
"seResources.resx. Click on an item to see its corresponding value displayed on " +
"the right.";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(398, 245);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label4,
this.groupBox1});
this.Name = "Form1";
this.Text = "GenResourceKeys Sample Application";
this.Load += new System.EventHandler(this.Form1_Load);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
// We want to fill the listbox with all the string access classes
Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream( "TestApp.BaseResources.resources" );
ResourceReader reader = new ResourceReader( stream );
IDictionaryEnumerator dict = reader.GetEnumerator();
// Iterate through the resource identifiers for the BaseResources file
while( dict.MoveNext() )
{
string key = dict.Key.ToString();
// Get the assembly name
string assembly = Assembly.GetExecutingAssembly().GetName().Name;
// Create the qualified type name to access
string typeName = string.Format( "{0}.{1}", assembly, key );
// Load the string resource access class
Type type = Type.GetType( typeName );
// Only add the resource name to the list if there
// is a corresponding access class defined.
if( type != null )
listBox1.Items.Add( key );
}
}
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
// Cast the sender to a ListBox control
ListBox list = sender as ListBox;
// Get the selected string
string key = (string)list.SelectedItem;
// Get the assembly name
string assembly = Assembly.GetExecutingAssembly().GetName().Name;
// Create the qualified type name to access
string typeName = string.Format( "{0}.{1}", assembly, key );
// Load the string resource access class
Type type = Type.GetType( typeName );
// Get information about the arguments (if any)
ParameterInfo[] paramInfo = type.GetMethod( "GetString").GetParameters();
// Create an array of arguments
Object[] args = new Object[paramInfo.Length];
// Supply the argument names as arguments when we call GetString
for( int i = 0; i < paramInfo.Length; i++ )
{
args[i] = paramInfo[i].Name;
}
// We know the GetString method is both public and static
BindingFlags bf = BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod;
// Call the GetString method
string result = (string)type.InvokeMember( "GetString", bf, null, null, args );
// Display the result
label3.Text = result;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Corey Frost is a software engineer with the Noetix Corporation (www.noetix.com). Prior to that he spent 10 years as a contract developer specializing in Windows applications development using C++ and MFC.