0

There is a way to declare a method with a collection of anonymous type as parameter?

for example somethings like this pseudo syntax :

private void dosomething ( List<class with { string Name, string Lastname, int Age }> persons) {
... 
}

To avoid creating a single use class.

UPDATE What I want is to avoid the definition of a class that I will use only for passing data to this method.

1
  • 1
    You can use generics private void dosomething<T> (List<T> ... Commented Nov 14, 2012 at 8:36

3 Answers 3

2

update

Your question is a bit confusing.

According to your first part of the question I think you need a generic method.

private void dosomething<T>(List<T> items) {
... 
}

but after looking at the code you wrote you just need an interface IPerson

public interface IPerson
{
   Name { get; set; }
   LastName { get; set; }
   Age { get; set; }
}

private void dosomething(List<IPerson> persons) {
    ... 
}
Sign up to request clarification or add additional context in comments.

10 Comments

This isn't much use if he actually wants to access the Name, Lastname or Age in the method, though.
@Rawling: If T is restricted further to an interface that includes the respective attributes, it comes close. (And certainly much closer than using the dynamic keyword.)
I doubt that - anonymous types don't automatically implement interfaces, do they?
Generic type can be restricted by void Dosomething<T>(List<T> items) where T : IPerson.
@O.R.Mapper Sorry, I assumed as the OP was asking about passing anonymous types to a method and not creating a class to pass to it, you were also talking about anonymous types. If you're going to create an interface and a class, you might as well not create the interface and just use the class.
|
1

You should make a class for this anonymous type because an anonymous type cannot be passed in a strong manner outside the scope of an individual method, because there is no way to represent it outside the method's scope. But if you really don't want to, you could use dynamics:

public void DoSomething (IEnumerable<dynamic> list)
{
    foreach (dynamic item in list)
    {
        string name = item.Name;
        string lastName = item.LastName;
        int age = item.Age;
    }
}

Note that this is not strongly typed, so if, for example, Name changes to EmployeeName, you won't know there's a problem until runtime.

4 Comments

As you've noted, this is not strongly typed and as such doesn't quite do what was asked for. On top of that, it also doesn't impose any restrictions for the existence of certain attributes as was asked for.
That's what i said at first place that ideal solution would be to create a class. And as per OP question he want's to pass an anonymous types as parameter for which i suggested can be done using dynamic.
This isn't the solution, beacause I'll loose the strongly type, and I'm sure that will have problems in runtime. but is the closest thing...
Yeah, you need to settle with interface or class then since anonymous types can't be accessed outside its scope.
0

You can do this with a dynamic keyword, although you will lose strongly typed properties.

private void doSomething (List<dynamic> persons)
{
var name = persons.FirstOrDefault().Name;
}

7 Comments

I can not see where he asked for a restriction? He simply asks if there is a way to pass a list of anonymous class into a method without declaring a class - and that is what dynamic keyword allows. So why you are downvoting every answer to this question?
He asks if there is a way to pass a list of objects *that have the three attributes string Name, string Lastname, int Age *, not just any anonymous types. And in fact, I have upvoted the answer that suggests generics.
He definitely asks: "There is a way to declare a method with a collection of anonymous type as parameter?" And yes it is - with the use of dynamic keyword. So you upvoted the only answer which won't work (which ends up in defining a new class) :)
Yes, an anonymous, i.e. an unknown type, with the restriction for a few attributes, as shown in the code sample. So solutions with the dynamic keyword won't work. The solution with a generic type avoids the requirement of defining a new class, but - as C# doesn't allow to restrict arguments by attribute lists - the eligible types have to implement an interface, which is as close as we can get to what the OP asked.
Yeah in theory, but in practice it won't work. See stackoverflow.com/questions/191013/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.