Questions tagged [anonymous-objects]
56 questions
120
votes
4 answers
Add property to anonymous type after creation
I use an anonymous object to pass my Html Attributes to some helper methods.
If the consumer didn't add an ID attribute, I want to add it in my helper method.
How can I add an attribute to this anonymous object?

Boris Callens
- 90,659
- 85
- 207
- 305
55
votes
8 answers
What to use: var or object name type?
this is a question that when programming I always wonder: What to use when we are writing code:
var myFiles = Directory.GetFiles(fullPath);
or
string[] myFiles = Directory.GetFiles(fullPath);
var is new and is a Implicitly Typed Local Variables,…

balexandre
- 73,608
- 45
- 233
- 342
33
votes
2 answers
Is there an easy way to merge C# anonymous objects
Let's say I have two anonymous objects like this:
var objA = new { test = "test", blah = "blah" };
var objB = new { foo = "foo", bar = "bar" };
I want to combine them to get:
new { test = "test", blah = "blah", foo = "foo", bar = "bar" };
I won't…

ajma
- 12,106
- 12
- 71
- 90
10
votes
6 answers
Create anonymous object by Reflection in C#
Is there any way to create C# 3.0 anonymous object via Reflection at runtime in .NET 3.5? I'd like to support them in my serialization scheme, so I need a way to manipulate them programmatically.
edited later to clarify the use case
An extra…

Michael Pliskin
- 2,352
- 4
- 26
- 42
9
votes
3 answers
How can I easily create a strongly typed object from an anonymous object in TypeScript?
I have some JSON containing anonymous objects coming to my client-side. Is there some built-in mechanism or external library for converting these anonymous objects into strongly-typed TypeScript objects? Is there something like AutoMapper for doing…

Ryan Shripat
- 5,574
- 6
- 49
- 77
7
votes
5 answers
C# anonymous object as parameter not working
https://dotnetfiddle.net/446j0U link to reproduce (failed on .net 4.7.2 not on .net core)
public class TEST {
static public void Main(string[] args)
{
var test = new { Text = "test", Slab = "slab"};
…

user3038144
- 95
- 1
- 6
6
votes
3 answers
Creating a hybrid of a mock and an anonymous object using e.g. Moq and AutoFixture?
I encountered a class during my work that looks like this:
public class MyObject
{
public int? A {get; set;}
public int? B {get; set;}
public int? C {get; set;}
public virtual int? GetSomeValue()
{
//simplified behavior:
return A…

Grzesiek Galezowski
- 765
- 7
- 20
6
votes
1 answer
Is this a completely anonymous object... if so, how do you access it?
Possible Duplicate:
What does a script-Tag with src AND content mean?
I was just viewing this page, on how to implements Googles +1 button. When one implementation (parse explicit) showed, among other things, this code: