logo
down
shadow

replace inheritance with generic extension method


replace inheritance with generic extension method

By : Yogini Lalwani
Date : November 21 2020, 04:01 AM
it helps some times I created a class that inherits from KeyedByTypeCollection and extends it. , You will have to define V.
code :
public static T ValueOrDefault<T,V>(this KeyedByTypeCollection<V> collection) where T : V
public static bool TryGetValue<T,V>(this KeyedByTypeCollection<V> collection, out T value) 
       where T : V
public class A { }
public class B : A { }
var myCollection = new KeyedByTypeCollection<A>();
myCollection.Add(new A());
myCollection.Add(new B());

myCollection.TryGetValue(out B b); // <-- Nice! :)
b = myCollection.ValueOrDefault<B,A>();  // <-- Ugly :(


Share : facebook icon twitter icon
Call a generic extension method on another generic extension method's parameter

Call a generic extension method on another generic extension method's parameter


By : patgar
Date : March 29 2020, 07:55 AM
Does that help e is not a nullable struct; it's just a struct. GetDescription takes a nullable struct.
Either make e nullable, or make a non-nullable version of GetDescription.
a generic extension method for classes with common methods but no inheritance

a generic extension method for classes with common methods but no inheritance


By : bharathi raj
Date : March 29 2020, 07:55 AM
help you fix your problem Ideally, you should make your types implement IList or IReadOnlyList. However, I'm guessing you can't do that...
You can do this with builtin methods (making use of the fact that they are IEnumerable):
code :
var list = myIManDocuments.Cast<IManDocument>().ToList();
public static List<T> ToList<T>(dynamic source)
{
    List<T> results = new List<T>(source.Count);
    for (int i = 1; i <= source.Count; i++)
    {
        results.Add(source.ItemByIndex(i));
    }
    return results;
}
var list = MyUtil.ToList<IManDocument>(myIManDocuments);
Generic extension method is slower than non-generic extension method

Generic extension method is slower than non-generic extension method


By : Milosz Plichta
Date : March 29 2020, 07:55 AM
Hope this helps The problem was that I used the non-generic version IComparable instead of IComparable which caused a performance-degregation. Now, using IComparable the generic version runs as fast as the type-specific one.
code :
public static int LinearSearch<T>(this List<T> list, int startIndex, T value) 
    where T : IComparable<T>
{
    for (int i = startIndex; i < list.Count; i++)
        if (list[i].CompareTo(value) >= 0)
            return i;

    return -1;
}
Why can't I replace IEnumerable<T> by a generic type variable in extension method?

Why can't I replace IEnumerable<T> by a generic type variable in extension method?


By : LuYao
Date : March 29 2020, 07:55 AM
like below fixes the issue I am trying to make an extension method more generic to avoid redundancy (Here is an example of some real code, the code below is just to demonstrate the issue - I had the idea to make the method available for IQueryable as well). ,
Why?
code :
where E : System.Linq.IQueryable<T>
Generic Repository Extending: Inheritance vs. Extension Methods

Generic Repository Extending: Inheritance vs. Extension Methods


By : Robin
Date : March 29 2020, 07:55 AM
I hope this helps you . Don't create query methods on repository. Use repositories to maintain aggregate consistency not for generic querying. Otherwise your interface is very bloated and your repositories just end up a dumping ground for a myriad of different queries. Just create queries and query handlers. I only use repository for getting data out that I am going to update.
Do something like this.
Related Posts Related Posts :
  • Linq to object Multiple Where short circuit evaluation
  • WPF - Create solid layout without designer
  • View not rendering on NET Core
  • Proper way to create domain models and viewmodels in microservices
  • StaticInjectorError(AppModule)[AuthGuard] after login
  • Getting Entity Model dynamically based on TableName?
  • Customizing the Navigation Form after my form's confirmation dialog
  • CSHTML Confirm Delete prior to deleting database record
  • How to set property values for indexers using Expressions?
  • My Mandelbrot sketching program in c# isn't working
  • Best way to validate a date string in C#
  • Bot Framework v4 Bot emulator authentication bad request
  • Can I use MVC 5 remote validation to check for duplicate values, but exclude the item being edited?
  • How to get list of all databases using MongodbClient class. (C# application)
  • WPF DataGrid disable cell edit based on value
  • how to check if there are 4 or more spaces in streamread line
  • How to perform style changes on datagridview rows using rows from the underlying bindingsource?
  • How do I add a C# solution file in Jetbrains Rider?
  • Docker ASP.NET CORE 2.1 app in Visual Studio 2017. App does not start in debug mode
  • ASP.NET Core WebApi with SQL Server and EFCore 2
  • Sitefinity Widget User Control
  • MultiSelectList shows one value from the IEnumerable given to it with multiple values
  • Items in picker from a list - Xamarin Code behind
  • How can I insert/update a List<T> as member of an entity in SQLite?
  • How to "flush" ApplicationInsightsTraceListener, in a Console Application, on exit?
  • Named Pipes Between Winforms and Service Can Only Connect Once
  • Add CDATA to a Node Value in a loop
  • Custom serializer for just one property in Json.NET, without changing the model class
  • How to stop a timer/change system UI OnSleep Xamarin Android
  • LINQ parse xml file and get values from specific nodes
  • How do I set the time out for page load?
  • Clarification of IL generated code referenced to strings
  • EntityFrameworkCore.SQLLite and UWP Not behaving as expected
  • WPF MediaElement: Video opened twice
  • Temporary excel file with Interop library
  • Insert \u2265 into a C# string
  • Constant Enemy Damage to Player - Unity 2019.1 Beta
  • Is there a way to scan for viruses when downloading an attachment using EWS?
  • How to generify a method when T can be multiple new () Types?
  • Reading binary file data into List of Structs
  • Updating Status Strip on Form from BackgroundWorker
  • Visual Studio using GIT repository Publishing with API and Website in one Solution
  • Return Standard Deviation of column values with other column value condition LINQ
  • Grab a count of different values in object
  • c# File used by who?
  • How to parse string into an array using set number of characters in C#
  • How to sent list using CommandParameter
  • Problem with referencing ResourceDictionaries, and Styling across multiple assemblies
  • StackExchange Redis - high CPU usage on WriteAllQueues
  • Not typing the full variable name but partial name is represented by another string value
  • Linq to Entities Selecting Data with Max Date?
  • How to extract data using Ical.Net?
  • C# program crashes because listbox items change
  • Kicking a user in Discord.Net
  • C# Linq Find duplicates with multiple group by
  • How to call control on a Panel that is inside usercontrol
  • c# ReadAsJsonAsync in framework 4.5
  • Inner join two table variables and return an output cursor from an anonymous plsql block in a c# application
  • Notify consumers when all tasks have completed without blocking the thread
  • Replace "\\" with "\" in a path
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org