Re: Can I "Invoke" a Property? by Stoitcho
Stoitcho
Wed Feb 11 08:33:33 CST 2004
Hi Stu,
You can do it in IL because properties are not more then two accessor
methods. As a method it can be used with delegates. However, C# doesn't
support delegates for properties. It doesn't mean you can't do it with C#,
though.
Take a look at the code below
using System;
namespace PropertyDelegate
{
delegate int GetIntAccessorDelegate(); //Deleagte for the Get method
delegate void SetIntAccessorDelegate(int value); //Delegate for the Set
method
//Test class
class Foo
{
private int mVal = 10;
public int Val
{
get
{
return mVal;
}
set
{
mVal = value;
}
}
}
class Class1
{
static Foo mFoo = new Foo();
[STAThread]
static void Main(string[] args)
{
//Creating *get* delegate
GetIntAccessorDelegate getDlgt =
(GetIntAccessorDelegate)Delegate.CreateDelegate(typeof(GetIntAccessorDelegat
e), mFoo, "get_Val");
//Creating *set* delegate
SetIntAccessorDelegate setDlgt =
(SetIntAccessorDelegate)Delegate.CreateDelegate(typeof(SetIntAccessorDelegat
e), mFoo, "set_Val");
Console.WriteLine("mFoo.Val(before) = {0}", getDlgt());
setDlgt(100);
Console.WriteLine("mFoo.Val(after) = {0}", getDlgt());
Console.ReadLine();
}
}
}
--
HTH
B\rgds
100
"Stu Smith" <stuarts@remove.digita.com> wrote in message
news:ePTCH7I8DHA.2712@tk2msftngp13.phx.gbl...
> On a related note, I think it's a shame you can't have a delegate to a
> property (either get or set) in C#. (I think you can do it in IL though,
not
> that that helps.)
>
> "babylon" <amuro@hotmail.com> wrote in message
> news:%232i4iw37DHA.2572@TK2MSFTNGP09.phx.gbl...
> > I know how to 'invoke' a method......but now...
> >
> > I have a control that has a property "Selected"
> > I would like to change it from different thread...
> > how can I invoke it?
> >
> > thx
> >
> >
>
>