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…
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:
Decent Dabbler
  • 22,532
  • 8
  • 74
  • 106
6
votes
6 answers

Anonymous type scoping issue

What is the proper way to create a variable that will house a list of anonymous objects that are generated through a LINQ query while keeping the variable declaration outside of a try/catch and the assignment being handled inside of a try/catch? At…
Jared
  • 5,840
  • 5
  • 49
  • 83
5
votes
3 answers

c++ problems with temporary ostream objects

I thought to transform this working code: ofstream outfile("my_file.txt"); copy(v.begin(), v.end(), ostream_iterator(outfile)); into this: copy(v.begin(), v.end(), ostream_iterator(ofstream("my_file.txt"))); In other words, I use an…
Jason
  • 231
  • 2
  • 4
4
votes
2 answers

How do I access the super class of TreeSelectionListener in Java?

this.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent e) { // How do I access the parent tree from here? } });
Goutham
  • 2,759
  • 9
  • 30
  • 36
3
votes
1 answer

Access anonymous object property from static class c#

I have a static class with multiple anonymous objects. Each object, has a different amount of properties, but each property is always a object of created class. static public class Fields{ static public Object FieldInfo1 = new { Customer…
3
votes
4 answers

echo (new DateTime())->getTimestamp(); ... anonymous object not suported?

There is probably no way to do work with anonymous objects? I want to do short thinks like this: echo (new DateTime())->getTimestamp(); In javascript we could shortly use: alert( (new Date()).getTime() ); Is there any shorter way which can be used…
SL5net
  • 2,282
  • 4
  • 28
  • 44
2
votes
2 answers

How do I add a List to an anonymous object in C#?

Let's say I have a list: var list = new List{1,2,3}; How do I add this to an anonymous object such that the object would look like: { list : [1,2,3] }
user3912349
  • 163
  • 1
  • 8
2
votes
1 answer

What are the advantages of an anonymous object?

I have one class named Sample which is used in my code like below: class Sample{ . . Object someMethod(){ return someObject; } . . } I call itlike: Object ob = new Sample().someMethod(); I want to know if there is any advantage if I…
mcacorner
  • 1,304
  • 3
  • 22
  • 45
1
2 3 4