如:public class HtmlInput 

    string _att1 = "123"; 
    string -att2 = "2";
    string -att3 = "1";
    string -att4 = "A";
    ....
    ....
    string -att1000 = "Z";    public override string ToString() 
    {
       System.Reflection.PropertyInfo p = this.GetType().??? 
      //通过反射取得当前私有属性_Type的值       //本来我是想能过反射私有属性然后进行合并的,
      //但是问了说私有属性反射不了,
      //那么像上面我这个个类的这么多私有属性,我要合并他们的值输出怎么办呢?
      //难道就只能是 _att1 + _att2 + _att3 .... + _att1000 ????
    } 
} 谢谢

解决方案 »

  1.   

    1、谁说不能反射私有成员?
    2、_att1 + _att2 + _att3 .... + _att1000是个比反射好的方法。
      

  2.   

    用 FieldInfo之前那个帖子  
    gomoku 有回复的
      

  3.   

    2、_att1 + _att2 + _att3 .... + _att1000是个比反射好的方法。
    ----------------------------------------------------------
    uppublic override string ToString() 
        {
           System.Reflection.PropertyInfo p = this.GetType().??? 
          //通过反射取得当前私有属性_Type的值       //本来我是想能过反射私有属性然后进行合并的,
          //但是问了说私有属性反射不了,
          //那么像上面我这个个类的这么多私有属性,我要合并他们的值输出怎么办呢?
          //难道就只能是 _att1 + _att2 + _att3 .... + _att1000 ????
        } 因为 你这段代码 本身就在class ,为什么还要反射获得
      

  4.   

    使用 FieldInfo 了解以下信息:
    字段的名称、访问修饰符(如 public 或 private)和实现详细信息(如 static)等;
    并获取或设置字段值
      

  5.   

    如果有1000个成员,一般用属性包(Property Bag)的方式来存放数据。
    比如用一个List或Dictionary来放:
    "_att1" ... "123"
    "_att2" ... "2"
    "_att3" ... "1"很少直接定义1000个成员的。如果你真的定义了1000个成员,你也不在乎多写一个长长的ToString方法:)
    而且用反射来做,难于进行准确控制(比如顺序等)。
      

  6.   


    MyClass mc = new MyClass();
    Type t = typeof(MyClass);
    BindingFlags bf = BindingFlags.Instance | BindingFlags.NonPublic;// Fields
    FieldInfo fi_protected = t.GetField("ProtectedField",bf);
    FieldInfo fi_private = t.GetField("PrivateField",bf);Console.WriteLine(fi_protected.GetValue(mc));
    Console.WriteLine(fi_private.GetValue(mc));
    fi_private.SetValue(mc,"New Private Field");
    Console.WriteLine(fi_private.GetValue(mc));Console.WriteLine();// Properties
    PropertyInfo pi_protected = t.GetProperty("ProtectedProperty", bf);
    PropertyInfo pi_private = t.GetProperty("PrivateProperty", bf);Console.WriteLine(pi_protected.GetValue(mc,null));
    Console.WriteLine(pi_private.GetValue(mc,null));
    pi_private.SetValue(mc,"New Private Property",null);
    Console.WriteLine(pi_private.GetValue(mc,null));Console.WriteLine();// Methods
    MethodInfo mi_protected = t.GetMethod("ProtectedMethod", bf);
    MethodInfo mi_private = t.GetMethod("PrivateMethod", bf);mi_protected.Invoke(mc,null);
    mi_private.Invoke(mc,null);Console.ReadLine();输出:
    Protected Field
    Private Field
    New Private FieldProtected Property
    Private Property
    New Private PropertyProtected Method Invoked
    Private Method Invoked
    本文来源于:古之娇子.焉客 http://www.dh2019.cn/  原文地址:http://www.dh2019.cn/post/197.html 晚上找的,反射私有的,不知道对不
      

  7.   


    class MyClass
    {
    private string PrivateField = "Private Field";
    protected string ProtectedField = "Protected Field";
    private string _ProtectedProperty = "Protected Property";
    protected string ProtectedProperty
    {
    get{return _ProtectedProperty;}
    set{_ProtectedProperty = value;}
    }
    private string _PrivateProperty = "Private Property";
    private string PrivateProperty
    {
    get{return _PrivateProperty;}
    set{_PrivateProperty = value;}
    }
    protected void ProtectedMethod()
    {
    Console.WriteLine("Protected Method Invoked");
    }
    private void PrivateMethod()
    {
    Console.WriteLine("Private Method Invoked");
    }
    }
    除了默认的构造函数,没有任何成员是公开的,但是我仍然想获取和设置Field和Property的值,以及调用那两个方法。方法是:
    MyClass mc = new MyClass();
    Type t = typeof(MyClass);
    BindingFlags bf = BindingFlags.Instance | BindingFlags.NonPublic;// Fields
    FieldInfo fi_protected = t.GetField("ProtectedField",bf);
    FieldInfo fi_private = t.GetField("PrivateField",bf);Console.WriteLine(fi_protected.GetValue(mc));
    Console.WriteLine(fi_private.GetValue(mc));
    fi_private.SetValue(mc,"New Private Field");
    Console.WriteLine(fi_private.GetValue(mc));Console.WriteLine();// Properties
    PropertyInfo pi_protected = t.GetProperty("ProtectedProperty", bf);
    PropertyInfo pi_private = t.GetProperty("PrivateProperty", bf);Console.WriteLine(pi_protected.GetValue(mc,null));
    Console.WriteLine(pi_private.GetValue(mc,null));
    pi_private.SetValue(mc,"New Private Property",null);
    Console.WriteLine(pi_private.GetValue(mc,null));Console.WriteLine();// Methods
    MethodInfo mi_protected = t.GetMethod("ProtectedMethod", bf);
    MethodInfo mi_private = t.GetMethod("PrivateMethod", bf);mi_protected.Invoke(mc,null);
    mi_private.Invoke(mc,null);Console.ReadLine();输出:
    Protected Field
    Private Field
    New Private FieldProtected Property
    Private Property
    New Private PropertyProtected Method Invoked
    Private Method Invoked
    事件,一样可以操作, EventInfo :-)
    本文来源于:古之娇子.焉客 http://www.dh2019.cn/  原文地址:http://www.dh2019.cn/post/197.html 网上找的