Calling properties using Invoke

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

I wish to set/get a property using System.ComponentModel.ISynchronizeInvoke.Invoke but I cannot find the exact syntax..

Any clue how to do this
(right now I am making GetEnabled/SetEnabled methods when I wish to get/set Form.Enabled property.

Thank
JPRoo
 
Hi,

ISynchronizeInvoke.Invoke gives you a way of calling a method from a
different thread to marshal the call to the proper thread.
To use this method, you need create a delegate for your calling method.
For more information, there is an article talks about this:
http://www.fawcette.com/vsm/2003_02/magazine/columns/qa/default_pf.aspx

Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Thanks for quick reply,

My question was how can I get/set a property using Invoke?

I already use Invoke with methods, but I haven't found the syntax to get/set properties...

Is it possible?

Ex: I would to set to 'True' the property 'myForm.Enable' with myForm.Invoke, how do I do it?

Thanks
JPRoot


----- \"Jeffrey Tan[MSFT]\" wrote: -----


Hi,

ISynchronizeInvoke.Invoke gives you a way of calling a method from a
different thread to marshal the call to the proper thread.
To use this method, you need create a delegate for your calling method.
For more information, there is an article talks about this:
http://www.fawcette.com/vsm/2003_02/magazine/columns/qa/default_pf.aspx

Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
I'm not sure what ISynchronizeInvoke does for you, but you can get/set
properties using reflection if that's all you want to do.

Say you have your Form object named form1, you can do the following:

public void SetFormEnabled( bool enabled )
{
System.Reflection.PropertyInfo pInfo =
form1.GetType().GetProperty("Enabled");
pInfo.SetValue( form1, enabled, null );
}
 
Jeffery,

regardless of the solutions you come up with to invoke properties, isnt it
good practice to provide access methods to members anyway (since not all
..NET languages support properties like VB.NET)?

Picho
 
Picho said:
regardless of the solutions you come up with to invoke properties, isnt it
good practice to provide access methods to members anyway (since not all
.NET languages support properties like VB.NET)?

I don't think there will be any .NET languages which don't support
properties in some sense or other - it would just be impossible to use
most of the framework libraries, which only provide properties to get
at various things.
 
=?Utf-8?B?SlBSb290?= said:
My question was how can I get/set a property using Invoke?

I already use Invoke with methods, but I haven't found the syntax to
get/set properties...

You could always use PropertyInfo.GetGetMethod and
PropertyInfo.GetSetMethod, and then invoke those methods.

Alternatively, use PropertyInfo.SetValue and PropertyInfo.GetValue.
 
Picho said:
Isnt this the situation now with VB.NET?

thats what I read anyway...

No, VB.NET can use properties - what makes you think it can't?

For instance, here's the sample program in VB.NET for String.Length
from MSDN:

' Sample for String.Length
Imports System

Class Sample
Public Shared Sub Main()
Dim str As String = "abcdefg"
Console.WriteLine("1) The length of '{0}' is {1}", str, str.Length)
Console.WriteLine("2) The length of '{0}' is {1}", "xyz", "xyz".Length)
End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'1) The length of 'abcdefg' is 7
'2) The length of 'xyz' is 3

Note the use of str.Length and "xyz".Length.

Were you perhaps thinking of operator overloading instead?
 
I am sorry,
Probably my mix-up...

Picho

Jon Skeet said:
No, VB.NET can use properties - what makes you think it can't?

For instance, here's the sample program in VB.NET for String.Length
from MSDN:

' Sample for String.Length
Imports System

Class Sample
Public Shared Sub Main()
Dim str As String = "abcdefg"
Console.WriteLine("1) The length of '{0}' is {1}", str, str.Length)
Console.WriteLine("2) The length of '{0}' is {1}", "xyz", "xyz".Length)
End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'1) The length of 'abcdefg' is 7
'2) The length of 'xyz' is 3

Note the use of str.Length and "xyz".Length.

Were you perhaps thinking of operator overloading instead?
 
Hi Picho,

VB.net supports set and get accessor of property.
Actually, the get and set accessors behave the same as methods.
To use get and set accessors instead of creating a special method to access
members, this gives you a more consistent way of solution.
So I recommand JProot to combine Invoke PropertyInfo.GetGetMethod and
PropertyInfo.GetSetMethod to get this done.

Best regards,
Jeffrey Tan
Microsoft Support Engineer
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Thanks Jon,
It help me figure out the internal name of the property "set_yyy"/"get_zzz"(by inspecting GetGetMethod.Name).

here's the solution I have found the most optimal... I would have like something more strongly typed but... hey... it works! :)

private delegate string GetStringDel();
private delegate void SetStringDel(string text);
static void MyMethod()
{
System.Delegate myDel = System.Delegate.CreateDelegate( typeof(SetStringDel), frm, "set_Text", true );
frm.Invoke( myDel, new object[]{"JPRoot"} );

myDel = System.Delegate.CreateDelegate( typeof(GetStringDel), frm, "get_Text", true );
string textContent = (string)frm.Invoke( myDel, null );
System.Console.WriteLine( textContent );
}

Thanks! Appreciated...
JPRoot

----- Jon Skeet [C# MVP] wrote: -----

=?Utf-8?B?SlBSb290?= said:
My question was how can I get/set a property using Invoke?
get/set properties...

You could always use PropertyInfo.GetGetMethod and
PropertyInfo.GetSetMethod, and then invoke those methods.

Alternatively, use PropertyInfo.SetValue and PropertyInfo.GetValue.
 
Back
Top