generics

How to use an instance initializer with a generic HashMap?

Can you use an instance initializer with a generic HashMap? I found this code online, but am having trouble converting it to a generic HashMap instead of a basic HashMap: someMethodThatTakesAHashMap(new HashMap(){{put("a","value-a"); put("c","value-c");}}); ...

Understanding this warning: The serializable class does not declare a static final serialVersionUID

I have some static initializer code: someMethodThatTakesAHashMap(new HashMap<K, V>() { { put("a","value-a"); put("c","value-c");} }); For some reason I am receiving a warning from Eclipse: The serializable class does not declare a static final serialVersionUID. Is this complaining about the anonymous class? What can I do about...

Dynamic Generic declaration of type T

I have an array which stores a dictionary of Types: //The dictionary: Dictionary<CacheKey,Type> TypeLookup; //This is the enum: public enum CacheKey { UserProfile, CustomerQuickSearch, CommissionConfiguration } I would like to use this Dictionary to declare a variable of type T //instead of T myvar; ...

Dynamically Building ChannelFactory

I'm trying to dynamically creating the ChannelFactory : var serviceType = GetServiceProxy(); var interfaceType = serviceType.GetServiceInterface(); //return IServiceInterface var service = new ChannelFactory(binding, address); the problem is, as you can see, on the second line, where I don't have the generic type, and unfortunately...

How to convert list type in Java hibernate

Hello there. In java application I use hibernate criteria queries, for example: Criteria criteria = session.createCriteria(Any.class); ... List<?> list = criteria.list(); and I definetly know that result list contains only objects of type Any but I don't know if it's possible to get list of type defined above? In this case, if I ...

Does .NET have a way to check if List a contains all items in List b?

I have the following method: namespace ListHelper { public class ListHelper<T> { public static bool ContainsAllItems(List<T> a, List<T> b) { return b.TrueForAll(delegate(T t) { return a.Contains(t); }); } } } The purpose of which is to determine if...

How to handle paging IQueryable with Linq To Entities? (problem with OrderBy)

Hi There, I am currently building a simple ASP.NET MVC site using Linq to Entities. My first foray into this world was nerd dinner, which is where I found the paginated list code that I am attempting to use. The code looks as follows: public class PaginatedList<T> : List<T> { public int PageIndex { get; private set; } public i...

How to get the last object in a generic list?

I am passing a populated SelectList to my View and I want to default the selected value to the last record in the list. So far I've got: IQueryable<BuildNumber> projBuildNos = tcRepository.GetProjectBuildNos(); BuildList = new SelectList(projBuildNos, "ID", "value", projBuildNos.Last().ID); I get an exception saying "the query operat...

C#: How to find and create instances which fullfills multiple type constraints

Ok, maybe that title didn't make much sense, but here is the deal. Say I have a generic method with multiple type constraints, this this: public static void DoSomethingAwesome<T>(T thing) where T : IThing, IAwesome, IComparable<T> { ... } Now.... how can I, using reflection, create something I can send in there? If it was on...

Help with C#.NET generic collections performance and optimization

I am trying to optimize a piece of .NET 2.0 C# code that looks like this: Dictionary<myType, string> myDictionary = new Dictionary<myType, string>(); // some other stuff // inside a loop check if key is there and if not add element if(!myDictionary.ContainsKey(currentKey)) { myDictionary.Add(currentKey, ""); } Looks like the Dictio...

ORMs that work with complex .Net generic objects (e.g. nested List<...>, etc)?

I am just starting to model the data for a new project, which must be persistable. Looks like the most natural OO model will have lots of nested .Net generics. Lists of objects, and these objects will also contain Lists of other generic types, and so on, nested at least three levels deep. Ideally, I'd like to just design the data mo...

How to upcast an object of type IEnumerable<Foo> to IEnumerable<IFoo> if passed to the function as object?

I'm trying to upcast a parameter passed as an object (which is an upcast of an object of type IEnumerable) to IEnumerable where Foo implements IFoo. Here is an example of what I'd like to do but it does not work. public void F(object o) { //I know the object o is of type IEnumerable<Foo> where Foo implements IFoo IEnumerable<IF...

Extension methods for specific generic types

I'm attempting to create various extension method for a generic type bound to specific generic type parameters in F#, but the language does not seem to be allowing me: What I want to do is something like the following: type IEnumerable<int> with member this.foo = this.ToString() Yet it gives me the compiler error (underli...

C#: Generics syntax question

What's the syntax to require T also be IComparable in this class definition? public class EditItems<T> : Form ...

Java Generics for class objects implementing a specific interface

I am trying to define HashMap< ? , String> where ? is a class object from some implementation of a given interface, P. For example, for interface ISearchEngine, i want ? to possibly be Google.class, Yahoo.class, Bing.class, etc. ...

Calling static member when you have only generic parameter

Ts there any way to call a static member on type when I only have a generic parameter. For example if I have something like this public Get<T>(int id) { // I would like to do this string s = T.SomeMethodName(); } I could do it like this but is kinda "yucky", and then it does not matter if it is static or not. Or I could use refl...

Structure map and generics (in XML config)

Hi I'm using the latest StructureMap (2.5.4.264), and I need to define some instances in the xml configuration for StructureMap using generics. However I get the following 103 error: Unhandled Exception: StructureMap.Exceptions.StructureMapConfigurationException: StructureMap configuration failures: Error: 103 Source: Requested Plugi...

Generic method to set Control values

Hi, I'm trying to clean up some pretty ugly form population code in the code behind of an aspx that looks like this: SchoolCensus1.Checked = (bool)questions[0].SchoolCensus1; SchoolCensus1Info.Text = questions[0].SchoolCensus1Info; SchoolCensus2.Checked = (bool)questions[0].SchoolCensus2; SchoolCensus2Info.Text = questions[0].SchoolCen...

C# get the the type Generic<T> given T

Hello. I have a generic class in C#, like this: public class GenericClass<T> { ... } Now, I have the Type object for an object, and would like to, through reflection or otherwise, to get the Type object for GenericClass<T> where T corresponds to that Type object I have my object. Like this: Type requiredT = myobject.GetType()...

Generics: Type as variable?

hello In order to be able to substitute a specific implementation, it is commonly known to write List<AnyType> myList = new ArrayList<AnyType>(); instead of ArrayList<AnyType> myList = new ArrayList<AnyType>(); This is easy to understand, this way you might change the implementation from ArrayList to LinkedList or any other kind o...