extension-methods

Method overloading using derived types as parameters in Java

ε祈祈猫儿з 提交于 2019-12-01 05:57:47
问题 Let's say I have existing code which I want to extend but also to avoid changing it as much as possible. Somewhere around this code there is a method that receives some type. Engine.method(Base b) Now, I want to extend this functionality. So I extends Base into a type called Derived which holds some more data I need and I also implements another method that receives the Derived type and do something different with it. Engine.method(Derived d) But I don't want to change the original call to

Error message in XSLT with C# extension function

╄→尐↘猪︶ㄣ 提交于 2019-12-01 05:52:32
I am received the following error while trying to implement a C# extension function in XSLT. Extension function parameters or return values which have CLR type 'Char[]' are not supported.** code: <xsl:variable name="stringList"> <xsl:value-of select="extension:GetList('AAA BBB CCC', ' ')"/> </xsl:variable> <msxsl:script language="C#" implements-prefix="extension"> <![CDATA[ public string[] GetList(string str, char[] delimiter) { ... ... return str.Split(delimiter, StringSplitOptions.None); } ]]> </msxsl:script> Can someone explain this error message and how to get past it? EDIT: I need a

How can I add this method as an extension method to properties of my class?

廉价感情. 提交于 2019-12-01 04:27:57
问题 I have a method and I want to add this method as an extension method to properties of my class. This method give an expression as input parameter. The method is like below : public static string GetPropertyName<T>(Expression<Func<T>> propertyExpression) { return (propertyExpression.Body as MemberExpression).Member.Name; } I want to use this method like below example : string propertyName = MyClass.Property1.GetPropertyName(); Is it possible? if yes, what is the solution? 回答1: No, it's not

Is it possible to create an extension method to format a string?

北慕城南 提交于 2019-12-01 03:31:44
the question is really simple. How do we format strings in C#? this way: string.Format("string goes here with placeholders like {0} {1}", firstName, lastName); Now, is it possible to create an extension method to do it this way? "string goes here {0} {1}".Format(firstName, lastName); That's all. Well, it's more complicated than it looks. Others say this is possible, and I don't doubt them, but it doesn't seem to be the case in Mono. There, the standard overloads of the Format() method seem to take precedence in the name resolution process, and compilation fails because a static method ends up

Why can you not invoke extension methods directly?

本小妞迷上赌 提交于 2019-12-01 03:18:41
Can someone explain to me why in the following the 3rd invocation of DoSomething is invalid? ( Error message is "The name 'DoSomething' does not exist in the current context" ) public class A { } public class B : A { public void WhyNotDirect() { var a = new A(); a.DoSomething(); // OK this.DoSomething(); // OK DoSomething(); // ?? Why Not } } public static class A_Ext { public static void DoSomething(this A a) { Console.WriteLine("OK"); } } Extension methods are still static methods, not true instance calls. In order for this to work you would need specific context using instance method syntax

Add new property to string class C# [duplicate]

会有一股神秘感。 提交于 2019-12-01 03:14:56
问题 This question already has answers here : Can I add extension methods to an existing static class? (15 answers) Closed 5 years ago . I am wondering if it is possible to add a new property as an extension property to the string class. What I'm looking for is something like this string.Empty I would like to make an extension, ex: string.DisplayNone; Can I add extension properties to the string C# class that I can call in a similar manner like when I do string.Empty? 回答1: You can only build

How to get MethodInfo for generic extension method?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 03:13:27
I have an IEnumerable<T> , and I want to call the Enumerable.Contains method by reflection. I'm just struggling to get the syntax right. Here's what I currently have: var containsMethod = typeof(Enumerable).GetMethod("Contains", new[] { typeof(IEnumerable<T>), typeof(T) }); This just comes back with a null. What is the correct way to get the MethodInfo ? What is the correct way to get the MethodInfo? You have to find the generic method - which is unfortunately a bit of a pain - and then construct that with the appropriate arguments. In this case you know that there are only 2 Contains

extension method on type and nullable<type>

被刻印的时光 ゝ 提交于 2019-12-01 03:13:00
For sake of simplicity, let's assume I want to write an extension method for the type int? and int: public static class IntExtentions { public static int AddOne(this int? number) { var dummy = 0; if (number != null) dummy = (int)number; return dummy.AddOne(); } public static int AddOne(this int number) { return number + 1; } } Can this be done using only 1 method? Unfortunately not. You can make the int? (or whichever nullable type you are using) method call the non nullable method very easily though, so you don't need to duplicate any logic with 2 methods - e.g. public static class

Generic extension method for automapper

强颜欢笑 提交于 2019-12-01 03:10:24
问题 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,

Using extension methods defined in C# from F# code

我的未来我决定 提交于 2019-12-01 03:04:56
I have a series of extension methods defined for various classes in a C# library. I'm currently writing some F# code and instead of rewriting that code I would simply like to use my existing extension methods in my F# code. I have added a reference to the library and used the open statement to import the namespace, but the extension methods to not appear in F# Update: In the current version of F#, you can simply consume extension methods by adding open TheNamespaceOfExtensionMethod to your source file (just like you do in C# with a using directive) and simply call the extension method as if it