extension-methods

Open DTE solution from another program (not add-in)

时光毁灭记忆、已成空白 提交于 2019-12-07 15:55:40
问题 Is it possible to modify a solution, and use envdte tools, from a command-line project ? I have an add-in that modifies a solution. But... the changes are required for over a hundred projects... So I'd like to make a c# program that has the same logic, only it iterates through all solution files. The add-in starts with EnvDTE.Solution solution = (EnvDTE.Solution)application.Solution; where DTE2 application is passed from the add-in... How can I get the same solution, which then I query for

Why is my event handlers target <>c? Also - What even is <>c?

折月煮酒 提交于 2019-12-07 15:34:16
问题 Following the specifications for creating a Minimal, Complete and Verifiable code set, please see below : using System; using System.Data; using System.Linq; using System.Windows; namespace WhatIsThis{ /// <summary> /// Interaction logic for App.xaml /// </summary> public partial class App : Application { private void Application_Startup( object sender, StartupEventArgs e ) { Foo.Bar += ( S, E ) => Console.WriteLine( "Do Something!" ); } protected override void OnActivated( EventArgs e ) {

Extension methods not showing

三世轮回 提交于 2019-12-07 13:51:27
问题 I am creating extension methods for the HtmlHelper class in an MVC web app. Nothing is showing, not even the default InputExtensions. public static class HtmlHelpers { public static void RegisterScriptInclude(this HtmlHelper htmlhelper, string script) { if (!RegisteredScriptIncludes.ContainsValue(script)) { RegisteredScriptIncludes.Add(RegisteredScriptIncludes.Count, script); } } public static string RenderScripts(this HtmlHelper htmlhelper) { var scripts = new StringBuilder(); foreach

What is the correct way to chain methods in .Net

╄→гoц情女王★ 提交于 2019-12-07 13:10:29
问题 In .Net, you can chain methods returning a value or by using a void. Is one of them the "right way"? So you could say 1) Foo myFoo = new Foo(); myfoo.Bars = myBars.DoSomethingCool(x) .DoSomethingElse(y) .AndSomethingElse(z); public static IList<IBar> DoSomethingCool(this IList<IBar> source, object x) { IList<IBar> result = //some fn(source) return result; } In this case, all 3 extension methods need to return IList (the type for myFoo.Bars) or it could also be written as 2) myBars

Type inference with multiple generic type parameters

萝らか妹 提交于 2019-12-07 11:35:44
问题 I don't understand why C# doesn't infer a type in the following complete situation: public interface IThing {} public class Thing1 : IThing {} public class Thing2 : IThing {} public interface IContainer {} public class Container1 : IContainer { public IThing A { get { return new Thing1(); } } public IThing B { get { return new Thing2(); } } } public class Container2 : IContainer { public IThing C { get { return new Thing1(); } } public IThing D { get { return new Thing2(); } } } public class

Process every pair in a sequence

不问归期 提交于 2019-12-07 08:42:42
问题 I'm looking for a concise way to process every (unordered) pair of elements in a sequence in .NET. I know I can do it with nested for loops, but I was looking for something a little more readable. I was imagining something like a modified Any() extension method: IEnumerable<Transaction> transactions = ... if (transactions.AnyPair( (first, second) => first.UniqueID == second.UniqueID)) throw ... Or maybe a foreach -style one: IEnumerable<JigsawPiece> pieces = ... pieces.ForEachPair( (first,

Best performance for ObservableCollection.AddRange

大兔子大兔子 提交于 2019-12-07 08:37:11
问题 I'm writing an extension method for ObservableCollection and have read that the .Add function raises 3 property changed events per call, So that something like this is a bad idea: public static void AddRange<T>(this ObservableCollection<T> oc, IEnumerable<T> collection) { if (collection == null) { throw new ArgumentNullException("collection"); } foreach (var i in collection) { oc.Add(i); } } Are there any other solutions to this? 回答1: Given that Concat<T> is an extension method it is almost

Overload resolution, extension methods and genericity in C#

谁说胖子不能爱 提交于 2019-12-07 08:21:45
问题 I have the following scenario in my C# source: class A{} class Dispatch<T>{} static class DispatchExt { public static void D<T>(this Dispatch<T> d, int a) { Console.WriteLine("Generic D chosen with a = " + a.ToString()); } public static void D(this Dispatch<A> d, int a) { Console.WriteLine("D<A> chosen with a = " + a.ToString()); } } class Program { static void D<T>(Dispatch<T> d, int a) { d.D(a); } static void Main(string[] args) { int a = 5; var dispatch = new Dispatch<A>(); dispatch.D(a);

Why doesn't EnumerableRowCollection<DataRow>.Select() compile like this?

点点圈 提交于 2019-12-07 08:17:37
问题 This works: from x in table.AsEnumerable() where x.Field<string>("something") == "value" select x.Field<decimal>("decimalfield"); but, this does not: from x in table.AsEnumerable() .Where(y=>y.Field<string>("something") == "value") .Select(y=>y.Field<decimal>("decimalfield")); I also tried: from x in table.AsEnumerable() .Where(y=>y.Field<string>("something") == "value") .Select(y=>new { name = y.Field<decimal>("decimalfield") }); Looking at the two overloads of the .Select() method, I

Type inference problem when writing a generic extension method with more than one type

房东的猫 提交于 2019-12-07 06:31:40
问题 I am writing a generic extension method for IEnumerable for mapping a list of objects to another list of mapped objects. This is how I would like the method to work: IList<Article> articles = GetArticles(); return articles.Map<ArticleViewModel>(_mappingEngine); This is the method: public static IEnumerable<T2> Map<T1, T2>(this IEnumerable<T1> list, IMappingEngine engine) { return list.Select(engine.Map<T1, T2>); } However articles.Map<ArticleViewModel>(_mappingEngine); gives a compile error.