extension-methods

Copy properties between objects using reflection and extesnion method

僤鯓⒐⒋嵵緔 提交于 2019-12-24 01:14:46
问题 This is my code where I create a "copy" of one object (Entity) into a custom object. It copies just properties with the same name in both source and target. My problem is when an Entity has a navgiaton to another Entity, for this case I added a custom attribute that I add above the property in the custom class. For example the custom class looks like: public class CourseModel:BaseDataItemModel { public int CourseNumber { get; set; } public string Name { get; set; } LecturerModel lecturer;

Pass HtmlHelper instance to another method MVC3 with Razor

元气小坏坏 提交于 2019-12-24 00:53:56
问题 Ok I have created the following two methods. The first one is an extension method on HtmlHelper. The second one gets passed that instance of the helper and it makes the checkboxes. My actual example has nothing to do with checkboxes, this was just the easiest way to explain my issue. public static MvcHtmlString MakeBoxGroup(this HtmlHelper<T> Html, List<string> names) { string outStr = ""; foreach(string name in names) outStr += MakeBox(Html, name); return new MvcHtmlString(outStr); } public

Constrain generic extension method to base types and string

半世苍凉 提交于 2019-12-23 21:17:55
问题 I want to have an extension method for XElement/XAttribute that allows me to apply a "ValueOrDefault" logic - perhaps with various slightly different implementations: ValueOrNull, ValueOrDefault, NumericValueOrDefault (which validates if the value is numeric), but I want to constrain these methods so that they can only work with ValueTypes or String (i.e. it does not really make sense to use any other reference types. Is it possible to do this with one implementation of each method, or will I

How do you have shared extension methods in Azure Functions without nesting classes?

淺唱寂寞╮ 提交于 2019-12-23 19:15:12
问题 I know how to include other .csx files via #load . However, extension methods have to be defined in a top-level class, and #load nests the class, so it's not top-level and the function throws a compilation error. How do I use shared code without having it nest inside the function's post-compiled class? 回答1: In order to correctly include extension methods for Azure Function you can use a #load as in all other cases, the only difference regarding the requirement that the extension methods needs

Why is there an error CS1973 when trying to invoke extension method with dynamic argument

て烟熏妆下的殇ゞ 提交于 2019-12-23 16:44:57
问题 Consider the following code: internal static class Program { public static string ExtensionMethod(this string format, dynamic args) { return format + args.ToString(); } private static void Main() { string test = "hello "; dynamic d = new { World = "world" }; // Error CS1973 'string' has no applicable method named 'ExtensionMethod' // but appears to have an extension method by that name. // Extension methods cannot be dynamically dispatched. // Consider casting the dynamic arguments or calling

Extension method on lambda expression

元气小坏坏 提交于 2019-12-23 16:33:57
问题 I have a helper method which gets the name of a property defined by a lambda which works as below: ExpressionUtil.GetName((Thing t) => t.Property); // returns "Property" I would like to turn this into an extension method so the syntax would be of the form: ((Thing t) => t.Property).GetName(); // wont compile : operator '.' cannot be applies to operand of type 'lambda expression' However I cant seem to do this as ((Thing t) => t.Property) is a lambda (not an expression or Func yet). Is there

Extension method on Nullable<T> not called unless generic type is set explicitly

让人想犯罪 __ 提交于 2019-12-23 16:17:39
问题 While investigating another question: "Why there is no Nullable<T>.Equals(T value) method?" I made an extension method on Nullable<T> that exposed a generic Equals<T>(T) method: public static class NullableExtensions { public static bool Equals<T>(this T? left, T right) where T : struct, IEquatable<T> { if (!left.HasValue) return false; return right.Equals(left.Value); } } However, calling it like so: double? d = null; d.Equals(0.0); Calls into the base Equals(object) with boxing, as you can

Debugging through an extension method

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 12:34:46
问题 I've created a method in C# that extends the string datatype, creating an additional overload to the Split function so that a text qualifier can be defined. Example string data is defined as "field 1","field 2","filed 3" string[] splitData = data.Split(',','"') The extension works fine. I can access the method once I reference and use the namespace. However there is an issue in the method I'm trying to debug, but the debugger won't step into the extension method. Extension Code namespace

C# Extension Methods Architecture Question

半城伤御伤魂 提交于 2019-12-23 12:29:30
问题 I recently asked this question: Compiler error referencing custom C# extension method Marc Gravell answer was perfect and it solved my problem. But it gave me something to think about... If and Extension method must be placed on a Static Class and the method itself must be static, why can't we create a static Extension method? I understand that the parameter marked as "this" will be used to allow access to an instance of the object we are extending. What I do not understand is why can't a

Understanding the extension ElementAt(index)

╄→гoц情女王★ 提交于 2019-12-23 12:08:20
问题 Consider this code: int size = 100 * 1000 * 1000; var emu = Enumerable.Range(0, size); var arr = Enumerable.Range(0, size).ToArray(); when I call emu.ElementAt(size-10) and arr.ElementAt(size-10) and measure the time the arr is much faster (the array is 0.0002s compared to IEnumerable 0.59s). As I understand it, the extention method ElementAt() have the signature public static TSource ElementAt<TSource>(this IEnumerable<TSource> source, int index) and since the 'source' is a IEnumerable the