C#通过反射检索属性

时间:2012-11-20 08:29:48

标签: c# reflection instantiation

  

可能重复:
  Get property value from string using reflection in C#

我有一个应用程序将数据库中的字段合并为电子邮件和字母。由于有不同的用户,他们要求合并不同的字段,并且当我创建新的合并域时,文档也必须重新编写。这会产生问题所以我想自动化文档。

我想出了以下代码(在这个例子中,我只定义了2个合并域,称为stringField,但目前它只有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; }
        }

    }
}

如果触发指令:System.Data.DataTable dtTest = FindStringFields.stringFields(); 你得到一个包含所有已定义的stringFields的数据表。 但是,除了stringfield的名称,我无法检索字符串字段的属性。 如何实例化找到的stringField以便我可以检索其他属性?

谢谢,

罗布

3 个答案:

答案 0 :(得分:0)

使用Type.GetProperties方法获取类型

的所有公共属性
Type type = typeof(stringField);
PropertyInfo[] properties = type.GetProperties();

还有超载可以指定BindingFlags。

然后你可以获得属性的值:

object value = property.GetValue(_ClientEmail);

答案 1 :(得分:0)

如果您有字符串字段名称,请尝试使用:

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

从这篇SO帖子中摘录:

Get property value from string using reflection in C#

答案 2 :(得分:0)

您可以这样做:

   //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);

    ...