How to Pass Anonymous Types as Parameters

How to pass anonymous types as parameters?

I think you should make a class for this anonymous type. That'd be the most sensible thing to do in my opinion. But if you really don't want to, you could use dynamics:

public void LogEmployees (IEnumerable<dynamic> list)
{
foreach (dynamic item in list)
{
string name = item.Name;
int id = item.Id;
}
}

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.

How to pass anonymous type to method as a parameter?

Yes you can.

public class Program
{
private static void Thing(dynamic other)
{
Console.WriteLine(other.TheThing);
}

private static void Main()
{
var things = new { TheThing = "Worked!" };
Thing(things);
}
}

But as a small, minor detail, DON'T!

Anonymous types are anonymous for a reason, they aren't first class entities in your code, they're more of a convenience. If a type is that important, define it as such.

Passing anonymous type as method parameters

There are some ways to make this possible although I wouldn't advice any of them.

First, you can use reflection which means you have to write a lot of additional (error-prone) code in your PluginService.Execute method to get the values you want.

Second, if you know the parameters of the anonymous type you are passing to your method you can use the technique described here. You can cast to another anonymous type inside your method that has the same properties. Here is another description of the same technique from Jon Skeet.

Third, you can use classes from the System.ComponentModel. For example, ASP.NET MVC uses this. It uses reflection under the hood. However, in ASP.NET MVC either the property names are well-known (controller and action for example) or their names don't matter because they are passed as-is to a controller method (id for example).

Can I pass an anonymous type as a parameter to a function?

After reading this post i realized that i cannot pass an anonymous type as a parameter to a function. So what other options do i have?

The post is telling lies. Of course you can pass anonymous types to a method.

Function DoSomething(Of T)(items As IEnumerable(Of T))

End Function

Dim Query = (From c In DB Select New With {.ElementName = c.Name})
DoSomething(Query)

In either case, your definition of DoSomething was the problem since of course Object isn’t a queryable object. ToList doesn’t help at all, since the result is still a collection of anonymous types.

How to pass a anonymous object into a generic function?

I have to agree with the comments saying "just use object for the parameters here", but; in the more general case:


To do what you want here with anonymous objects, you'd need to split the API in two; for example, instead of SaveData<T, U> consider:

int x = Save(new { image.UserID, image.FileName, image.UploadDate })
.Expect<int>();

which could be implemented as:

NamingIsHard<T> Save<T>(T x);

with

public readonly struct NamingIsHard<T>
{
T theT; // and other things; possibly the connection, etc
// ...
public U Expect<U>() {
// here you have a T and a U; do your thing!
}
}


Related Topics



Leave a reply



Submit