extension-methods

Stream stream = WriteableBitmap.PixelBuffer.AsStream() missing

筅森魡賤 提交于 2019-12-10 19:18:09
问题 I'm writing windows store app (8.1) and I have sample (probably for win 8.0): private WriteableBitmap WriteableBitmap; … WriteableBitmap = new WriteableBitmap(500,500); … using (Stream stream = WriteableBitmap.PixelBuffer.AsStream()) { … } So I have created new win 8.1 project and trying to do it same way, but when I get to: using (Stream stream = WriteableBitmap.PixelBuffer.AsStream()) My project dont recognize *.AsStream() method, can anyone tell me why? 回答1: using System.Runtime

why is LINQ to SQL tricked by an extension method? what now?

强颜欢笑 提交于 2019-12-10 18:14:41
问题 Consider my Event class, and that i store DateTime 's in the DB as UTC dates. I simply want to return a filtered range based on the current date in a particular time zone - easy right? This works fine: IQueryable<Event> test1 = this.GetSortedEvents().Where(e => e.FinishDateTime.Date >= DateTime.UtcNow.Date); This also works fine: IQueryable<Event> test2 = this.GetSortedEvents().Where(e => e.FinishDateTime.AddHours(3).Date >= DateTime.UtcNow.AddHours(3).Date); .. and additionally meets my time

Call generic extension method with a dynamic type [duplicate]

白昼怎懂夜的黑 提交于 2019-12-10 17:45:43
问题 This question already has answers here : How do I use reflection to call a generic method? (7 answers) Closed last year . I'm trying to execute an extension method that returns and object of type T, but I'm trying to have type T dynamic based on a Header/Detail dynamic generic type. This maybe a bit verbose... using System; using System.Collections.Generic; namespace Blah { public interface IHeader { string Name { get; set; } IDetail Detail { get; set; } } public interface IDetail { //Nothing

Concise way to do a plus equals operation on a Dictionary element that might not be initialized

可紊 提交于 2019-12-10 17:44:50
问题 I'm looking for an extension method or any other suggestion that can help me make this code as concise as possible. 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); The above code has a fairly clear purpose, I'm bucketing values into groups with a compound key. However, this code will fail because the

Markup extension 'StaticResourceExtension' requires 'IXamlSchemaContextProvider' be implemented in the IServiceProvider for ProvideValue

我们两清 提交于 2019-12-10 17:07:41
问题 A resource extension I've been using for a few years now stopped working at design time in a new .Net 4 project with the following error: Markup extension 'StaticResourceExtension' requires 'IXamlSchemaContextProvider' be implemented in the IServiceProvider for ProvideValue. The relevant method from the extension is the following: public override object ProvideValue(IServiceProvider serviceProvider) { Style resultStyle = new Style(); foreach (string currentResourceKey in resourceKeys) {

How can I override ToString() method for all IEnumerable<Int32>?

六眼飞鱼酱① 提交于 2019-12-10 16:16:42
问题 I want to override ToString() on IEnumerable<Int32> . I was thinking to use Extension methods. But when I do this below, it still calls the ToString() on System.Object . When I rename my method, then it calls my method. As my extension method is in a static class, I am not able to override. How can I achieve this so that my ToString() implementation is called when I call .ToString() on List<Int32> for example? public static class ExtensionMethods { public static new string ToString(this

Using extension methods inside extended class itself

时光毁灭记忆、已成空白 提交于 2019-12-10 14:09:22
问题 Let's say I have an interface, like this: public interface ILoggable { void Log(Func<string> message, Logger.Type type); } And some extension methods, like this: public static class Logger { public static void Log(this ILoggable loggable, Func<string> message) { loggable.Log(message, Type.Information); } public static void Log(this ILoggable loggable, string prefix, byte[] data, int len) { /* snip */ } public static void Log(this ILoggable loggable, Exception ex) { /* snip */ } // And so on..

Why is my Extension Method not showing up in my test class?

跟風遠走 提交于 2019-12-10 13:57:40
问题 I created an extension method called HasContentPermission on the System.Security.Principal.IIdentity : namespace System.Security.Principal { public static class IdentityExtensions { public static bool HasContentPermission (this IIdentity identity, int contentID) { // I do stuff here return result; } } } And I call it like this: bool hasPermission = User.Identity.HasPermission(contentID); Works like a charm. Now I want to unit test it. To do that, all I really need to do is call the extension

Is there an easier way of using extension methods in Powershell v2

可紊 提交于 2019-12-10 13:08:12
问题 Background this post explains how one can consume extension methods in Powershell http://community.bartdesmet.net/blogs/bart/archive/2007/09/06/extension-methods-in-windows-powershell.aspx Compare this to what someone does in C# - they add a "using SomeAssembly" and all the extension methods are loaded. My questions Did this get simpler Powershell 2.0. And if so, what does one do to use extension methods in Powershell 2.0? I have checked the publically available documentation and installed

Linq to entities extension method inner query (EF6)

倖福魔咒の 提交于 2019-12-10 13:05:20
问题 Can someone explain to me why the EF Engine is failing in the following scenario? It works fine with the following expression: 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(); But if I encapsulate some into an extension method: public static IQueryable