Scott Bellware

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...



Re: LINQ Project General Projection into Interface

zproxy

And 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;
}
};







Re: LINQ Project General Projection into Interface

Tomas Petricek

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 .. */ });






Re: LINQ Project General Projection into Interface

zproxy

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 */

}
}





Re: LINQ Project General Projection into Interface

Matt Warren - MSFT

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.