generics

Multiple generic types in one container

I was looking at the answer of this question regarding multiple generic types in one container and I can't really get it to work: the properties of the Metadata class are not visible, since the abstract class doesn't have them. Here is a slightly modified version of the code in the original question: public abstract class Metadata { } ...

[VB.Net] Typecasting generic parameters.

Hello world! Using the following code: Function GetSetting(Of T)(ByVal SettingName As String, ByRef DefaultVal As T) As T Return If(Configuration.ContainsKey(SettingName), CType(Configuration(SettingName), T), DefaultVal) End Function Yields the following error: Value of type 'String' cannot be converted to 'T'. Any way I coul...

Class<T> and static method Class.forName() drive me crazy.

Hi, this code doesn't compile. I'm wondering what I am doing wrong: private static Importable getRightInstance(String s) throws Exception { Class<Importable> c = Class.forName(s); Importable i = c.newInstance(); return i; } where Importable is an interface and the string s is the name of an implementing class. The compiler says: ....

Partial generic type inference possible in C#?

I am working on rewriting my fluent interface for my IoC class library, and when I refactored some code in order to share some common functionality through a base class, I hit upon a snag. Note: This is something I want to do, not something I have to do. If I have to make do with a different syntax, I will, but if anyone has an idea on ...

Is there a non-type-erased generics extension to the Java Compiler available as a 3rd party compiler extension?

I'm becoming increasingly frustrated with the limits of type-erased Java generics. I was wondering if there was a custom Java Compiler that provided a full version of generics without the quirks associated with type-erasure? Chris ...

Java multiple generic collection parameters compile error

So strange! Please have a look the code first: public class A { } public class B extends A { } public class C extends A { } public class TestMain { public <T extends A> void test(T a, T b) { } public <T extends A> void test(List<T> a, List<T> b) { } public void test1(List<? extends A> a, List<? extends A> b) { } public ...

is there some lightweight tecnique for adding type safety to identifier properties?

After using C++ I got used to the concept of Identifier which can be used with a class for the type, provides type safety and has no runtime overhead (the actual size is the size of the primitive). I want to do something like that, so I will not make mistakes like: personDao.find(book.getId());//I want compilation to fail personDao.fi...

How do I put all types implementing a certain generic interface in a dictionary?

Given a particular interface ITarget<T> and a particular type myType, here's how you would determine T if myType implements ITarget<T>. (This code snippet is taken from the answer to an earlier question.) foreach (var i in myType.GetInterfaces ()) if (i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ITarget<>)) ...

JSR 275 - Units, Percent per second

Hi all, I need to represent the unit of Percent per second using the JScience.org's JSR 275 units and measures implementation. I am trying to do to the following: Unit<Dimensionless> PERCENT_PER_SECOND = NonSI.PERCENT.divide(Si.SECOND).asType(Dimensionless.class) but I am getting a ClassCastException when I try to do that. The fo...

C# Generic Generics (A Serious Question)

In C# I am trying to write code where I would be creating a Func delegate which is in itself generic. For example the following (non-Generic) delegate is returning an arbitrary string: Func<string> getString = () => "Hello!"; I on the other hand want to create a generic which acts similarly to generic methods. For example if I want ...

Casting an object to two interfaces at the same time, to call a generic method

I want to call a generic method that constrains the input type T to implement two interfaces: interface IA { } interface IB { } void foo<T>(T t) where T : IA, IB { } How can I fix the last line of void bar(object obj) { if (obj is IA && obj is IB) { foo((IA && IB)obj); } } ? Reflection probably allows to do th...

Dealing with multiple generics in a method call

I've been dealing a lot lately with abstract classes that use generics. This is all good and fine because I get a lot of utility out of these classes but now it's making for some rather ugly code down the line. For example: abstract class ClassBase<T> { T Property { get; set; } } class MyClass : ClassBase<string> { OtherClass P...

Generic type parameter naming convention for Java (with multiple chars)?

In some interfaces i wrote I'd like to name generic type parameter with more than one character to make the code more readable. Something like.... Map<Key,Value> Instead of this... Map<K,V> But when it comes to methods, the type-parameters look like java-classes which is also confusing. public void put(Key key, Value value) Thi...

Weird use of generics

After a bit of programming one of my classes used generics in a way I never seen before. I would like some opinions of this, if it's bad coding or not. abstract class Base<T> : where T : Base<T> { // omitted methods and properties. virtual void CopyTo(T instance) { /*code*/ } } class Derived : Base<Derived> { override void ...

Performance of C# method polymorphism with generics

I noticed in C#, unlike C++, you can combine virtual and generic methods. For example: using System.Diagnostics; class Base { public virtual void Concrete() {Debug.WriteLine("base concrete");} public virtual void Generic<T>() {Debug.WriteLine("base generic");} } class Derived : Base { public override void Concrete() {Debug...

Signature of Collections.min/max method

In Java, the Collections class contains the following method: public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> c) Its signature is well-known for its advanced use of generics, so much that it is mentioned in the Java in a Nutshell book and in the official Sun Generics Tutorial. However, I could n...

Extension method question. Why do I need to use someObj = someObj.somemethod();

I have a simple extension method that I would like to use to add an item to an array of items. public static T[] addElement<T>(this T[] array, T elementToAdd) { var list = new List<T>(array) { elementToAdd }; return list.ToArray(); } this works ok, but when I use it, I am having to set the array equal to the return value. I se...

Question about using Anonymous List Types

private List<T> GetFieldList() { var Fields = new { DisplayName = "MCP", FieldName = "t.MCP", FieldType = 1 }; var FieldList = (new[] { Fields }).ToList(); return FieldList; } Should I be able to do something like this? ...

C# Constructor Problem When Using Generics

Please see an example of my code below: CODE UPDATED public class ScrollableCheckboxList { public List<ScrollableCheckboxItem> listitems; public ScrollableCheckboxList<TModel>(IEnumerable<TModel> items, string valueField, string textField, string titleField) where TModel : class { listitems = new List<ScrollableChe...

C# Using Reflection to Get a Generic Object's (and its Nested Objects) Properties

This is a scenario created to help understand what Im trying to achieve. I am trying to create a method that returns the specified property of a generic object e.g. public object getValue<TModel>(TModel item, string propertyName) where TModel : class{ PropertyInfo p = typeof(TModel).GetProperty(propertyName); return p.GetValue...