generics

Generic wrapper for System.Web.Caching.Cache functions

I've created a generic wrapper for using the Cache object: public class Cache<T> where T : class { public Cache Cache {get;set;} public CachedKeys Key {get;set;} public Cache(Cache cache, CachedKeys key){ Cache = cache; Key = key; } public void AddToCache(T obj){ Cache.Add(Key.ToString(), ...

Why does using Collections.emptySet() with generics work in assignment but not as a method parameter?

So, I have a class with a constructor like this: public FilterList(Set<Integer> labels) { ... } and I want to construct a new FilterList object with an empty set. Following Joshua Bloch's advice in his book Effective Java, I don't want to create a new object for the empty set; I'll just use Collections.emptySet() instead: FilterL...

Generic Dictionary - Getting Conversion Error

The following code is giving me an error: // GetDirectoryList() returns Dictionary<string, DirectoryInfo> Dictionary<string, DirectoryInfo> myDirectoryList = GetDirectoryList(); // The following line gives a compile error foreach (Dictionary<string, DirectoryInfo> eachItem in myDirectoryList) The error...

Get derived class type from a base's class static method

Hi, i would like to get the type of the derived class from a static method of its base class. How can this be accomplished? Thanks! class BaseClass { static void Ping () { Type t = this.GetType(); // should be DerivedClass, but it is not possible with a static method } } class DerivedClass : BaseClass {} // somewhere in the...

Getting the constructor of an Interface Type through reflection, is there a better approach than looping through types?

I have written a generic type: IDirectorySource<T> where T : IDirectoryEntry, which I'm using to manage Active Directory entries through my interfaces objects: IGroup, IOrganizationalUnit, IUser. So that I can write the following: IDirectorySource<IGroup> groups = new DirectorySource<IGroup>(); // Where IGroup implements `IDirectoryEnt...

adhoc struct/class in C#?

Currently i am using reflection with sql. I find if i want to make a specialize query it is easiest to get the results by creating a new class inheriting from another and adding the 2 members/columns for my specialized query. Then due to reflections in the lib in my c# code i can write foreach(var v in list) { v.AnyMember and v.MyExtraMe...

What would be different in Java if Enum declaration didn't have the recursive part

Please see http://stackoverflow.com/questions/211143/java-enum-definition and http://stackoverflow.com/questions/3061759/why-in-java-enum-is-declared-as-enume-extends-enume for general discussion. Here I would like to learn what exactly would be broken (not typesafe anymore, or requiring additional casts etc) if Enum class was defined a...

Scala method where type of second parameter equals part of generic type from first parameter

I want to create a specific generic method in Scala. It takes two parameters. The first is of the type of a generic Java Interface (it's from the JPA criteria query). It currently looks like this: def genericFind(attribute:SingularAttribute[Person, _], value:Object) { ... } // The Java Interface which is the type of the first paramet...

Why does not IDictionary (non-generic) inherit from IEnumerable<DictionaryEntry>?

IDictionary<TKey, TValue> inherits from IEnumerable<KeyValuePair<TKey, TValue>>, but IDictionary for some reason doesn't inherit from IEnumerable<DictionaryEntry>. I wonder why? I hate to write this ugly .OfType<DictionaryEntry>() every time when I need to make a query against an IDictionary. ...

List<object>.RemoveAll - How to create an appropriate Predicate

This is a bit of noob question - I'm still fairly new to C# and generics and completely new to predicates, delegates and lamda expressions... I have a class 'Enquiries' which contains a generic list of another class called 'Vehicles'. I'm building up the code to add/edit/delete Vehicles from the parent Enquiry. And at the moment, I'm sp...

How to get array of string from List<Tuple<int, int, string>>?

Hi, I was wondering is there an elegant way of geting string[] from List<Tuple<int, int, string>>? I'm thinking of .NET way (preferable extension methods and lambda expressions :P) P.S. Code is from .NET 3.5 project, so Tuple is my own implementation. ...

Non-constructed generics as properties (eg. List<T>)

The Problem It's something I came across a while back and was able to work around it somehow. But now it came back, feeding on my curiosity - and I'd love to have a definite answer. Basically, I have a generic dgv BaseGridView<T> : DataGridView where T : class. Constructed types based on the BaseGridView (such as InvoiceGridView : Bas...

How to make Castle windsor resolve generics with constraints?

using System; using Castle.Windsor; using Castle.MicroKernel.Registration; using System.Reflection; using Castle.MicroKernel.Resolvers.SpecializedResolvers; namespace Windsor { class MainClass { public static void Main (string[] args) { var container = new WindsorContainer (); container.R...

Improving methods with variable parameters using .NET Generics

I have a lot of functions which are currently overloaded to operate on int and string: bool foo(int); bool foo(string); bool bar(int); bool bar(string); void baz(int p); void baz(string p); I then have a lot of functions taking 1, 2, 3, or 4 arguments of either int or string, which call the aforementioned functions: void g(int p1) ...

generic function where generic type is any interface

I'd like do implement a generic function with the generic constraint that the Type passed in is an interface. Is this possible in C#? I have it working fine without the constraint, but the code will fail at runtime if it is not an interface, so I'd like to have the compile time checking. public T MyFunction<T> where T : {any interface...

Overloading a params function with an IEnumerable

Suppose I have two functions: Foo(params INotifyPropertyChanged[] items) { //do stuff } Foo<T>(IEnumerable<T> items) where T : INotifyPropertyChanged { Foo(items.ToArray(); } The second one allows me to call Foo from a generic class with the constraint where T : INotifyPropertyChanged, but the second resolves to itself so I get...

AutoMapping custom Generic Types - How?!

hey guys, I'm using automapper version 1.1.0.188 In my AutoMapper.Configure I'm mapping Entities to DTOs and vice versa, like so: // entity >> DTO Mapper.CreateMap<MetaTemplate, MetaTemplateDTO>(); Mapper.CreateMap<Person, PersonDTO>(); // DTO >> Entity Mapper.CreateMap<MetaTemplateDTO, MetaTemplate>(); Mapper.CreateMap<PersonDTO...

Generic built-in EventArgs to hold only one property?

I have a number of EventArgs classes with only one field and an appropriate property to read it: public class SomeEventArgs : EventArgs { private readonly Foo f; public SomeEventArgs(Foo f) { this.f = f; } public Foo Foo { get { return this.f; } } } Is there any built-in, generic class t...

List<T>.Clear - Does it have to be called?

So I've been fighting another memory problem in my project for the past week. I tried a couple of memory profilers but nothing gave me insight into what was causing the minor memory leak. The following code turned out to be causing it: private void DeleteAll( FlowLayoutPanel flp) { List<ImageControl> AllList = GetAllList(flp); L...

Can't be as specific with Java generic field types as with generic method types?

I've got some old command-line argument parsing code I wrote 4 years ago for Java 5, and it won't compile in Java 6 thanks to changes in how generics are handled and/or whether certain library classes are generic. While trying to fix it, I came across the following problem; since it's been 4 years since I've touched Java and I was never...