Passing Anonymous types as parameters to the functions
I need to pass to a function an object that is an anonymous type.
The thing I expect th开发者_开发知识库at the object contains some properties, say Name(string) and Rang(BO).
Say, I have a query like
Dim query = From city In _container.Cities
From street In city.Streets
Select Name = street.Name, Rang = street.Rang
Distinct
Now, I would like to pass each element of this query to a function.
Is it possible?
Yes, it's possible.
You can use cast by example. Here's a sample:
class CastByExample {
public T Cast<T>(T t, object toCast) {
return (T)toCast;
}
public void M(object o) {
var y = Cast(new { Name = "Name", Range = 0 }, o);
Console.WriteLine(y.Name);
}
public void Test() {
var x = new { Name = "John Doe", Range = 17 };
M(x);
}
}
You can also use reflection.
But really, how is jumping through these hoops easier than just writing a concrete non-anonymous type?
It's not possible to state the type of an anonymous object in VB.Net. However if you're comfortable with Option Strict Off
then you can use late binding. Just have the target method accept the parameter of type Object
Public Sub UseAnonymousObject(ByVal p as Object)
Console.WriteLine(p.Name)
Console.WriteLine(p.Rang)
End Sub
...
For Each current in query
UseAnonymousObject(current)
Next
In a word, no. From MSDN :
"You cannot declare a field, a property, an event, or the return type of a method as having an anonymous type. Similarly, you cannot declare a formal parameter of a method, property, constructor, or indexer as having an anonymous type." http://msdn.microsoft.com/en-us/library/bb397696.aspx
There isn't a good way (meaning simple) to pass an anonymous type between methods. I would create a helper class instead.
Having said that, you might also be interested in Jon Skeet's article on anonymous types leaving the method scope:
http://blogs.msmvps.com/jonskeet/2009/01/09/horrible-grotty-hack-returning-an-anonymous-type-instance/
I haven't looked too much into them, and I may be missing the point of your question, but if you're on the .Net 4 framework, you could use Tuples instead of anonymous types. They seem to lose the explicit property naming that is nice w/ Anonymous types, but they do have an advantage in that they can be passed to and from methods.
The other draw-back, though, is that the method signature does still need to be defined with the proper Tuple definition. So, in most cases, I'm not sure that it buys you too much over just using non-anonymous types, because what it offers in flexbility, it hinders in terms meaningful argument communication.
So I would not recommend them for this type of use at any sort of broad-level interface. (e.g. publicly exposed methods with argument types like Tuple seem much better suited to well-defined type definitions over the overly-vague Tuple arguments) They can be handy, though, and well-suited I think, for private methods that support one-off, ad-hoc type objects.
Haven't tried it, but how about using dynamics?
IEnumerable<dynamic> SubMethod(...)
{
....
return (IEnumerable<dynamic>) (from .... select new{Property1 = v1,...})
}
void Method()
{
var q = SubMethod().FirstOrDefault(); //or whatever
var j = q.Property1;
...
}
精彩评论