Posted By: stun | Mar 18th @ 12:07 PM
page 1 of 1
Comments: 7 | Views: 1140
I have an idea about C# Anonymous Types.


Currently anonymous type by definition is anonymous. No confusion there.
However, I think it would be really cool if we can do something like this.
var myIEmployeeAnonType = new { EmployeeId = 1, FirstName = "John", LastName = "Doe" } : IEmployee;
So basically, I am declaring an anonymous type that implements the IEmployee interface.
You might be asking why am I wishing for a language feature like that.


Let's say I have
  • a partial view that accepts List<IEmployee> collection as its model.
  • a LINQ Query that returns an inner-joined record set [Employee]<=>[PurchaseOrders].

Well, since the ASP.NET MVC is finally RTM, there are some cool things I want to do with.

In this case, I want to pass that returned result set as an anonymous type into the partial view, but "cast" it as List<IEmployee> type.

Having that ability to implementing interfaces dynamically for the anonymous types will be an awesome feature for me.
What do you guys think?
ktr
ktr
two sides to everything

I like that idea a lot; very cool.  I would prefer a syntax like this though:

var employee = new IEmployee { EmployeeId = 1, FirstName = "John", LastName = "Doe" };

for interfaces, the compiler would automatically create a class that implements that interface unless it is impossible (i.e. if it has methods -- currently not supported in anonymous types).
and you go back to the object intializer syntax Smiley I like the stun's idea, but it looks wrong when you see it in methods
littleguru
littleguru
<3 Seattle

A while ago I wrote a wrapper that tries to achieve what you are requesting: http://www.liensberger.it/web/blog/?p=298 - I know it's not perfect and it would be WAY nicer to have it in C# - I like your idea! - but it might help Smiley

blowdart
blowdart
Peek-a-boo
Aggh Duck Typing?!
stevo_
stevo_
Human after all
Ah but duck typing is implicit. If you want to do this today I think you should using a mocking framework.
ktr
ktr
two sides to everything
Sure, basically as of C# 3, everything was statically typed, meaning the compiler knows the type of everything at compile time. This is good because it gives you a safety net when passing different types around; since the compiler knows what properties/methods accept as parameters and return as values it can ensure you are passing the right types.

However, sometimes you have no idea what the type of the thing is. A good example is XML. Most of the time the XML is loosely typed and we have to resort to element.Element("FirstName").Value. Other good examples of things that do not have static type information are: REST services, COM APIs, the HTML DOM, SQL Result Sets (When not using ORM). Also when communicating with a library written in alanguage that does not have static typing (Ruby, Python, JScript, ...)

So it makes sense to have a concept of dynamic in the language for these things that do not have static types. C# introduces this in the form of IDynamicObject. If you implement this interface on a class, that means that you can intercept all calls to the object for method calls, property setters/getters, etc and define logic for them.

Then we could deal with XML in a more natural way:
// xmlElement is statically typed as dynamic
string firstName = xmlElement.FirstName;

Or REST Services:
// restEndpoint is statically typed as dynamic
List<People> = restEndpoint.People.Group("Marketing");

Or SQL Result sets:
// db.Query() returns an IEnumerable of dynamic objects (objects that implement IDynamicObject)
foreach (dynamic person in db.Query("SELECT * FROM People")) {
  Console.WriteLine(person.FirstName);
  Console.WriteLine(person.LastName);
  // ...
}

You should read here for more information:
http://blogs.microsoft.co.il/blogs/shayf/archive/2008/12/19/the-dynamic-keyword-part-1-introduction.aspx
http://blogs.msdn.com/samng/archive/2008/10/29/dynamic-in-c.aspx
page 1 of 1
Comments: 7 | Views: 1140
Microsoft Communities