Exposing delegate through properties in C#
How can I expose delegate through via a property? I am exposing a delegate which is a member of a third party class through my class.
public CardInformationAvailable 开发者_开发知识库OnDataRecieve //CardInformationAvailable is a delegate type
{
set
{
_cardReaderBase.OnDataReady += value; // OnDataReady is a delegate of type
//CardInformationAvailable
// Where will i call -=value?
}
}
Not 100% sure on the question, but you can wrap events in this way if you want to expose via your class. But as others have mentioned, you could just add the event directly to _cardReaderBase?
public CardReader
{
public event OnDataReady;
private CardReaderBase _cardReaderBase;
public event OnDataReady OnDataReadyEvent
{
add
{
_cardReaderBase.OnDataReady += value;
}
remove
{
_cardReaderBase.OnDataReady -= value;
}
}
}
You can put like if
condition for Null parameter. In other words somethign like this:
public CardInformationAvailable OnDataRecieve
{
set
{
if(value == null)
_cardReaderBase.OnDataReady -= value;
else
_cardReaderBase.OnDataReady += value;
}
}
精彩评论