Delegate Gurus: Can I create a delegate for a property (as opposed to a method)?

B

Bill Davidson

All:

I'd like to create a delegate for a property (as opposed to a method). Can
I do this? If so, please teach me the syntactic magic.

For example, suppose I have a boolean ready-only property defined in an
instance of a class name SomeObj as follows:

public bool TheValue
{
get
{
// do some work
return someBoolValue;
}
}

I've tried the following two approaches to creating the delegate but
received compilation errors:

private delegate bool BoolDelegate();

// The following results in compilation error: error CS0118: denotes a
'property' where a 'method' was expected
BoolDelegate del = new BoolDelegate(SomeObj.TheValue);

// The following results in compilation error: error CS0571: cannot
explicitly call operator or accessor
BoolDelegate del = new BoolDelegate(SomeObj.get_TheValue);

Any other ideas?

TIA,
Bill
 
P

Paul E Collins

Bill Davidson said:
I'd like to create a delegate for a property (as
opposed to a method). Can I do this?

Delegates only work for methods, *but* if you don't mind losing some
run-time type safety, you can achieve what you want using reflection.
The PropertyInfo class represents a property, and its SetValue method
will change the value.

What makes you want this feature? There might be a neater solution.

P.
 
B

Bill Davidson

Paul:

Our .Net app calls into a 3rd-party .Net library to obtain the value of a
certain property. For whatever reason, sometimes when we access this
property it just 'hangs' (doesn't return in any reasonable timeframe). It
isn't absolutely essential that we have the value of this property; however
is *is* essential that we don't hang our app while waiting on this @#%!
property to return.

I'd like to create a delegate and invoke this property asynchronously (e.g.
BeginInvoke), so that we can move on if the property doesn't return in a
reasonable timeframe.

Bill
 
K

Kevin Yu [MSFT]

Hi Bill,

According to C# language specification, The declaration of a delegate type
establishes a contract that specifies the signature of one or more methods.
A delegate is an instance of a delegate type, and references one or more of
the following:

1. A target object that is not a null reference (Nothing in Visual Basic)
and an instance method of the target object
2. A static method

So property cannot be used. I suggest you contact the library vendor to
resolved the blocking issue instead of calling it using a delegate.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
D

Daniel O'Connell [C# MVP]

Bill Davidson said:
Paul:

Our .Net app calls into a 3rd-party .Net library to obtain the value of a
certain property. For whatever reason, sometimes when we access this
property it just 'hangs' (doesn't return in any reasonable timeframe). It
isn't absolutely essential that we have the value of this property;
however is *is* essential that we don't hang our app while waiting on this
@#%! property to return.

I'd like to create a delegate and invoke this property asynchronously
(e.g. BeginInvoke), so that we can move on if the property doesn't return
in a reasonable timeframe.

Wouldn't the simple answer just be to write a short method that sets the
value and invoke *that* method asynchronously?
 
G

Guest

Daniel O'Connell said:
Wouldn't the simple answer just be to write a short method that sets the
value and invoke *that* method asynchronously?

my choice will also be what Daniel has suggested. I think this approach is
much cleaner and simple.
 
B

Bill Davidson

Rahul (and Daniel):

I like Daniel's idea as well. 'Should work like a champ. Thanks Daniel
and to all of those who replied.

Happy Coding,
Bill
 

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