In C #, we do not have anonymous types like Java. You can create an anonymous type that contains the following fields:
var myObject = new { Foo = "foo", Bar = 1, Quz = 4.2f }
However, they cannot contain methods and can only go to methods using object or dynamic (since they have no type at compile time, they are generated by the AFAIK compiler)
Instead of C #, we use, as you said, delegates or lambdas.
If I understand your pickle correctly, you can implement a nested private class as follows:
interface IMyInterface { void Foo(); } class MyClass { public void Bar() { var obj = new MyInterface(); obj.Foo(); } private class MyInterface : IMyInterface { public void Foo() {
Now MyClass can create an instance of MyInterface that implements IMyInterface . As commentators noted, MyInterface can refer to MyClass members (although you certainly want to try using publicly available members of both types).
This encapsulates an βanonymousβ class (using Java terms here to simplify it), and also means that you could return MyInterface as IMyInterface , and the rest of the software would not be wiser. In fact, how some abstract factory patterns work.
Basically, I need to run different code from the calling class, depending on what happens in the methods of the SomethingModel class.
It smells of heavy grip. Oh dear!
It seems to me that your specific problem may use refactoring. In C #, you can use Events to solve this problem (note: Can, not should). Just specify an event for each "branch" of your method. However, I must say that this makes your decision harder to foresee and support.
However, I suggest you architect your solution in such a way that you do not need such a heavy connection.
You can also try using the Pipeline model, but I'm not sure how to implement it. I know that the pier (or is it Netty? NIO for Java from JBOSS), of course, used a similar model.
You may find that dropping some unit tests to test the expected functionality of your class will simplify archiving your solution (TDD).