How to do anonymous type in .net 2.0

M

mohaaron

Can anyone tell me how I can mimic the following code in .net 2.0?
This code works perfectly for converting the returned object to json
which I then use in javascript. I'm now on a project where I'm limited
to .net 2.0 and need to same functionality. Its the var data = new {}
that I need to mimic.

I'm using this inside of a web service.

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public Object GetData()
{
List<Person> people = new List<Person>()
{
new Person { FirstName="Andy", LastName="Todd", Age=21,
Height=221 },
new Person { FirstName="Dave", LastName="Nelson", Age=1, Height=1 },
new Person { FirstName="Lil", LastName="Edgar", Age=24,
Height=121 },
new Person { FirstName="Ian", LastName="Davis", Age=11, Height=190 }
};

// How can I do this in .net 2.0?
var data = new { rows = people, success = true, totalProperty =
people.Count};

return data;
}
 
A

Arne Vajhøj

mohaaron said:
Can anyone tell me how I can mimic the following code in .net 2.0?
This code works perfectly for converting the returned object to json
which I then use in javascript. I'm now on a project where I'm limited
to .net 2.0 and need to same functionality. Its the var data = new {}
that I need to mimic.

I'm using this inside of a web service.

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public Object GetData()
{
List<Person> people = new List<Person>()
{
new Person { FirstName="Andy", LastName="Todd", Age=21,
Height=221 },
new Person { FirstName="Dave", LastName="Nelson", Age=1, Height=1 },
new Person { FirstName="Lil", LastName="Edgar", Age=24,
Height=121 },
new Person { FirstName="Ian", LastName="Davis", Age=11, Height=190 }
};

// How can I do this in .net 2.0?
var data = new { rows = people, success = true, totalProperty =
people.Count};

return data;
}

Why not just create a class or struct with those 3 fields/properties ?

Arne
 
P

Peter Duniho

Can anyone tell me how I can mimic the following code in .net 2.0?
This code works perfectly for converting the returned object to json
which I then use in javascript. I'm now on a project where I'm limited
to .net 2.0 and need to same functionality. Its the var data = new {}
that I need to mimic.

Are you limited to using C# 2.0? Or can you target .NET 2.0 using
VS2008/C# 3.0?

Anonymous types are a _language_ feature. You don't need a specific
version of the framework, just C# 3.0. And in VS2008, you can specify
..NET 2.0 as your target.

If you're stuck using C# 2.0 (i.e. VS2005), then you'll have to use a
named type as Arne suggests.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top