我希望了解在C#中,协议和委托的等效方法。
这是我的协议,包括定义协议的接口和类的实现,以及符合协议的类的实现。我想知道在C#中的等效方法,请帮忙解答 :)
/******************************************/
// Communicator Protocol
@class Communicator
@protocol CommunicatorDelegate <NSObject>
@required
- (void)communicator:(Communicator *)communicator receivedData:(NSData *)data;
- (void)fetchingFailedWithError:(NSError *)error;
@optional
- (void)anOptinalMethod;
@end
/******************************************/
// Communicator Class
@protocol CommunicatorDelegate;
@interface Communicator : NSObject
@property (weak, nonatomic) id<CommunicatorDelegate> delegate;
@end
/******************************************/
// Communicator Implementation
@implementation
-(void)someMethodThatFail:(NSError *)error;
{
[self.delegate fetchingFailedWithError:error];
}
- (void)someMethodThatGetData:(NSData *)data;
{
[self.delegate communicator:self receivedData:data];
}
@end
/******************************************/
// Interface of Some Class that conform with the protocol
#import "Communicator.h"
@interface SomeClass : NSObject <CommunicatorDelegate>
@end
/******************************************/
// Implementation of Some Class that conform with the protocol
- (void)communicator:(Communicator *)communicator receivedData:(NSData *)data;
{
// Do something
}
- (void)fetchingFailedWithError:(NSError *)error;
{
// Do something
}