Evaluation Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nicolas

    Evaluation Problem

    The lat part is not working why ????????

    Please help.........

    using System;

    namespace ConsoleApplicat ion4

    {

    class reflection

    {



    public const string mstr = "hellpppo world!";

    public System.Windows. Forms.TextBox textBox1;

    [STAThread]

    static void Main(string[] args)

    {

    this.textBox1 = new System.Windows. Forms.TextBox() ;

    //

    // textBox1

    //

    this.textBox1.B ackColor = System.Drawing. Color.FromArgb( ((System.Byte)( 128)), ((System.Byte)( 64)), ((System.Byte)( 0)));

    this.textBox1.F ont = new System.Drawing. Font("Microsoft Sans Serif", 12F, System.Drawing. FontStyle.Regul ar, System.Drawing. GraphicsUnit.Po int, ((System.Byte)( 0)));

    this.textBox1.F oreColor = System.Drawing. Color.FromArgb( ((System.Byte)( 192)), ((System.Byte)( 192)), ((System.Byte)( 0)));

    this.textBox1.L ocation = new System.Drawing. Point(24, 48);

    this.textBox1.N ame = "textBox1";

    this.textBox1.T abIndex = 12;

    this.textBox1.T ext = "textBox1";

    this.Controls.A dd(this.textBox 1);





    System.Reflecti on.FieldInfo fi =

    Type.GetType("C onsoleApplicati on4.reflection" ) .GetField("mstr ");

    string temp = (string)fi.GetV alue(null);

    MessageBox.Show ("get string {0}", temp);

    /////////// THIS IS NOT WORKING \\\\\\\\\\\

    Type.GetType("C onsoleApplicati on4.reflection" ) .GetField("text box1");

    string temp = (string)fi.GetV alue("Text");

    MessageBox.Show ("get string {0}", temp);

    \\\\\\\\\\\\\\\ \\\\\\\\\\\\\\\ \\/////////////////////////////////

    ERROR Object Type cannot be converted to target type

    }

    }

    }

  • 100

    #2
    Re: Evaluation Problem

    1. This program shouldn't work at all.
    *Main* is static method and there is no *this*.

    2.Beside my first remark if we look at the reflection usage:
    System.Reflecti on.FieldInfo fi =

    Type.GetType("C onsoleApplicati on4.reflection" ) .GetField("mstr ");

    string temp = (string)fi.GetV alue(null);

    MessageBox.Show ("get string {0}", temp);

    This is ok


    /////////// THIS IS NOT WORKING \\\\\\\\\\\
    Type.GetType("C onsoleApplicati on4.reflection" ) .GetField("text box1");

    You have forgotten *fi = ....*. You are using FieldInfo for the *mstr* field.
    BTW this should return *null* since IFAIK the default binding is case sensitive. You shoud use "textBox1";
    string temp = (string)fi.GetV alue("Text");

    You are trying ot get a value for *mstr* field of object of type String. String doesn't have this field. GetValue exepects object which field value will be returned. The type of this object has to be type that declares or inhertis the field. String doesn't declares the field nor inherits from reflection class. This is the exception that you've got.

    The correct code should look something like the following:
    reflection r = new reflection();

    FiledInfo fi = Type.GetType("C onsoleApplicati on4.reflection" ) ..GetField("tex tBox1");

    //Alternatives for obtaining type object :

    //typeof(ConsoleA pplication4.ref lection)

    //r.GetType();

    TextBox tb = (TextBox))fi.Ge tValue(r);

    MessageBox.Show ("get string {0}", tb.Text);



    HTH
    B\rgds
    100

    Comment

    • Ignacio Machin \( .NET/ C#  MVP \)

      #3
      Re: Evaluation Problem

      Hi again Nicolas,

      Type.GetField( ) expect the field name, the field name is "Text" , "textBox1" is the variable containing the instance of TextBox.
      therefore the correct construction is:
      FieldInfo fi = Type.GetType("C onsoleApplicati on4.reflection" ) ..GetField("tex tBox1") ;

      This line return a FieldInfo instance, now to get the value of that instance you could use:
      object o = fi.GetValue( this);

      now you have the value of the field ( textBox1 ) for a particular instance ( this )

      to get the value of the property "Text" of it you have to do something similar:
      PropertyInfo pi = o.GetType() .GetProperty("T ext") ;


      And finally evaluate it:

      string text = pi.GetValue( textBox1, null).ToString( );


      Hope you understand it, otherwise just post back ;)

      In short you could do this:
      string text = Type.GetType("C onsoleApplicati on4.reflection" ) ..GetField("tex tBox1").GetType () .GetProperty("T ext").GetValue ( textBox1, null).ToString( );



      Hope this help,

      --
      Ignacio Machin,
      ignacio.machin AT dot.state.fl.us
      Florida Department Of Transportation

      message news:OPxZLMpjDH A.2216@TK2MSFTN GP12.phx.gbl...
      The lat part is not working why ????????

      Please help.........

      using System;

      namespace ConsoleApplicat ion4

      {

      class reflection

      {



      public const string mstr = "hellpppo world!";

      public System.Windows. Forms.TextBox textBox1;

      [STAThread]

      static void Main(string[] args)

      {

      this.textBox1 = new System.Windows. Forms.TextBox() ;

      //

      // textBox1

      //

      this.textBox1.B ackColor = System.Drawing. Color.FromArgb( ((System.Byte)( 128)), ((System.Byte)( 64)), ((System.Byte)( 0)));

      this.textBox1.F ont = new System.Drawing. Font("Microsoft Sans Serif", 12F, System.Drawing. FontStyle.Regul ar, System.Drawing. GraphicsUnit.Po int, ((System.Byte)( 0)));

      this.textBox1.F oreColor = System.Drawing. Color.FromArgb( ((System.Byte)( 192)), ((System.Byte)( 192)), ((System.Byte)( 0)));

      this.textBox1.L ocation = new System.Drawing. Point(24, 48);

      this.textBox1.N ame = "textBox1";

      this.textBox1.T abIndex = 12;

      this.textBox1.T ext = "textBox1";

      this.Controls.A dd(this.textBox 1);





      System.Reflecti on.FieldInfo fi =

      Type.GetType("C onsoleApplicati on4.reflection" ) .GetField("mstr ");

      string temp = (string)fi.GetV alue(null);

      MessageBox.Show ("get string {0}", temp);

      /////////// THIS IS NOT WORKING \\\\\\\\\\\

      Type.GetType("C onsoleApplicati on4.reflection" ) .GetField("text box1");

      string temp = (string)fi.GetV alue("Text");

      MessageBox.Show ("get string {0}", temp);

      \\\\\\\\\\\\\\\ \\\\\\\\\\\\\\\ \\/////////////////////////////////

      ERROR Object Type cannot be converted to target type

      }

      }

      }

      Comment

      • Ignacio Machin \( .NET/ C#  MVP \)

        #4
        Re: Evaluation Problem

        Hi,


        I just noted that you are doing all this inside the main() method of a console application , this introduce a few others things:

        1- the main method is declared as static, therefore there is no instance associated, you cannot access textBox1 as it's not marked as static.
        2- If you want to create a windows application to this this, you need to include the System.Windows. Forms namespace and inside the main method make this call:

        Application.Run (new Form1());



        This create a new instance of the form you need and create the message loop to receive events.



        The code you are written you could write it in the Load event handler of the form.



        Sorry for not reading well your original post, long time without caffeine I think :)

        but the explanatio of how to get the field and later evaluate a property of it is still correct.

        Cheers,


        --
        Ignacio Machin,
        ignacio.machin AT dot.state.fl.us
        Florida Department Of Transportation

        "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote in message news:Oh%232Cspj DHA.2656@TK2MSF TNGP10.phx.gbl. ..
        Hi again Nicolas,

        Type.GetField( ) expect the field name, the field name is "Text" , "textBox1" is the variable containing the instance of TextBox.
        therefore the correct construction is:
        FieldInfo fi = Type.GetType("C onsoleApplicati on4.reflection" ) ..GetField("tex tBox1") ;

        This line return a FieldInfo instance, now to get the value of that instance you could use:
        object o = fi.GetValue( this);

        now you have the value of the field ( textBox1 ) for a particular instance ( this )

        to get the value of the property "Text" of it you have to do something similar:
        PropertyInfo pi = o.GetType() .GetProperty("T ext") ;


        And finally evaluate it:

        string text = pi.GetValue( textBox1, null).ToString( );


        Hope you understand it, otherwise just post back ;)

        In short you could do this:
        string text = Type.GetType("C onsoleApplicati on4.reflection" ) ..GetField("tex tBox1").GetType () .GetProperty("T ext").GetValue ( textBox1, null).ToString( );



        Hope this help,

        --
        Ignacio Machin,
        ignacio.machin AT dot.state.fl.us
        Florida Department Of Transportation

        message news:OPxZLMpjDH A.2216@TK2MSFTN GP12.phx.gbl...
        The lat part is not working why ????????

        Please help.........

        using System;

        namespace ConsoleApplicat ion4

        {

        class reflection

        {



        public const string mstr = "hellpppo world!";

        public System.Windows. Forms.TextBox textBox1;

        [STAThread]

        static void Main(string[] args)

        {

        this.textBox1 = new System.Windows. Forms.TextBox() ;

        //

        // textBox1

        //

        this.textBox1.B ackColor = System.Drawing. Color.FromArgb( ((System.Byte)( 128)), ((System.Byte)( 64)), ((System.Byte)( 0)));

        this.textBox1.F ont = new System.Drawing. Font("Microsoft Sans Serif", 12F, System.Drawing. FontStyle.Regul ar, System.Drawing. GraphicsUnit.Po int, ((System.Byte)( 0)));

        this.textBox1.F oreColor = System.Drawing. Color.FromArgb( ((System.Byte)( 192)), ((System.Byte)( 192)), ((System.Byte)( 0)));

        this.textBox1.L ocation = new System.Drawing. Point(24, 48);

        this.textBox1.N ame = "textBox1";

        this.textBox1.T abIndex = 12;

        this.textBox1.T ext = "textBox1";

        this.Controls.A dd(this.textBox 1);





        System.Reflecti on.FieldInfo fi =

        Type.GetType("C onsoleApplicati on4.reflection" ) .GetField("mstr ");

        string temp = (string)fi.GetV alue(null);

        MessageBox.Show ("get string {0}", temp);

        /////////// THIS IS NOT WORKING \\\\\\\\\\\

        Type.GetType("C onsoleApplicati on4.reflection" ) ..GetField("tex tbox1");

        string temp = (string)fi.GetV alue("Text");

        MessageBox.Show ("get string {0}", temp);

        \\\\\\\\\\\\\\\ \\\\\\\\\\\\\\\ \\/////////////////////////////////

        ERROR Object Type cannot be converted to target type

        }

        }

        }

        Comment

        • Nicolas

          #5
          Re: Evaluation Problem

          Thank you but I still have some problem as I explain here.
          1 application with form1
          1 dll which is holding some usercontrol etc...
          The idea is to use the user control dll to alter the Form1 controls
          When Form1 load then I call and initialize my usercontrol dll
          First I notice that the controls have too be public and not private >>> is that correct?
          Can this work with private or do I have to change all the default setting of every control to public?
          Second it look like I dont really have an handle of the source class (Form1 from the usercontrol dll) >>>>

          When this is run within the form1_Load all together it works fine, but not when the code reside into the dll.
          It must be something very stupid that I'm doing here. But I'm loosing my hair way too fast on that one.
          Thank for not letting going hairless.

          I marked the code where it stop working.

          FORM1
          private void Form1_Load(obje ct sender, System.EventArg s e)

          {


          // The control Button1 is well set in the Form1 has it is used correctly.

          inTouch.Tools.P roperty.GetCont rolProperties getProp = new inTouch.Tools.P roperty.GetCont rolProperties(t his, this.Button1,"B ackColor","ffff ff80");

          }



          USERCONTROL DLL
          namespace inTouch.Tools.P roperty

          {

          public class GetControlPrope rties

          {

          public GetControlPrope rties(Form myForm, Control myControl , string myProperty, object myPropValue)

          {

          // I assume here that this is BackColor

          // then I receive a color for myPropValue

          System.Reflecti on.FieldInfo fi = Type.GetType(my Form.GetType(). ToString()).Get Field(myControl .ToString()) ;
          [color=blue][color=green][color=darkred]
          >>>>> STOP HERE WITH THIS ERROR (Description: Object reference not set to an instance of an object.)[/color][/color][/color]

          object o = fi.GetValue(myC ontrol);

          <<<<<<

          System.Reflecti on.PropertyInfo pi = o.GetType().Get Property(myProp erty);

          string text = pi.GetValue(myC ontrol, null).ToString( );

          MessageBox.Show (text.ToString( ));

          // NOW Set the new Property

          o.GetType().Get Property(myProp erty).SetValue( o,Color.FromArg b((int)myPropVa lue), null);

          }

          }

          }











          "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote in message news:Oh%232Cspj DHA.2656@TK2MSF TNGP10.phx.gbl. ..
          Hi again Nicolas,

          Type.GetField( ) expect the field name, the field name is "Text" , "textBox1" is the variable containing the instance of TextBox.
          therefore the correct construction is:
          FieldInfo fi = Type.GetType("C onsoleApplicati on4.reflection" ) ..GetField("tex tBox1") ;

          This line return a FieldInfo instance, now to get the value of that instance you could use:
          object o = fi.GetValue( this);

          now you have the value of the field ( textBox1 ) for a particular instance ( this )

          to get the value of the property "Text" of it you have to do something similar:
          PropertyInfo pi = o.GetType() .GetProperty("T ext") ;


          And finally evaluate it:

          string text = pi.GetValue( textBox1, null).ToString( );


          Hope you understand it, otherwise just post back ;)

          In short you could do this:
          string text = Type.GetType("C onsoleApplicati on4.reflection" ) ..GetField("tex tBox1").GetType () .GetProperty("T ext").GetValue ( textBox1, null).ToString( );



          Hope this help,

          --
          Ignacio Machin,
          ignacio.machin AT dot.state.fl.us
          Florida Department Of Transportation

          message news:OPxZLMpjDH A.2216@TK2MSFTN GP12.phx.gbl...
          The lat part is not working why ????????

          Please help.........

          using System;

          namespace ConsoleApplicat ion4

          {

          class reflection

          {



          public const string mstr = "hellpppo world!";

          public System.Windows. Forms.TextBox textBox1;

          [STAThread]

          static void Main(string[] args)

          {

          this.textBox1 = new System.Windows. Forms.TextBox() ;

          //

          // textBox1

          //

          this.textBox1.B ackColor = System.Drawing. Color.FromArgb( ((System.Byte)( 128)), ((System.Byte)( 64)), ((System.Byte)( 0)));

          this.textBox1.F ont = new System.Drawing. Font("Microsoft Sans Serif", 12F, System.Drawing. FontStyle.Regul ar, System.Drawing. GraphicsUnit.Po int, ((System.Byte)( 0)));

          this.textBox1.F oreColor = System.Drawing. Color.FromArgb( ((System.Byte)( 192)), ((System.Byte)( 192)), ((System.Byte)( 0)));

          this.textBox1.L ocation = new System.Drawing. Point(24, 48);

          this.textBox1.N ame = "textBox1";

          this.textBox1.T abIndex = 12;

          this.textBox1.T ext = "textBox1";

          this.Controls.A dd(this.textBox 1);





          System.Reflecti on.FieldInfo fi =

          Type.GetType("C onsoleApplicati on4.reflection" ) .GetField("mstr ");

          string temp = (string)fi.GetV alue(null);

          MessageBox.Show ("get string {0}", temp);

          /////////// THIS IS NOT WORKING \\\\\\\\\\\

          Type.GetType("C onsoleApplicati on4.reflection" ) ..GetField("tex tbox1");

          string temp = (string)fi.GetV alue("Text");

          MessageBox.Show ("get string {0}", temp);

          \\\\\\\\\\\\\\\ \\\\\\\\\\\\\\\ \\/////////////////////////////////

          ERROR Object Type cannot be converted to target type

          }

          }

          }

          Comment

          Working...