extension-methods

Using reflection to check if a method is “Extension Method”

家住魔仙堡 提交于 2019-11-28 09:06:21
As part of my application I have a function that receives a MethodInfo and need to do specific operations on it depending if that method is "Extension Method". I've checked the MethodInfo class and I could not find any IsExtension property or flag that shows that the method is extension. Does anyone knows how can I find that from the method's MethodInfo? Brian Based on F# extension methods in C# it seems there is an attribute on the compiled form. So see if the method has this attribute: http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.extensionattribute.aspx You can

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

时光怂恿深爱的人放手 提交于 2019-11-28 07:49:58
问题 This question already has answers here : Partial type inference (3 answers) Closed 5 years ago . 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

How to use GetMethod for static extension method

和自甴很熟 提交于 2019-11-28 07:29:16
问题 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); 回答1: Use this extension method to get other extension methods: public

How to create extension methods for Types

不羁岁月 提交于 2019-11-28 07:01:44
问题 I am writing an extension method for parsing JSON string for any given type. I wanted to use the method on types instead of instances like many examples we already know, but I somewhat feel it is not supported by Visual Studio. Can someone enlighten me here? The following is the method: public static T ParseJson<T>(this T t, string str) where T: Type { if (string.IsNullOrEmpty(str)) return null; var serializer = new JavaScriptSerializer(); var obj = serializer.Deserialize<T>(str); return obj;

How to use EntityFramework.BulkInsert?

[亡魂溺海] 提交于 2019-11-28 05:16:58
问题 I'm trying to use EntityFramework.BulkInsert located at http://efbulkinsert.codeplex.com/. It's an extension for Entity Framework. I installed the proper NuGet package and added using EntityFramework.BulkInsert to the top of my class. When I go to do a context.BulkInsert(entities) "BulkInsert" has a red line underneath as if it doesn't exist. Can someone help me in the proper usage of this extension? I haven't used an extension method before. You are supposed to be able to just do this:

Extension methods for sealed class in c#

让人想犯罪 __ 提交于 2019-11-28 04:49:45
问题 I have this sealed class, public sealed class A { public string AName {get;set;} } and someone can write an extension method for it like this: public static class Extensions { public static void ExtensionMethodForA (this A a) { Console.WriteLine("A's Extension method!"); } } The question is, how do you prevent that ? 回答1: You don't. You can't. And you shouldn't want to. Instance methods are always preferred to extension methods, so it should not present a conflict. Other than that, they are

Try-with-resources in Kotlin

你。 提交于 2019-11-28 04:47:52
When I tried to write an equivalent of a Java try -with-resources code in Kotlin, it didn't work for me. I tried different variations of the following: try (writer = OutputStreamWriter(r.getOutputStream())) { // ... } But neither works. Does anyone know what should be used instead? Apparently Kotlin grammar doesn't have definition for such a construct, but maybe I'm missing something. It defines grammar for try block as follows: try : "try" block catchBlock* finallyBlock?; There is use -function in kotlin stdlib ( src ). How to use it: OutputStreamWriter(r.getOutputStream()).use { // by `it`

Extension method priority

我是研究僧i 提交于 2019-11-28 04:45:20
问题 I read from https://msdn.microsoft.com/en-us/library/vstudio/bb383977.aspx that extension methods with same name and signature as existing ones on the base type are never called, but what about "overriding" an extension-method itself: using System; using System.Linq; using System.Collections.Generic; namespace ConsoleApplication1 { public class Program { public static void Main(string[] args) { var query = (new[] { "Hans", "Birgit" }).Where(x => x == "Hans"); foreach (var o in query) Console

Is it necessary to throw a NullReferenceException from an extension method? [duplicate]

回眸只為那壹抹淺笑 提交于 2019-11-28 04:18:44
问题 This question already has an answer here: ArgumentNullException or NullReferenceException from extension method? 5 answers If I define an extension method such as this: static public String ToTitleCase(this string instance, CultureInfo culture) { if (instance == null) throw new NullReferenceException(); if (culture == null) throw new ArgumentNullException("culture"); return culture.TextInfo.ToTitleCase(instance); } Is it necessary for me to check the string instance for null and throw an null

Error when using extension methods in C#

非 Y 不嫁゛ 提交于 2019-11-28 04:17:33
I came across an issue that makes me think there is bug in the 3.0 framework. When I try to use extension methods I get the following error: Missing compiler required member 'System.Runtime.CompilerServices.ExtensionAttribute..ctor' When using this simple code: public static class StringUtils { static void TestExtension(this String targetString) { } } The only way to make this compile error go away is to add the following code: namespace System.Runtime.CompilerServices { public class ExtensionAttribute : Attribute { } } It's been a few months since I have used extensions methods, but I'm