Tag: 扩展方法

无法对ContainsGenericParameters为true的类型或方法执行后期绑定操作

我有一个所有实体的扩展: public static class EntityBaseExtensions { public static T Clone(this T item) where T : EntityBase { return item.EntityClone(); } } 和 public virtual T EntityClone() where T : EntityBase { return this.MemberwiseClone() as T; } 但当我称之为: var details = user.Details.Clone(); 我明白了 无法对ContainsGenericParameters为true的类型或方法执行后期绑定操作。 有任何想法吗?

流stream = WriteableBitmap.PixelBuffer.AsStream()缺失

我正在写windows store app(8.1),我有样本(可能是win 8.0): private WriteableBitmap WriteableBitmap; … WriteableBitmap = new WriteableBitmap(500,500); … using (Stream stream = WriteableBitmap.PixelBuffer.AsStream()) { … } 所以我创建了新的win 8.1项目并尝试以同样的方式完成,但是当我到达时: using (Stream stream = WriteableBitmap.PixelBuffer.AsStream()) 我的项目不认识*.AsStream()方法,谁能告诉我为什么?

lambda表达式的扩展方法

我有一个帮助方法,它获取由lambda定义的属性的名称,其工作方式如下: ExpressionUtil.GetName((Thing t) => t.Property); // returns “Property” 我想将其转换为扩展方法,因此语法将是以下forms: ((Thing t) => t.Property).GetName(); // wont compile : operator ‘.’ cannot be applies to operand of type ‘lambda expression’ 但是我似乎不能这样做((Thing t) => t.Property)是一个lambda(不是表达式或Func)。 有没有办法编写一个直接应用于lambda的扩展方法? 如果不是为什么这是一件坏事?

IQueryable 扩展方法不起作用

我如何制作一个像这样工作的扩展方法 public static class Extensions { public static IQueryable Sort(this IQueryable query, string sortField, SortDirection direction) { // System.Type dataSourceType = query.GetType(); //System.Type dataItemType = typeof(object); //if (dataSourceType.HasElementType) //{ // dataItemType = dataSourceType.GetElementType(); //} //else if (dataSourceType.IsGenericType) //{ // dataItemType = dataSourceType.GetGenericArguments()[0]; //} //var fieldType = dataItemType.GetProperty(sortField); if (direction == SortDirection.Ascending) return query.OrderBy(s => s.GetType().GetProperty(sortField)); […]

在可能未初始化的Dictionary元素上执行加号等于操作的简洁方法

我正在寻找一种扩展方法或任何其他建议,可以帮助我使这段代码尽可能简洁。 foreach( Layer lyr in this.ProgramLayers ) foreach( UWBCEvent evt in this.BcEvents.IncludedEvents ) EventGroupLayerLosses[new EventGroupIDLayerTuple(evt.EventGroupID, lyr)] += GetEL(evt.AsIfs, lyr.LimitInMillions, lyr.AttachmentInMillions); 上面的代码有一个相当明确的目的,我用一个复合键将值分组。 但是,此代码将失败,因为字典最初为空,而+ =运算符将不知道在0处启动存储桶。 我能想到的最好的是: public V AddOrSet(this Dictionary dict, K key, V value) { if( dict.ContainsKey(key) ) dict[key] += value; else dict[key] = value; } 但是,当然,即使这样也无法编译,因为没有办法限制V的类型,使得运算符+=存在。 规则 只有一次迭代通过double for循环。 在使用0值初始化字典之前,不允许循环一次。 可以使用辅助方法或扩展方法,但我希望内循环是一个衬里。 尽可能通用和可重用,这样我就不需要为不同类型(小数,整数等)的类似存储创建一堆相同的函数。 作为参考 – 在类的其他地方,键被定义为一个实际的元组(只有命名参数),这就是为什么它可以用作字典键: […]

从词典中组合“检查添加或提取”

我厌倦了这个字典成语: Dictionary Contacts; //… if (!Contacts.ContainsKey(id)) { contact = new Contact(); Contacts[id] = contact; } else { contact = Contacts[id]; } 如果有一种语法允许从默认构造函数隐式创建新值,如果它不存在(字典知道值的类型,毕竟),那将是很好的。 有人看过帮助者(例如扩展方法)吗?

Linq到实体扩展方法内部查询(EF6)

有人可以向我解释为什么EF引擎在以下情况下失败了吗? 它可以使用以下表达式正常工作: var data = context.Programs .Select(d => new MyDataDto { ProgramId = d.ProgramId, ProgramName = d.ProgramName, ClientId = d.ClientId, Protocols = d.Protocols.Where(p => p.UserProtocols.Any(u => u.UserId == userId)) .Count(pr => pr.Programs.Any(pg => pg.ProgramId == d.ProgramId)) }) .ToList(); 但是,如果我将一些封装到扩展方法中: public static IQueryable ForUser(this IQueryable protocols, int userId) { return protocols.Where(p => p.UserProtocols.Any(u => u.UserId == […]

请向我解释扩展方法

我刚看了一下这篇文章: 你见过的扩展方法的最佳或最有趣的用途是什么? 我从来没有听说过扩展方法。 他们适用于所有语言吗? 他们有什么意义? 在那个特定的post中,我不明白这个例子。

在十进制上舍入扩展时出错 – 无法使用实例引用访问; 用类型名称来限定它

我已经多次使用扩展方法,并没有遇到这个问题。 任何人都有任何想法,为什么这会引发错误? /// /// Rounds the specified value. /// /// The value. /// The decimals. /// public static decimal Round (this decimal value, int decimals) { return Math.Round(value, decimals); } 用法: decimal newAmount = decimal.Parse(“3.33333333333434343434”); this.rtbAmount.Text = newAmount.Round(3).ToString(); newAmount.Round(3)抛出了编译器错误: Error 1 Member ‘decimal.Round(decimal)’ cannot be accessed with an instance reference; qualify it with a type […]

无法将扩展方法转换为存储表达式

我有一个扩展方法如下: public static bool SatisfiesSomeCondition(this Post post, SomeObj someObj) { return post.SomeObjId == someObj.SomeObjId; } 而我正试图像这样使用它: var query = ctx.Posts.Where(p => p.SatisfiesSomeCondition(someObj)).ToList(); 但我得到错误: LINQ to Entities无法识别方法’Boolean SatisfiesSomeCondition(xx.xx.xx.Post,xx.xx.xx.SomeObj)’方法,并且此方法无法转换为商店表达式。 如果我将查询更改为: var query = ctx.Posts.Where(p => p.SomeObjId == someObj.SomeObjId).ToList(); 这与方法相同。 它工作正常,并执行预期的T-SQL。 为什么我的第一个查询不起作用? 这是一个静态方法,不能弄清楚如何创建表达式树? (例如WHEREfilter)。 当然,我不必首先实现查询? (这意味着我不希望通过网络回来的记录,我在这里进行分页/排序,所以这不是一个选项)。 当然,我可以使用有效的方法(例如上面的内容),但方法SatisfiesSomeCondition是一种在域中使用的现有方法,我想重用该function,而不是重复它。 有任何想法吗?