using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Reflection; namespace LocalizableFormPropertyDiscoverer { // Sample 99.9% based on a sample provided by Christophe Nasarre. Merci! public partial class MainForm : Form { public MainForm() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // get the localizable properties of System.Windows.Forms.Form DumpLocalizableProperties(typeof(Form)); } private void GetLocalizableProperties(Type type, List allProperties) { // get the properties for the type PropertyInfo[] properties = type.GetProperties( BindingFlags.Instance | BindingFlags.Public ); // for each property, look for adorned LocalizableAttribute // if any foreach( PropertyInfo property in properties ) { // check if it is localizable LocalizableAttribute[] attributes = property.GetCustomAttributes(typeof(LocalizableAttribute), true) as LocalizableAttribute[]; // not localizable if this attribute is not here if( attributes == null ) continue; if( attributes.Length == 0 ) continue; // IsLocalizable must be true if( attributes[0].IsLocalizable ) { // add the property if it is not already in if( allProperties.Find( delegate(PropertyInfo p) { return (p.Name.CompareTo(property.Name) == 0); } ) != null ) continue; allProperties.Add(property); } } // concat the properties of its parent if any because // some overriden properties might hide localizable ones if( type.BaseType != null ) GetLocalizableProperties(type.BaseType, allProperties); } private void DumpLocalizableProperties(Type type) { // get the description of all the non-static properties // even protected ones List allProperties = new List(); GetLocalizableProperties(type, allProperties); // sort the properties by name PropertyInfo[] sortedProperties = allProperties.ToArray(); Array.Sort( sortedProperties, delegate(PropertyInfo p1, PropertyInfo p2) { return (String.Compare(p1.Name, p2.Name)); } ); // time to dump foreach( PropertyInfo property in sortedProperties ) { this.listBox1.Items.Add(string.Format("{0} ({1})", property.Name, property.PropertyType.Name)); } } } }