extension-methods

How to Define Extension Method for ICollection<T> where T : IMyInterface without Specifying T in the Method Definition

痴心易碎 提交于 2019-12-12 21:04:37
问题 Background: I want to hook in business rules at the point that DTOs are being mapped to entities. I figured encapsulating the mapping into an extension method would be a good route. IEntityDto is the interface that all DTOs that can be directly mapped to entities implement. The single instance works fine: public static TEntity MapTo<TEntity>(this IEntityDto dto) { ... Run Business Rules that don't require db access ... return AutoMapper.Mapper.Map<TEntity>(dto); } I'd like to also extend

Why can't I call OrderBy in a class that extends List?

会有一股神秘感。 提交于 2019-12-12 20:54:20
问题 I have a class, Deck , that contains a method called Shuffle . I'm working on refactoring Deck to extend List<Card> , rather than having List<Card> Cards as a property. However, while Cards.OrderBy (a => Guid.NewGuid ()) worked, OrderBy (a => Guid.NewGuid ()) does not: Error CS0103: The name 'OrderBy' does not exist in the current context (CS0103) Why does this not work? 回答1: Add this to the front of OrderBy as in this.OrderBy(a => Guid.NewGuid()); // a random ordering OrderBy is an extension

How to extend TCL with C++?

喜你入骨 提交于 2019-12-12 19:57:23
问题 Can I write a C++ code that can be compiled and used for extending TCL (I don't mean calling an executable file)? Can I describe some classes, functions and use them for in my TCl code by calling the compiled (.so or .a file) C++ code? If yes, then please explain me schematically how it is being done. 回答1: http://cpptcl.sourceforge.net/ It is a C++ wrapper of Tcl C API and very convenient to use, well documented. I tell this from my personal experience. C++/Tcl is a library that allows to

MVC extension method error

蹲街弑〆低调 提交于 2019-12-12 18:44:49
问题 Hi i've got an extension method in my PagingHelpers class: namespace SportsStore.WebUI.HtmlHelpers { public static class PagingHelpers { public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int, string> pageUrl) { StringBuilder result = new StringBuilder(); for (int i = 1; i < pagingInfo.TotalPages; i++) { TagBuilder tag = new TagBuilder("a"); tag.MergeAttribute("href", pageUrl(i)); tag.InnerHtml = i.ToString(); if (i == pagingInfo.CurrentPage) tag

Extensions Method trying to call protected methods / wrong exhibition of dynamic parameters

冷暖自知 提交于 2019-12-12 14:28:00
问题 I have a class named NIFERepository . It contains a SaveObject method, which returns nothing and performs an operation on a database. public class NIFERepository<E> { protected void SaveObject(object saveObject) { if (saveObject is E) this.RepositorySession.Merge(saveObject); } } 1 . I wish to create a public extension method who calls this SaveObject() method, like this: public static class NIFERepositoryExtensions { public static T Save<T, E>(this T self, E saveObject) where T :

null target of extension method

别说谁变了你拦得住时间么 提交于 2019-12-12 13:38:37
问题 public static IFoo Bar<T>(this IFoo target, ...) { // I'm curious about how useful this check is. if (target == null) throw new ArgumentNullException("target"); ... } (1) The code above seems strange to me because I feel like in any case the calling code would be the one to check that. Is there some subtlety to extension methods in this area that applies? (2) Is there a legitimate pattern that leverages the fact that the target can be null? I ask this as a consequence of wondering why calling

C# extension method for setting to new instance if null

只愿长相守 提交于 2019-12-12 12:16:17
问题 I have the following extension methods to help me check and instantiate objects if they are null. The top two work just fine but they are not very useful. public static bool IsNull<T>(this T t) { return ReferenceEquals(t, null); } public static T NewIfNull<T>(this T t, Func<T> createNew) { if (t.IsNull<T>()) { return createNew(); } return t; } public static void Ensure<T>(this T t, Func<T> createNew) { t = t.NewIfNull<T>(createNew); } Ultimately I would like to do something like IList<string>

Extending Enum in C#

橙三吉。 提交于 2019-12-12 11:17:12
问题 I was wondering whether or not I can extend the Enum type in C# to implement my custom Enum.GetValues(type) and call it like Enum.GetMyCustomValues(type) I am trying to implement something like this: public static bool IsFlagSet<T>(this T value, T flag) where T : Enum { return (value & flag) != (T)0; } but it cannot be done... any work arounds I can do? Cheers 回答1: Extensions work on instances, not for creating static methods. You can extend the base Enum using public static void MyExtensions

Handling null in extension method

戏子无情 提交于 2019-12-12 11:16:06
问题 I have a simple extension method for string class which will strip all non numeric characters from a string. So if I have a string like for example a phone number such as "(555) 215-4444" it will convert it to "5552154444". It looks like this: public static string ToDigitsOnly(this string input) { Regex digitsOnly = new Regex(@"[^\d]"); return digitsOnly.Replace(input, String.Empty); } I am just wondering what is the most elegant way to handle a null value here? Is there a typical pattern to

IQueryable<T> Extension Method not working

假如想象 提交于 2019-12-12 10:49:50
问题 How can i make an extension method that will work like this public static class Extensions<T> { public static IQueryable<T> Sort(this IQueryable<T> 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]; //}