C++/CLI and C#, "Casting" Functions to Delegates, What's the scoop?
- by Jacob G
In C# 2.0, I can do the following:
public class MyClass
{
delegate void MyDelegate(int myParam);
public MyClass(OtherObject obj)
{
//THIS IS THE IMPORTANT PART
obj.SomeCollection.Add((MyDelegate)MyFunction);
}
private void MyFunction(int myParam);
{
//...
}
}
Trying to implement the same thing in C++/CLI, it appears I have to do:
MyDelegate del = gcnew MyDelegate(this, MyFunction);
obj-SomeCollection-Add(del);
Obviously I can create a new instance of the delegate in C# as well instead of what's going on up there. Is there some kind of magic going on in the C# world that doesn't exist in C++/CLI that allows that cast to work? Some kind of magic anonymous delegate? Thanks.