Evaluation Problem

1

100

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.Reflection.FieldInfo fi =

Type.GetType("ConsoleApplication4.reflection") .GetField("mstr");

string temp = (string)fi.GetValue(null);

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

This is ok


/////////// THIS IS NOT WORKING \\\\\\\\\\\
Type.GetType("ConsoleApplication4.reflection") .GetField("textbox1");

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.GetValue("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("ConsoleApplication4.reflection") ..GetField("textBox1");

//Alternatives for obtaining type object :

//typeof(ConsoleApplication4.reflection)

//r.GetType();

TextBox tb = (TextBox))fi.GetValue(r);

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



HTH
B\rgds
100
 
I

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

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("ConsoleApplication4.reflection") ..GetField("textBox1") ;

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("Text") ;


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("ConsoleApplication4.reflection") ..GetField("textBox1").GetType() .GetProperty("Text").GetValue( textBox1, null).ToString();



Hope this help,

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

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

Please help.........

using System;

namespace ConsoleApplication4

{

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.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(64)), ((System.Byte)(0)));

this.textBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

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

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

this.textBox1.Name = "textBox1";

this.textBox1.TabIndex = 12;

this.textBox1.Text = "textBox1";

this.Controls.Add(this.textBox1);





System.Reflection.FieldInfo fi =

Type.GetType("ConsoleApplication4.reflection") .GetField("mstr");

string temp = (string)fi.GetValue(null);

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

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

Type.GetType("ConsoleApplication4.reflection") .GetField("textbox1");

string temp = (string)fi.GetValue("Text");

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

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

ERROR Object Type cannot be converted to target type

}

}

}
 
I

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

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.machin AT dot.state.fl.us> wrote in message 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("ConsoleApplication4.reflection") ..GetField("textBox1") ;

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("Text") ;


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("ConsoleApplication4.reflection") ..GetField("textBox1").GetType() .GetProperty("Text").GetValue( textBox1, null).ToString();



Hope this help,

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

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

Please help.........

using System;

namespace ConsoleApplication4

{

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.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(64)), ((System.Byte)(0)));

this.textBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

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

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

this.textBox1.Name = "textBox1";

this.textBox1.TabIndex = 12;

this.textBox1.Text = "textBox1";

this.Controls.Add(this.textBox1);





System.Reflection.FieldInfo fi =

Type.GetType("ConsoleApplication4.reflection") .GetField("mstr");

string temp = (string)fi.GetValue(null);

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

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

Type.GetType("ConsoleApplication4.reflection") ..GetField("textbox1");

string temp = (string)fi.GetValue("Text");

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

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

ERROR Object Type cannot be converted to target type

}

}

}
 
N

Nicolas

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

Please help.........

using System;

namespace ConsoleApplication4

{

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.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(64)), ((System.Byte)(0)));

this.textBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

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

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

this.textBox1.Name = "textBox1";

this.textBox1.TabIndex = 12;

this.textBox1.Text = "textBox1";

this.Controls.Add(this.textBox1);





System.Reflection.FieldInfo fi =

Type.GetType("ConsoleApplication4.reflection") .GetField("mstr");

string temp = (string)fi.GetValue(null);

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

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

Type.GetType("ConsoleApplication4.reflection") .GetField("textbox1");

string temp = (string)fi.GetValue("Text");

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

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

ERROR Object Type cannot be converted to target type

}

}

}
 
N

Nicolas

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(object sender, System.EventArgs e)

{


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

inTouch.Tools.Property.GetControlProperties getProp = new inTouch.Tools.Property.GetControlProperties(this, this.Button1,"BackColor","ffffff80");

}



USERCONTROL DLL
namespace inTouch.Tools.Property

{

public class GetControlProperties

{

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

{

// I assume here that this is BackColor

// then I receive a color for myPropValue

System.Reflection.FieldInfo fi = Type.GetType(myForm.GetType().ToString()).GetField(myControl.ToString()) ;

object o = fi.GetValue(myControl);

<<<<<<

System.Reflection.PropertyInfo pi = o.GetType().GetProperty(myProperty);

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

MessageBox.Show(text.ToString());

// NOW Set the new Property

o.GetType().GetProperty(myProperty).SetValue(o,Color.FromArgb((int)myPropValue), null);

}

}

}











"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote in message 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("ConsoleApplication4.reflection") ..GetField("textBox1") ;

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("Text") ;


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("ConsoleApplication4.reflection") ..GetField("textBox1").GetType() .GetProperty("Text").GetValue( textBox1, null).ToString();



Hope this help,

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

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

Please help.........

using System;

namespace ConsoleApplication4

{

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.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(64)), ((System.Byte)(0)));

this.textBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

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

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

this.textBox1.Name = "textBox1";

this.textBox1.TabIndex = 12;

this.textBox1.Text = "textBox1";

this.Controls.Add(this.textBox1);





System.Reflection.FieldInfo fi =

Type.GetType("ConsoleApplication4.reflection") .GetField("mstr");

string temp = (string)fi.GetValue(null);

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

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

Type.GetType("ConsoleApplication4.reflection") ..GetField("textbox1");

string temp = (string)fi.GetValue("Text");

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

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

ERROR Object Type cannot be converted to target type

}

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top