2

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

Community
  • 1
  • 1
RobRdam
  • 31
  • 6
  • Thanks to everybody who put an effort in answering my problem. The pointer Matias Fidemraizer gave is correct and working! – RobRdam Nov 20 '12 at 09:07

3 Answers3

0

Use Type.GetProperties method to get all public properties of type

Type type = typeof(stringField);
PropertyInfo[] properties = type.GetProperties();

Also there is overload where you can specify BindingFlags.

Then you can get values of properties:

object value = property.GetValue(_ClientEmail);
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
0

If you have the string field name try with this:

public static object GetPropValue( object src, string propName )
 {
     return src.GetType( ).GetProperty( propName ).GetValue( src, null );
 }

Extracted from this SO post:

Get property value from string using reflection in C#

Community
  • 1
  • 1
Carlos Landeras
  • 11,025
  • 11
  • 56
  • 82
  • If it's an exact duplicate, it's better to vote for closing it as it's "an exact duplicate of an existing question" :D – Matías Fidemraizer Nov 20 '12 at 08:35
  • Im not sure if he wanna perform exactly the same. Thanks @Matías Fidemraizer – Carlos Landeras Nov 20 '12 at 08:36
  • Hummm, read questions carefully!! ;) – Matías Fidemraizer Nov 20 '12 at 08:39
  • English is not my native language so I do my best ;). Thanks dude – Carlos Landeras Nov 20 '12 at 08:59
  • It looks as if I didn't do my homework but I googled first, and then created and tested the sample code to ask the question. If I had found the answer myself it would have been quicker! But I thank all who did their best to help me! – RobRdam Nov 20 '12 at 09:09
  • @CarlosLande Mine too. I'm Spanish. But how you answer a question that you don't understand? StackOverflow goes beyond just reading the title of a question and answering something that would sound ok. Don't misunderstand me. I just want to point out that's better to close a question that have been asked before and I'd argue that your effort will be better used in an unanswered question :D – Matías Fidemraizer Nov 20 '12 at 09:17
0

You can do something like this:

   //its better to use a property than get it from the array every time!
   PropertyInfo pi = prop[i]; 

    //get the underlying value
    stringField underlyingStringField = prop.GetValue(test, null) as stringField; 

    //use the underlying value now
    Debug.Write(underlyingStringField.AvalilableEmail);

    ...
Alex Pollan
  • 863
  • 5
  • 13