【问题标题】:C# retrieve properties through reflection [duplicate]C#通过反射检索属性[重复]
【发布时间】:2012-11-20 08:29:48
【问题描述】:

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

谢谢,

罗伯

【问题讨论】:

  • 感谢所有努力回答我的问题的人。 Matias Fidemraizer 给出的指针是正确且有效的!

标签: c# reflection instantiation


【解决方案1】:

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

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

还有可以指定 BindingFlags 的重载。

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

object value = property.GetValue(_ClientEmail);

【讨论】:

    【解决方案2】:

    如果你有字符串字段名试试这个:

    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#

    【讨论】:

    • 如果它是完全重复的,最好投票关闭它,因为它是“现有问题的完全重复”:D
    • 我不确定他是否想表现得完全一样。谢谢@Matías Fidemraizer
    • 嗯,仔细阅读问题!! ;)
    • 英语不是我的母语,所以我尽我所能;)。谢谢老兄
    • 看起来好像我没有做功课但我先google了,然后创建并测试了示例代码以提出问题。如果我自己找到答案会更快!但我感谢所有尽力帮助我的人!
    【解决方案3】:

    你可以这样做:

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

    【讨论】:

      猜你喜欢
      • 2022-11-15
      • 1970-01-01
      • 1970-01-01
      • 2017-01-28
      • 2010-09-22
      • 1970-01-01
      • 1970-01-01
      • 2011-07-19
      • 1970-01-01
      相关资源
      最近更新 更多