Tag: 匿名类型

将项添加到匿名列表

我有一个匿名类型的列表 var myList = db.Products.Select(a => new {a.ProductName, a.ProductId, a.Priority}).ToList(); 我想在此列表中添加其他项目,例如 myList.Insert(0, new { “–All–“, 0, 0}); //Error: Has some invalid arguments 我也试过了 myList.Add(new { “–All–“, 0, 0}); //Error: Has some invalid arguments 我怎样才能做到这一点? 编辑: 我在第一次回答后这样做了 var packageList = db.Products.Select(a => new { a.ProductName, a.ProductId, a.Priority }).ToList(); packageList.Insert(0, new { ProductName = “All”, ProductId = […]

除了LINQ查询,你如何在C#中使用匿名类型?

我一直在努力加快C#中一些较新的function,其中一个我没有用过的是匿名类型。 我理解它与LINQ查询有关的用法,我查看了这个SOpost ,它提出了类似的问题。 我在网上看到的大多数例子都与LINQ查询有关,这很酷。 我看到了一些有些人为的例子,但实际上并没有看到很多有价值的东西。 您是否对匿名类型有一种新颖的用途,您认为它真的为您提供了一些实用工具?

将匿名类型作为方法参数传递

在我的插件架构中,我目前正在将插件名称(字符串),方法名称(字符串)和参数(对象数组)传递给我的插件服务,以执行指定的方法并返回结果(类型为T)。 插件服务的execute方法如下所示: public TResult Execute(string pluginName, string operation, params object[] input) { MethodInfo method = null; TResult result = default(TResult); var plugin = _plugins.Enabled().FirstOrDefault(x => x.GetType().Name.Equals(pluginName, StringComparison.InvariantCultureIgnoreCase)); if (plugin != null) { method = plugin.GetType().GetMethods().FirstOrDefault(x => x.Name == operation); if (method != null) { result = (TResult)method.Invoke(plugin, input); } } return result; } 示例用法: var […]

如何使用匿名返回类型声明Func?

我需要能够做到这一点: var getHed = () => { // do stuff return new { Property1 = value, Property2 = value2, etc…}; }; var anonymousClass = getHed(); 但是我得到一个错误,表明我需要明确声明getHed。 如何声明Func使得T是我要返回的匿名类型? 如果你很好奇为什么我需要这样做,那是因为我使用第三方软件允许自定义代码,但只能在一个方法中使用。 这可能变得非常难以管理。 我有一个想法,我可以使用匿名方法来帮助保持程序代码的组织。 在这种情况下,为了帮助我,我需要一个新的类,除了匿名之外我无法定义。

匿名类型和元组

匿名类型和元组之间有什么区别?

为什么Enum.GetValues()在使用“var”时返回名称?

有谁能解释一下? 替代文字http://www.deviantsart.com/upload/g4knqc.png using System; namespace TestEnum2342394834 { class Program { static void Main(string[] args) { //with “var” foreach (var value in Enum.GetValues(typeof(ReportStatus))) { Console.WriteLine(value); } //with “int” foreach (int value in Enum.GetValues(typeof(ReportStatus))) { Console.WriteLine(value); } } } public enum ReportStatus { Assigned = 1, Analyzed = 2, Written = 3, Reviewed = 4, Finished = […]

通过c#中的reflection获得’基本’数据类型而不是奇怪的可空类型

我的基本需求是从LINQ to SQL查询生成的匿名类型中获取数据类型。 我有一段代码(比我能写的更聪明,因为我还没有深入研究过reflection)从匿名类型中返回数据类型,并且在linq2sql属性中标记为’not nullable’的元素非常有效。 所以,如果我有一个字符串,它将作为System.String返回。 但是,当元素可以为空时,我最终会得到它的“全名”: {Name =“Nullable 1″ FullName = “System.Nullable 1 [[System.Decimal,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]]”} 在这种情况下,我想要提取的是System.Decimal类型(在字符串或其他情况下,我只想要System.String)。 我查看了属性,找不到任何似乎存储它的东西。 private static Dictionary GetFieldsForType(IEnumerable data) { object o = data.First(); var properties = o.GetType().GetProperties(); return properties.ToDictionary(property => property.Name, property => property.PropertyType); } LINQ查询(我认为这不重要): var theData = from r in db.tblTestEdits select new […]

匿名类型

我有一个Dictionary(TKey, TValue)类的 Dictionary Deduction_Employees = new Dictionary(); 后来我在这个数组列表中添加了这样的匿名类型 var day_and_type = new { TheDay = myDay, EntranceOrExit = isEntranceDelay }; Deduction_Employees[Employee_ID].Add(day_and_type); 现在我该如何取消对var的访问并访问这些属性?

从匿名类型中获取价值?

假设我们有以下方法: public object Test() { return new { A = “Test” }; } 是否有可能获得存储在A中的值? var b = Test(); //Any chance to cast this to the anonymous type?

在ASP.NET MVC 3中以编程方式创建routeValues(匿名类型)

假设我们有一个页面,其中包含一些支持从DB搜索的元素,因此在Post Action中我们需要找到哪些元素是活动的,并根据这些创建路由值,如下所示: List SearchParameters = GetFilterParameters(collection); if(SearchParameters.Count > 0) foreach(Parameter item in SearchParameters) { switch(item.Name) { case “Category”: CategoryValue= item.Value; break; case “StartDate”: StartDateValue= item.Value; break; case “Product”: ProductValue= item.Value; break; } return RedirectToAction(“Index”, new {category = CategoryValue, startdate=StartDateValue, product=ProductValue }); 那么有没有办法动态定义routeValues类似于以下Pseudo-Code: var dynamicRoutValues; foreach(Parameter item in SearchParameters) { dynamicRoutValues.Add(item.Name, item.Value) } return RedirectToAction(“Index”, dynamicRoutValues);