Projection into Interface
-
Tuesday, December 26, 2006 5:33 PMWill it be possible to project into an anonymous type and cause that type to be cast to an interface?
I know this question was asked some time ago (http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=127275&SiteID=1), but I wanted to bring it up again to see what the thinking is on this point...
All Replies
-
Friday, December 29, 2006 8:20 AMAnd for the second thing it would be great to implement an interface on the fly (Like java does at the moment)
var z = new ISomething
{
MyMethod =
delegate (string e)
{
return e;
}
}; -
Saturday, December 30, 2006 3:38 AM
Hi,
I think that the interface feature would be very useful!
Just to answer the zproxy's question - this would be interesting feature, but I'm not sure if it is that important for C#, because you can achieve similar thing using the C# 2.0 (it requires a bit more code, but I think it is good enough in the most cases):
// let's have this interface for example..
interface ISomething {
void Foo(int n);
}
// interface implementation using delegates
class SomethingDelegates {
private Action<int> _foo;
public SomethingDelegates(Action<int> foo) { _foo = foo; }
public void Foo(int n) { _foo(n); }
}
// how to use it?
ISomething s = new SomethingDelegates(delegate (int n) { /* .. code goes here .. */ }); -
Wednesday, January 17, 2007 1:04 PM
Well yes, but i was looking for a solution that would not involve the use of delegates at all. For example java does not have a load function token opcode:)
They have those inline classes.
In c# i would be delighted to be able to implement an interface on the fly.
var i = new IFoo { Bar = (Action) delegate { /* body */ } }:
or
var i = new IFoo { Bar = (Action) () => { /* body */ } }:
And this would be compiled into IL
[CompilerGenerated]
class Foo_tampa<> : IFoo
{
void IFoo.Bar()
{
/* body */
}
} -
Monday, January 22, 2007 4:42 AM
Scott Bellware wrote: Will it be possible to project into an anonymous type and cause that type to be cast to an interface?
I know this question was asked some time ago (http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=127275&SiteID=1), but I wanted to bring it up again to see what the thinking is on this point...No, we chose not to do a feature like this at this time.

