extension-methods

Writing an extension method for Entity Framework

别说谁变了你拦得住时间么 提交于 2021-02-08 10:13:11
问题 I would like to have an extension method called FirstOrDefaultCache() which would check dbContext.EntityName.Local.FirstOrDefault(condition) , and only if that is null, check dbContext.EntityName.FirstOrDefault(condition) . I got the following from another post, which works okay: public static TEntity FirstOrDefaultCache<TEntity>(this DbSet<TEntity> queryable, Expression<Func<TEntity, bool>> condition) where TEntity : class { return queryable .Local.FirstOrDefault(condition.Compile()) // find

IDictionary to string

南笙酒味 提交于 2021-02-08 06:39:43
问题 I have created an IDictionary extension to write IDictionary Exception.Data values to a string. The extension code: public static class DictionaryExtension { public static string ToString<TKey, TValue>(this IDictionary<TKey, TValue> source, string keyValueSeparator, string sequenceSeparator) { if (source == null) throw new ArgumentException("Parameter source can not be null."); return source.Aggregate(new StringBuilder(), (sb, x) => sb.Append(x.Key + keyValueSeparator + x.Value +

IDictionary to string

大城市里の小女人 提交于 2021-02-08 06:39:39
问题 I have created an IDictionary extension to write IDictionary Exception.Data values to a string. The extension code: public static class DictionaryExtension { public static string ToString<TKey, TValue>(this IDictionary<TKey, TValue> source, string keyValueSeparator, string sequenceSeparator) { if (source == null) throw new ArgumentException("Parameter source can not be null."); return source.Aggregate(new StringBuilder(), (sb, x) => sb.Append(x.Key + keyValueSeparator + x.Value +

In ETAS INCA, is there a way to quickly retrieve an item from an arbitrary path in an ASAP2Project?

折月煮酒 提交于 2021-02-07 11:12:53
问题 I'm trying to connect to INCA through the .NET API in order to navigate the folder structure of an ASAP2 project. Specifically, I want to get an object that represents the "Target" folder that I've highlighted below. This is proving pretty tricky--The API provides a ton of classes that have the name "Folder" in them: Folder , Asap2ProjectFolder , and IncaFolder . So what do I need to know in order to retrieve the "Target" folder? 回答1: Your first impulse might be to think of the Target folder

Rust equivalent to Swift's extension methods to a protocol?

人走茶凉 提交于 2021-02-05 06:30:45
问题 In Swift I can attach extension methods to any of struct , enum or protocol (same with trait in Rust). protocol Foo1 { func method1() -> Int } extension Foo1 { func method2() { print("\(method1())") } } Then all types conforming the protocol Foo1 now all have method2() . This is very useful to build "method chaining" easily. How to do same in Rust? This doesn't work with an error. struct Kaz {} impl Foo for Kaz {} trait Foo { fn sample1(&self) -> isize { 111 } } impl Foo { fn sample2(&self) {

How to add variable in existing class in swift?

妖精的绣舞 提交于 2021-02-05 05:27:05
问题 I know that in swift we can use Extensions to add new methods to existing classes. But what about if i want to add a variable? extension UIViewController { var myVar = "xyz" } It gives like : Extensions must not contain stored properties 回答1: We can't add the stored properties to extensions directly but we can have the computed variables . Extensions in Swift can: Add computed instance properties and computed type properties Define instance methods and type methods Provide new initializers

Invoke Enumerable.Where (or other overloaded generic method) using reflection

守給你的承諾、 提交于 2021-02-04 15:47:20
问题 There are 2 overloads (or method signatures) of the "Where" method in Enumerable class: namespace System.Linq { public static class Enumerable { public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate); public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, int, bool> predicate); } So var where = typeof(Enumerable).GetMethod("Where") throws an exception stating an ambiguous match because, of

Use Contains() extension method on arrays in C#

不羁岁月 提交于 2021-01-29 03:31:26
问题 There is a Contains() extension method on IEnumerable; In VB I am able to do this: If New String() {"A", "B"}.Contains("B") Then ' ... End If What would be the equivalent of this in C#? 回答1: It is the same idea in C#, using LINQ extension methods: using System.Linq; ... if (new string[] {"A", "B"}.Contains("B")) { ... } 来源: https://stackoverflow.com/questions/1972820/use-contains-extension-method-on-arrays-in-c-sharp

Swift3 Extension on “Any?” -ish class?

旧时模样 提交于 2021-01-29 03:15:26
问题 Shorter explanation: You often want to extend on "target" ... and targets are usually Any? . But you can't have an extension on Any . How to do it? Consider this, extension UIViewController { func add(tap v:UIView, _ action:Selector) { let t = UITapGestureRecognizer(target: self, action: action) v.addGestureRecognizer(t) } } Excellent, you can now... self.tap(redButton, #selector(clickedRedButton)) ... in any view controller. But you can do the same thing to just about any target. So, to use

How to avoid service locator in .net extension methods

蹲街弑〆低调 提交于 2021-01-27 05:48:41
问题 I'm looking for a clean pattern to use dependencies in .Net extension methods without explicitly newing-up or using a service locator: public static class HttpContextExtensions { public static SomeClass ExtensionMethod(this HttpContext context) { //looking to avoid this var dependency = ServiceLocator.GetService<DependencyType>(); return dependency.DoSomething(context); } } Am I barking up the wrong tree here? Should I be looking for a more direct solution that passes context into a method? I