views:

301

answers:

6

I know I can't write a method like:

public var MyMethod()
{
   return new{ Property1 = "test", Property2="test"};
}

I can do it otherwise:

public object MyMethod()
{
   return new{ Property1 = "test", Property2="test"}
}

but I don't want to do the second option because, if I do so, I will have to use reflection.


Why I want to do that:

Today i have a method inside my aspx page that returns a datatable as result and I cannot change it, I was trying to convert this DataTable to an Anonymous method with the properties that I want to work with. I didn't want to create a class only to do that and as I will need to perform the same query more than one time, I Thought to create a method that returns an anonymous type would be a good ideia.

+12  A: 

Returning it as a System.Object is the only way to return an anonymous type from a method. Unfortunately there is no other way to do this since anonymous types were designed specifically to prevent their use in this way.

There are some tricks that you can do to in conjunction with returning an Object that allow you to get close. If you are interested in this workaround please read Can't return anonymous type from method? Really?.

Disclaimer: Even though the article I linked does show a workaround that doesn't mean it is a good idea to do it. I would strongly discourage you using this approach when creating a regular type would be safer and easier to understand.

Andrew Hare
It should be noted that this is not a recommended idea. Intellisense will no longer recognize the strongly typed properties of the anonymous type (Property1 and Property2 from your example). Actually, you can use reflection to get at the properties, but you may as well be hitting your thumb with hammer for all the work you're putting into it, and not to mention you just took something that was supposed to make your life easier, but in fact made it harder.
Jagd
+1 couldn't have said it better myself. Yes, it can be done, but that doesn't mean its a good idea.
Brian Rasmussen
+1  A: 

No, anonymous types cannot exist outside of the context in which they are created, and as a result cannot be used as a method return type. You can return the instance as an object, but it's a much better idea to explicitly create your own container type for this purpose.

Adam Robinson
A: 

Sorry, you really aren't supposed to do that. You can hack around it with reflection or by making a generic helper method to return the type for you, but doing so is really working against the language. Just declare the type so it's clear what's going on.

mquander
A: 

I think Andrew Hare is right, you'd have to just return "object." For an editorial comment, I feel like dealing with raw objects in OO code can be a "code smell." There are cases where it's the right thing to do, but most of the time, you'd be better off defining an interface to return, or using some sort of base class type, if you're going to be returning related types.

Andy White
A: 

No, there is no support for expanding the scope of the anonymous class outside the method. Outside of the method the class is truly anonymous, and reflection is the only way to access it's members.

Guffa
+3  A: 

The easiest solution is to create a class, shove the values into the property, and then return it. If anonymous types are making your life harder then you're not using them correctly.

Jagd