anonymous-objects

What are the advantages of an anonymous object?

孤街醉人 提交于 2019-12-29 08:07:10
问题 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 create anonymous ``Object of any class ( new Sample() ) and call any require method if I don't have any further use of this Object`. 回答1: I assume that you are asking about the code you posted as contrasted with the following: Sample s = new Sample(); s.someMethod

Is it possible to load a UserControl into an anonymous object, instead of just instantiating it?

笑着哭i 提交于 2019-12-24 23:31:11
问题 Currently I'm generating UserControls as follows in an abstract base class so they are available for any other pages that implement the base class: // doc is an XML file that may or may not contain a TopStrapline node var pageControls = new { TopStrapline = (from strap in doc.Elements("TopStrapline") select new TopStrapline { StraplineText = (string)strap.Attribute("text") }).FirstOrDefault(), // loads of other UserControls generated from other nodes }; // if there's a StrapLine node, I want

LINQ to XML: creating complex anonymous type

情到浓时终转凉″ 提交于 2019-12-11 07:27:41
问题 I've an xml file as follows: <ProductGroup> <Product id="4601A"> <name>Roses</name> <section>Floral</section> <price>46</price> <PopupImages> <PopupImage>img1.jpg</PopupImage> <PopupImage>img2.jpg</PopupImage> </PopupImages> <ImageThumbs> <thumb>img1-thm.jpg</thumb> <thumb>img2-thm.jpg</thumb> </ImageThumbs> </Product> </ProductGroup> In production the ProductGroup node might contain many Product nodes. For this I kind of want to build a list of an anonymous object that has the following

Why can't I instantiate a generic class inferring types from anonymous objects?

人走茶凉 提交于 2019-12-07 21:55:15
问题 Let's say I have some class - hypothetical example: public class InvalidResponseException<TReq, TResp> : Exception { public TReq RequestData { get; protected set; } public TResp ResponseData { get; protected set; } public InvalidResponseException(string message, TReq requestData, TResp responseData) : this(message, null, requestData, responseData) { } public InvalidResponseException(string message, Exception innerException, TReq requestData, TResp responseData) : base(message, innerException)

Why can't I instantiate a generic class inferring types from anonymous objects?

倖福魔咒の 提交于 2019-12-06 07:53:44
Let's say I have some class - hypothetical example: public class InvalidResponseException<TReq, TResp> : Exception { public TReq RequestData { get; protected set; } public TResp ResponseData { get; protected set; } public InvalidResponseException(string message, TReq requestData, TResp responseData) : this(message, null, requestData, responseData) { } public InvalidResponseException(string message, Exception innerException, TReq requestData, TResp responseData) : base(message, innerException) { RequestData = requestData; ResponseData = responseData; } } Okay, class defined... and compiles, no

Is there an easy way to merge C# anonymous objects

江枫思渺然 提交于 2019-11-28 06:19:42
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 know what the properties are for both objA and objB at compile time. I want this to be like jquery's extend method. Anybody know of a library or a .net framework class that can help me do this? If you truly do mean dynamic in the C# 4.0 sense, then you can do something like: static dynamic Combine(dynamic item1, dynamic item2) { var dictionary1 =

Add property to anonymous type after creation

主宰稳场 提交于 2019-11-27 11:37:29
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? If you're trying to extend this method: public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues); Although I'm sure Khaja's Object extensions would work, you might get better performance by creating a RouteValueDictionary and passing in the routeValues object, add your additional parameters from the Context, then return

Is there an easy way to merge C# anonymous objects

五迷三道 提交于 2019-11-27 01:16:17
问题 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 know what the properties are for both objA and objB at compile time. I want this to be like jquery's extend method. Anybody know of a library or a .net framework class that can help me do this? 回答1: If you truly do mean dynamic in the C# 4.0 sense,

What to use: var or object name type? [duplicate]

百般思念 提交于 2019-11-27 00:40:02
This question already has an answer here: Use of var keyword in C# 86 answers 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 , so we can only use locally and it has rules like can't be null, etc., but I wonder if we get any advantage of using it "normally". The "normally" part says, not in Anonymous Types , Object and Collection Initializers and Query Expressions where that was the intent to use the