generics

Interface design using C# Generics

Hi, I am currently designing a class library that will provide data to a web application graph rendering engine in C#. I am currently defining the interfaces of this library. I have a IGraphData interface which I would like to cache using a service that accesses the cache, this is called IGraphDataCacheService and has set and get met...

How to turn a Type instance into a generic type argument

I basically have something like this: void Foo(Type ty) { var result = serializer.Deserialize<ty>(inputContent); } Foo(typeof(Person)); The Deserialize<ty> doesn't work because it expects Deserialize<Person> instead. How do I work around this? I'd also like to understand how generics work and why it won't accept ty which is type...

Can I simplify this Java Generics Hierarchy

Sometimes in my code I'd like to refer to (what I think of as) the implicit type in a generics heirarchy (basically the type implied by the first ? in the following). public class Foo<BAR extends Bar<?>> { public void t(BAR x) { List l = new ArrayList(); Object baz = x.makeBaz(null); l.add(baz); x.setBazList(l); } ...

Casting value to T in a generic method

(This may be a dupe, I can't imagine someone hasn't already asked this, I probably haven't worked out the keywords I need) I have an interface for a creaky property-map: interface IPropertyMap { bool Exists(string key); int GetInt(string key); string GetString(string key); //etc.. } I want to create an extension method li...

What are the reasons why Map.get(Object key) is not (fully) generic

What are the reasons behind the decision to not have a fully generic get method in the interface of java.util.Map<K,V>. To clarify the question, the signature of the method is V get(Object key) instead of V get(K key) and I'm wondering why (same thing for remove, containsKey, containsValue). ...

Trying to get generic when generic is not available.

I am trying to develop some general purpose custom ValidationAttributes. The fact that one cannot create a generic subclass of an attribute is making me nuts. Here is my code for the IsValid override in a ValidationAttribute to verify that the value of a property is unique: public override bool IsValid(object value) { S...

Generic ServerControl syntax?

Is there a way that I can have a server control MyControl<T> so that I can register and use it in an aspx page like so <mc:MyControl<ThingForControlToUse> ID="instanceOfMyControl" runat="server" Obviously the designer doesn't like this, are there any cool ways round it other than creating a non generic wrapper with a type parameter...

The difference between implicit and explicit delegate creation (with and without generics)

See the four lines in the Go() method below: delegate void Action<T>(T arg); delegate void Action(); void DoSomething<T>(Action<T> action) { //... } void DoSomething(Action action) { //... } void MyAction<T>(T arg) { //... } void MyAction() { //... } void Go<T>() { DoSomething<T>(MyAction<T>); // throws compiler...

How do I tell if a type is a "simple" type? i.e. holds a single value

typeof(string).IsPrimitive == false typeof(int).IsPrimitive == true typeof(MyClass).IsClass == true typeof(string).IsClass == true typeof(string).IsByRef == false typeof(MyClass).IsByRef == true I have a method that instantiates a new instance of T and, if it's a "complex" class, fills its properties from a set of source data values. ...

How do I compare a generic type to its default value?

void Get<T>(Action<T> createObject) { T obj = createObject(); if(obj == default(T)) return obj; // .. do a bunch of stuff return obj; } Compiler error: Operator '==' cannot be applied to operands of type 'T' and 'T' How do I do this properly? ...

Resource for learning Java generics?

Hello I'm clearly in need of improving my knowledge in this Java field, or I'm doomed to produce "look the same but not exactly" code. I'm already have the bases... but I'm looking for a training / tuturial oriented only to this. Thanks ...

Polymorphic or generic approach is better? C#

I have two classes and an interface like interface IVehicle { void Drive(); } class Benz :IVehicle { public void Drive() { Console.WriteLine("WOW! driving benz"); } } class Ferrari : IVehicle { public void Drive() { Console.WriteLine("WOW! driving ferrari"); } } I got a Driver class which ...

How to convert object[] to List<string> in one line of C# 3.0?

ok I give up, how do you do this in one line? public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { //List<string> fields = values.ToList<string>(); //List<string> fields = values as List<string>; //List<string> fields = (List<string>)values; List<string> f...

Type safety, Java generics and querying

I have an interesting situation and I'm wondering if there is a better way to do this. The situation is this, I have a tree structure (an abstract syntax tree, specifically) and some nodes can contain child nodes of various types but all extended from a given base class. I want to frequently do queries on this tree and I'd like to get ...

Generic constructors and reflection

Is it possible to see which constructor was the generic one? internal class Foo<T> { public Foo( T value ) {} public Foo( string value ) {} } var constructors = typeof( Foo<string> ).GetConstructors(); The property 'ContainsGenericParameters' returns me for both constructors false. Is there any way to find out that constructors[0...

Sort Generic list on two or more values

We have a generic List(Of Product) that must be sorted on two or more properties of the Product class. The product class has the properties "Popular" numeric (asc), "Clicked" numeric (desc), "Name" string (asc). In order of naming the properties we want the list to sort. How can it be sort with an lamba statement? If have found to sort...

How to make this extension method compile?

I am trying to create an extension method for the generic delegate Action<T> to be able to make simple asynchronous calls on Action<T> methods. It basically just implements the pattern for when you want to execute the method and don't care about it's progress: public static class ActionExtensions { public static void AsyncInvoke<T>(...

Concrete Implementation of Generic Form Not Working in Designer

I have a base class, defined as below (I'm also using DevExpress components): public abstract partial class BaseFormClass<R> : XtraForm where R : DataRow { ... } Contrary to what I've read from elsewhere, I'm still able to design this class. I didn't have to create a concrete class from it to do so. But, when I create a concrete cla...

ADO.NET Madness and Nullable Types

I've seem to have gone down a rabbit hole. I would like to convert the data from ADO .NET datasets into a Nullable type. At first I assumed that a straight cast (int?) would do it. How naive I was. Wrong, badly wrong. Now I'm trying to write a generic converter but am getting hung up on the syntax. This is so 2005 - someone must have sol...

Java Generics putting on Map<String, ? extends List<String>>

Is there a way to make the following implementation in a type safe manner? public void myMethod( Map<String, ? extends List<String>> map ) { map.put("foo", Collections.singletonList("bar"); } The above implementation doesn't work. It requires a Map<String, ? super List<String>> to compile the method map.put() correctly. But myMetho...