2

可能重复:
使用 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(); 您将获得一个包含所有已定义字符串字段的数据表。但是,除了字符串字段的名称之外,我无法检索字符串字段的属性。如何实例化找到的 stringField 以便检索其他属性?

谢谢,

4

3 回答 3

0

如果您有字符串字段名称,请尝试以下操作:

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

摘自此 SO 帖子:

在 C# 中使用反射从字符串中获取属性值

于 2012-11-20T08:34:44.000 回答
0

使用Type.GetProperties方法获取类型的所有公共属性

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

还有可以指定 BindingFlags 的重载。

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

object value = property.GetValue(_ClientEmail);
于 2012-11-20T08:33:23.207 回答
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);

    ...
于 2012-11-20T08:40:21.240 回答