我有一个对象Person和一个协议PersonDelegate
Person有一个@property (assign) id<PersonDelegate> delegate;
在我的代码中,我做到了:
Person *newPerson = [[Person alloc] init];
newPerson.delegate = theDelegate;
...
[theDelegate release]; // and dealloc
...
now Person has some new information for the delegate, so I cal
和匿名方法一样,我使用"delegate“关键字声明的委托也是匿名委托吗?
namespace Test
{
public delegate void MyDelegate();
class Program
{
static void Main(string[] args)
{
DelegateTest tst = new DelegateTest();
tst.Chaining();
Console.ReadKey(true);
}
我看到了下面的代码,我不确定为什么它不能编译:
protocol CellDelegate: class {}
protocol DelegatingCellViewModel {
var delegate: CellDelegate? { get }
}
protocol ProductCellViewModelDelegate: CellDelegate {}
// Error: Type 'ProductCellViewModel' does not conform to protocol 'DelegatingCellViewModel'
我在和代表们玩,但我显然没有抓住一些基本的东西。
我有以下几点:
class Test
{
delegate bool Foo();
public void Run()
{
Delegate foo = new Foo((delegate () { return true; }));
bool result = (bool)foo.Method.Invoke(foo, null);
}
}
Test test = new Test();
test.Run();
在上面的示例中,我遇到了一个异常:"Object不匹配目标
我有以下几点: type CallbackString = delegate of string -> unit
type CallbackByte = delegate of byte[] -> unit
type CallbackType =
| String of CallbackString
| Byte of CallbackByte 由于我可能有很多类型加在一起,有没有语法可以做类似这样的事情(即编译): type CallbackType =
| String of (delegate of string -> unit)
| Byte of (delega
C#中是否有我只用于调用的标准委托。是否必须为必须调用的每个新函数签名声明新的委托?
现在我只有这些签名。但是,如果我可以使用本地委托来处理任何复杂的返回和参数,那就太好了。
public bool isDone()
{...}
public void doStuff()
{...}
public void doMoreStuff(object o)
{...}
public void doEvenMoreStuff(string str)
{...}
// I'm declaring my "custom" delegates like this:
private
Action<int, string> s = new Action<int, string>(delegate(int a,string b){});
Action<int, string> ss = delegate(int a, string b) { };
为什么这两个都能用呢?为什么构造函数Action可以同时接受函数和委托者?