extension-methods

Preserving state in an extension method

混江龙づ霸主 提交于 2019-12-01 14:31:41
问题 The C# team has previously considered adding extension properties, events, etc. to C#. Per Eric Lippert: http://blogs.msdn.com/b/ericlippert/archive/2009/10/05/why-no-extension-properties.aspx For these features to be useful however, they would have to be able to store some new kind of state with an object. It seems like the only way to do this would be to use a dictionary and associate each instance of an object with whatever the additional state is. It would be useful if it were possible to

How can I set properties on all items from a linq query with values from another object that is also pulled from a query?

北战南征 提交于 2019-12-01 14:07:21
问题 I have a query pulling from a database: List<myClass> items = new List<myClass>(from i in context select new myClass { A = i.A, B = "", // i doesn't know this, this comes from elsewhere C = i.C } I also have another query doing a similar thing: List<myClass2> otherItems = new List<myClass2>(from j in context select new myClass2 { A = j.A, // A is the intersection, there will only be 1 A here but many A's in items B = j.B } In reality these classes are much larger and query data that is

IQueryable Extension: create lambda expression for querying a column for a keyword

人走茶凉 提交于 2019-12-01 13:45:13
I started with the IQueryable extension methods from this example on CodePlex . What i believe i need is an IQueryable extension method to "Where", where the method signature looks like: public static IQueryable<T> Where<T>(this IQueryable<T> source, string columnName, string keyword) and effectively does this (assuming T.columnName is of type string): source.Where(p => p.ColumnName.Contains("keyword")) using the above CodePlex example, i think i understand how he got the OrderBy method working, but my problem seems a bit more complex and I don't know how to get the Contains("keyword") part

Enum from string, int, etc

最后都变了- 提交于 2019-12-01 13:42:54
问题 Using extension method we can create methods to convert an enum to other datatype like string, int by creating extension methods ToInt() , ToString() , etc for the enum. I wonder how to implement the other way around, e.g. FromInt(int) , FromString(string) , etc. As far as I know I can't create MyEnum.FromInt() (static) extension method. So what are the possible approaches for this? 回答1: I would avoid polluting int or string with extension methods for enums, instead a good old fashioned

Changing size of array in extension method does not work?

与世无争的帅哥 提交于 2019-12-01 12:28:04
So basically I wrote my little Add extension method for array types. using System; using System.Linq; public static class Extensions { public static void Add<T>(this T[] _self, T item) { _self = _self.Concat(new T[] { item }).ToArray(); } } public class Program { public static void Main() { string[] test = { "Hello" }; test = test.Concat(new string[] { "cruel" }).ToArray(); test.Add("but funny"); Console.WriteLine(String.Join(" ", test) + " world"); } } The output should be Hello cruel but funny world , but the but funny will never be concatenated within the extension method. Editing the same

C# Extension Method for String Data Type

Deadly 提交于 2019-12-01 10:56:14
My web application deals with strings that need to be converted to numbers a lot - users often put commas, units (like cm, m, g, kg) and currency symbols in these fields so what I want to do is create a string extension method that cleans the field up and converts it to a decimal. For example: decimal myNumber = "15 cm".ToDecimal(); Are you expecting users of different 'cultures' to use your application? If so it's better to factor in the user's regional settings: static decimal ToDecimal(this string str) { return Decimal.Parse(str, CultureInfo.CurrentCulture); } Or you could replace every

Changing size of array in extension method does not work?

守給你的承諾、 提交于 2019-12-01 10:32:04
问题 So basically I wrote my little Add extension method for array types. using System; using System.Linq; public static class Extensions { public static void Add<T>(this T[] _self, T item) { _self = _self.Concat(new T[] { item }).ToArray(); } } public class Program { public static void Main() { string[] test = { "Hello" }; test = test.Concat(new string[] { "cruel" }).ToArray(); test.Add("but funny"); Console.WriteLine(String.Join(" ", test) + " world"); } } The output should be Hello cruel but

Extension methods with generics

邮差的信 提交于 2019-12-01 09:43:18
I was looking at this question and i was curious why this deosn't compile. Given this code, can anyone explain why the call to IBase.Test() doesn't resolve to the correct extension method? public interface IBase { } public interface IChildA : IBase { } public interface IChildB : IBase { } public static class BaseExtensions { public static IBase Test(this IBase self) { return self; } public static T Test<T>(this T self) where T : IChildB { return self; } } public static class TestClass { public static void Test() { IChildA a = null; //Yeh i know its null but just testing for compile here..

Generic extension method for automapper

≯℡__Kan透↙ 提交于 2019-12-01 06:35:16
public abstract class Entity : IEntity { [Key] public virtual int Id { get; set; } } public class City:Entity { public string Code { get; set; } } public class BaseViewModel:IBaseViewModel { public int Id { get; set; } } public class CityModel:BaseViewModel { public string Code { get; set; } } my domain and view classes... and for mapping extension public static TModel ToModel<TModel,TEntity>(this TEntity entity) where TModel:IBaseViewModel where TEntity:IEntity { return Mapper.Map<TEntity, TModel>(entity); } and i am using like below City city = GetCity(Id); CityModel model = f.ToModel

Why generic extension method with constraint is not recognized as extension method? [duplicate]

只愿长相守 提交于 2019-12-01 06:06:00
Possible Duplicate: No type inference with generic extension method Consider two methods: public static IEnumerable<V> Merge<V> (this IEnumerable<IEnumerable<V>> coll) public static IEnumerable<V> Merge<T, V> (this IEnumerable<T> coll) where T : IEnumerable<V> Both compile just fine, in both cases the type of generic types will be known at compile time of caller, and thus the exact type of extended type. You can call both fine, but only the first one as extension. Why? Update 1 To see it fail, use the second method and such example: var x = new List<List<int>>(); var y = x.Merge(); Update --