Possible Duplicate:
Get property value from string using reflection in C#
I have an application that merges fields from a database into emails and letters. As there are different users they request different fields to be merged, and as I create a new mergefield the documentation also has to be reworked. This gives problems so I want to automate the documentation.
I came up with the following code (in this example I defined only 2 mergefields, called stringField, but currently it's allmost 100):
namespace ReflectionTest
{
    public class clReflection
    {
        private System.Data.DataTable dtClient = new System.Data.DataTable();
        public int ClientNumber { get; set; }
        public int AdressNumber { get; set; }
        public stringField ClientName
        {
            get
            {
                stringField _ClientName = new stringField();
                _ClientName.FieldContent = "Obama";
                _ClientName.FieldNote = "Last name of client";
                //Available email and available letter should be default true
                return _ClientName; 
            }
            set { }
        }
        public stringField ClientEmail
        {
            get
            {
                stringField _ClientEmail = new stringField();
                _ClientEmail.FieldContent = "obama@whitehouse.gov";
                _ClientEmail.FieldNote = "use only tested email adresses";
                _ClientEmail.AvailableLetter = false;
                return _ClientEmail; 
            }
            set
            { }
        }
    }
    public static class FindStringFields
    {
        public static System.Data.DataTable stringFields()
        {
            System.Data.DataTable dtStringFields = new System.Data.DataTable();
            dtStringFields.Columns.Add(new System.Data.DataColumn("FieldName", typeof(string)));
            dtStringFields.Columns.Add(new System.Data.DataColumn("FieldNote", typeof(string)));
            dtStringFields.Columns.Add(new System.Data.DataColumn("AvailableEmail", typeof(bool))); 
            clReflection test = new clReflection();
            System.Reflection.PropertyInfo[] props = test.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
            for (int i = 0; i < props.Length; i++)
            {
                if (props[i].PropertyType == typeof(stringField))
                {
                    dtStringFields.Rows.Add(new object[] { props[i].Name , "FieldNote", true });
                }
            }
            return dtStringFields;
        }
    }
    public class stringField
    {
        private bool _AvailableEmail = true;
        private bool _AvailableLetter = true;
        public string FieldContent { get; set; }
        public string FieldNote { get; set; }
        public bool AvailableEmail
        {
            get { return _AvailableEmail; }
            set { AvailableEmail = value; }
        }
        public bool AvailableLetter
        {
            get { return _AvailableLetter; }
            set { _AvailableLetter  = value; }
        }
    }
}
If you fire the instruction: System.Data.DataTable dtTest = FindStringFields.stringFields(); you get a datatable with all defined stringFields. However, apart from the name of the stringfield I can't retrieve the properties of a stringfield. How do I instantiate a found stringField so I can retrieve the other properties?
Thanks,
Rob
 
     
     
     
    