C# Build anonymous object dynamically












3














In C# I can easily create an anonymous object like this at the compile time:



var items = new {
Price = 2000,
Description = "",
Locations = new List<string> { "", "" }
};


My question is, it's possible to create this object at the runtime ?
I've heard of emit/meta programming, but I don't know if it helps here.



Noting that these objects will be created inside a for loop (100 items or more), so I recommend techniques that allow type caching.



Thanks.



Update - Why I need this




  • One example could be implementing the Include functionality like in db.Users.Include("Users"), so I need to add the Users property at runtime on demand.










share|improve this question
























  • can you post the code for for loop just to see what you are trying
    – Rahul
    Nov 12 at 14:27










  • What is the purpose of such dynamically typed types?
    – Patrick Hofman
    Nov 12 at 14:27
















3














In C# I can easily create an anonymous object like this at the compile time:



var items = new {
Price = 2000,
Description = "",
Locations = new List<string> { "", "" }
};


My question is, it's possible to create this object at the runtime ?
I've heard of emit/meta programming, but I don't know if it helps here.



Noting that these objects will be created inside a for loop (100 items or more), so I recommend techniques that allow type caching.



Thanks.



Update - Why I need this




  • One example could be implementing the Include functionality like in db.Users.Include("Users"), so I need to add the Users property at runtime on demand.










share|improve this question
























  • can you post the code for for loop just to see what you are trying
    – Rahul
    Nov 12 at 14:27










  • What is the purpose of such dynamically typed types?
    – Patrick Hofman
    Nov 12 at 14:27














3












3








3







In C# I can easily create an anonymous object like this at the compile time:



var items = new {
Price = 2000,
Description = "",
Locations = new List<string> { "", "" }
};


My question is, it's possible to create this object at the runtime ?
I've heard of emit/meta programming, but I don't know if it helps here.



Noting that these objects will be created inside a for loop (100 items or more), so I recommend techniques that allow type caching.



Thanks.



Update - Why I need this




  • One example could be implementing the Include functionality like in db.Users.Include("Users"), so I need to add the Users property at runtime on demand.










share|improve this question















In C# I can easily create an anonymous object like this at the compile time:



var items = new {
Price = 2000,
Description = "",
Locations = new List<string> { "", "" }
};


My question is, it's possible to create this object at the runtime ?
I've heard of emit/meta programming, but I don't know if it helps here.



Noting that these objects will be created inside a for loop (100 items or more), so I recommend techniques that allow type caching.



Thanks.



Update - Why I need this




  • One example could be implementing the Include functionality like in db.Users.Include("Users"), so I need to add the Users property at runtime on demand.







c# dynamic reflection anonymous-types reflection.emit






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 14:42

























asked Nov 12 at 14:25









amd

13.6k33550




13.6k33550












  • can you post the code for for loop just to see what you are trying
    – Rahul
    Nov 12 at 14:27










  • What is the purpose of such dynamically typed types?
    – Patrick Hofman
    Nov 12 at 14:27


















  • can you post the code for for loop just to see what you are trying
    – Rahul
    Nov 12 at 14:27










  • What is the purpose of such dynamically typed types?
    – Patrick Hofman
    Nov 12 at 14:27
















can you post the code for for loop just to see what you are trying
– Rahul
Nov 12 at 14:27




can you post the code for for loop just to see what you are trying
– Rahul
Nov 12 at 14:27












What is the purpose of such dynamically typed types?
– Patrick Hofman
Nov 12 at 14:27




What is the purpose of such dynamically typed types?
– Patrick Hofman
Nov 12 at 14:27












1 Answer
1






active

oldest

votes


















0














Anonymous types are generated on compile time, and are just regular types.



Since you are talking beyond the compilation process, you don't have the 'generate an (anonymous) type on compile time' option any more, unless you compile the type yourself to an assembly you load right after generating it. System.Reflection.Emit is your friend here. This is quite expensive though.



Personally I would skip all that hassle, and look into a type that is dynamic by nature, like ExpandoObject. You can add properties, just like you would add entries to a dictionary:



dynamic eo = new System.Dynamic.ExpandoObject();
IDictionary<string, object> d = (IDictionary<string, object>)eo;
d["a"] = "b";

string a = eo.a;


Result:



enter image description here






share|improve this answer





















  • Thanks, isn't expando objects two slow in this case ?
    – amd
    Nov 12 at 14:44










  • It is basically a dictionary. That can't be really that slow.
    – Patrick Hofman
    Nov 12 at 14:45











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264203%2fc-sharp-build-anonymous-object-dynamically%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Anonymous types are generated on compile time, and are just regular types.



Since you are talking beyond the compilation process, you don't have the 'generate an (anonymous) type on compile time' option any more, unless you compile the type yourself to an assembly you load right after generating it. System.Reflection.Emit is your friend here. This is quite expensive though.



Personally I would skip all that hassle, and look into a type that is dynamic by nature, like ExpandoObject. You can add properties, just like you would add entries to a dictionary:



dynamic eo = new System.Dynamic.ExpandoObject();
IDictionary<string, object> d = (IDictionary<string, object>)eo;
d["a"] = "b";

string a = eo.a;


Result:



enter image description here






share|improve this answer





















  • Thanks, isn't expando objects two slow in this case ?
    – amd
    Nov 12 at 14:44










  • It is basically a dictionary. That can't be really that slow.
    – Patrick Hofman
    Nov 12 at 14:45
















0














Anonymous types are generated on compile time, and are just regular types.



Since you are talking beyond the compilation process, you don't have the 'generate an (anonymous) type on compile time' option any more, unless you compile the type yourself to an assembly you load right after generating it. System.Reflection.Emit is your friend here. This is quite expensive though.



Personally I would skip all that hassle, and look into a type that is dynamic by nature, like ExpandoObject. You can add properties, just like you would add entries to a dictionary:



dynamic eo = new System.Dynamic.ExpandoObject();
IDictionary<string, object> d = (IDictionary<string, object>)eo;
d["a"] = "b";

string a = eo.a;


Result:



enter image description here






share|improve this answer





















  • Thanks, isn't expando objects two slow in this case ?
    – amd
    Nov 12 at 14:44










  • It is basically a dictionary. That can't be really that slow.
    – Patrick Hofman
    Nov 12 at 14:45














0












0








0






Anonymous types are generated on compile time, and are just regular types.



Since you are talking beyond the compilation process, you don't have the 'generate an (anonymous) type on compile time' option any more, unless you compile the type yourself to an assembly you load right after generating it. System.Reflection.Emit is your friend here. This is quite expensive though.



Personally I would skip all that hassle, and look into a type that is dynamic by nature, like ExpandoObject. You can add properties, just like you would add entries to a dictionary:



dynamic eo = new System.Dynamic.ExpandoObject();
IDictionary<string, object> d = (IDictionary<string, object>)eo;
d["a"] = "b";

string a = eo.a;


Result:



enter image description here






share|improve this answer












Anonymous types are generated on compile time, and are just regular types.



Since you are talking beyond the compilation process, you don't have the 'generate an (anonymous) type on compile time' option any more, unless you compile the type yourself to an assembly you load right after generating it. System.Reflection.Emit is your friend here. This is quite expensive though.



Personally I would skip all that hassle, and look into a type that is dynamic by nature, like ExpandoObject. You can add properties, just like you would add entries to a dictionary:



dynamic eo = new System.Dynamic.ExpandoObject();
IDictionary<string, object> d = (IDictionary<string, object>)eo;
d["a"] = "b";

string a = eo.a;


Result:



enter image description here







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 14:32









Patrick Hofman

125k18170224




125k18170224












  • Thanks, isn't expando objects two slow in this case ?
    – amd
    Nov 12 at 14:44










  • It is basically a dictionary. That can't be really that slow.
    – Patrick Hofman
    Nov 12 at 14:45


















  • Thanks, isn't expando objects two slow in this case ?
    – amd
    Nov 12 at 14:44










  • It is basically a dictionary. That can't be really that slow.
    – Patrick Hofman
    Nov 12 at 14:45
















Thanks, isn't expando objects two slow in this case ?
– amd
Nov 12 at 14:44




Thanks, isn't expando objects two slow in this case ?
– amd
Nov 12 at 14:44












It is basically a dictionary. That can't be really that slow.
– Patrick Hofman
Nov 12 at 14:45




It is basically a dictionary. That can't be really that slow.
– Patrick Hofman
Nov 12 at 14:45


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264203%2fc-sharp-build-anonymous-object-dynamically%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine