extension-methods

Using Attributes for Generic Constraints [duplicate]

纵然是瞬间 提交于 2019-11-29 17:37:28
问题 This question already has an answer here: Can you use “where” to require an attribute in c#? 5 answers Given an example such as .. public interface IInterface { } public static void Insert<T>(this IList<T> list, IList<T> items) where T : IInterface { // ... logic } This works fine, but I was wondering if it is possible to use an Attribute as a constraint. Such as ... class InsertableAttribute : Attribute public static void Insert<T>(this IList<T> list, IList<T> items) where T : [Insertable] {

How to resolve dependency in static class with Unity?

∥☆過路亽.° 提交于 2019-11-29 16:58:15
问题 I have the following extension method, which exists (naturally) in a static class. public static class MyExtensions { [Dependency] private static IMyDataContext _myDataContext { get; set; } public static void MyExtensionMethod(this MyType myType) { // do stuff _myDataContext.DoAwesomeThing(); } } the _myDataContext object is null . Normally I'd use the UnityContainer to register the type, but as this is a static class, I can't. What do I need to instantiate _ myDataContext so that it's not

The type or namespace name 'Window' does not exist in the namespace 'System.Windows'

我的梦境 提交于 2019-11-29 16:11:40
I am trying to write an extension method for the WPF Window class. I am doing it in a class library project in my solution, so that I can use it in all my projects in the solution. Here is my code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace ExtensionMethods { public static class MyExtensions { public static void ResizeToPrimaryScreenWorkingArea(this System.Windows.Window w) { } } } When I try to compile this, I get the following error: The type or namespace name 'Window' does not exist in

What's the Best Way to Add One Item to an IEnumerable<T>?

扶醉桌前 提交于 2019-11-29 16:10:12
问题 Here's how I would add one item to an IEnumerable object: //Some IEnumerable<T> object IEnumerable<string> arr = new string[] { "ABC", "DEF", "GHI" }; //Add one item arr = arr.Concat(new string[] { "JKL" }); This is awkward. I don't see a method called something like ConcatSingle() however. Is there a cleaner way to add a single item to an IEnumerable object? 回答1: Nope, that's about as concise as you'll get using built-in language/framework features. You could always create an extension

C# Count() Extension Method Performance

左心房为你撑大大i 提交于 2019-11-29 15:56:41
If the LINQ Count() extension method is invoked on an IEnumerable<T> that has a Count property (e.g. List<T> ), does the Count() method look for that property and return it (rather than counting the items by enumerating them)? The following test code seems to indicate that it does: using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Linq; namespace CountSpeedTest { // Output: // List - CLR : 0 ms // Enumerate - CLR : 10 ms // List - Mine: 12 ms // Enumerate - Mine: 12 ms class Program { private const int Runs = 10; private const int

Extending a solution for simple binding to a 'Text property to multiple Controls to handle binding to any Type?

一世执手 提交于 2019-11-29 15:32:07
My question is : how to move beyond writing a custom implementation of a technique for databinding multiple controls (controls without built-in DataSource properties), for each possible type of data , to simple properties ... as described and demonstrated in code that follows ... to achieve a more poweful solution that will be independent of whether the binding is to a string, or an int, or other types. My guess is: this will involve reflection; but, I'm stuck at that point. I'm looking for strategic advice on which "direction" to move next, hints, clues, not a complete code answer, but of

Pulling Apart Expression<Func<T, object>>

北慕城南 提交于 2019-11-29 15:13:10
问题 I am busy creating wrapper extension methods on top of Dapper and DapperExtensions. At the moment I am trying to add filtering to the GetList<T> extension method, similar to LINQ's Where<T> extension method. I have seen this question but it seems I cannot implement what Marc Gravell suggested because there isn't a type EqualsExpression in .NET 4.5. Here is some demo code to help with the explanation of my problem: using System; using System.Collections.Generic; using System.Configuration;

IEnumerable<T> null coalescing Extension

女生的网名这么多〃 提交于 2019-11-29 14:18:11
I frequently face the problem to check whether an IEnumerable<T> is null before iterating over it through foreach or LINQ queries, then I often come into codes like this: var myProjection = (myList ?? Enumerable.Empty<T>()).Select(x => x.Foo)... Hence, I thought to add this extension-method to an Extensions class: public static class MyExtensions { public static IEnumerable<T> AsEmptyIfNull<T>(this IEnumerable<T> source) { return source ?? Enumerable.Empty<T>(); } } A little issue comes immediately in my mind looking at this code, i.e., given the "instance-methods aspect" of extension-methods,

How to use GetMethod for static extension method

╄→尐↘猪︶ㄣ 提交于 2019-11-29 13:58:59
I've got an extension method: public static class StringEx { public static bool Like(this string a, string b) { return a.ToLower().Contains(b.ToLower()); } } How to reflect it properly via GetMethod with my parameters? I've tried this with no success (Got an exception about static method): var like = typeof(StringEx).GetMethod("Like", new[] {typeof(string), typeof(string)}); comparer = Expression.Call(prop, like, value); Use this extension method to get other extension methods: public static class ReflectionExtensions { public static IEnumerable<MethodInfo> GetExtensionMethods(this Type type,

How to pass 2 generics types into an extension method [duplicate]

狂风中的少年 提交于 2019-11-29 13:56:23
This question already has an answer here: Partial type inference 3 answers I've created the following extension method public static T Map<TEntity,T>(this TEntity entity) where TEntity : IEntity { return Mapper.Map<TEntity, T>(entity); } This allows the following Db.ExchangeSets.FirstOrDefault().Map<ExchangeSet, ExchangeSetSimpleViewModel>() However i'm wondering if there is anyway i can modify the extension method so i can call a shorted version as follows Db.ExchangeSets.FirstOrDefault().Map<ExchangeSetSimpleViewModel>() Please Note : Whether or not automapper should be used like this is not