generics

User Control - Generic List Bindable Property

Hey SO, I am trying to create a User Control that accepts a generic List of CustomObject as a bindable property. I've hit a wall and can't figure it out. Any help would be greatly appreciated! I'd like it to look a little somthing like this : TabularReport.ascx [Bindable (true)] public IList<T> Source { get; set; ...

Generic overload resolution

I have the following scenario: class Foo { } class Foo<T> : Foo { } And then two methods void DoStuff(Foo foo) { DoStuffImpl(foo); } void DoStuffImpl(Foo foo) { Console.WriteLine("A"); } void DoStuffImpl<T>(Foo<T> foo) { Console.WriteLine("B"); } void Main() { DoStuff(new Foo<int>()); // prints A } ...

"Store" a type parameter for later use in C#?

I'm creating a Settings object in my app, used for storing user-defined settings and whatnot. I plan on using various datatypes for my setting fields (ints, strings, enums, anything Serializable, really). I'd also like, if possible, a type-safe way to set any of the settings. My proposed method would be doing something like this: Tuple...

cast to generic type at runtime

Ok, i don't think this is possible so I thought I would ask to make sure. I'm in the process of creating a function which reflects over the properties in a class and adds them to this data structure I have. Some of the properties are generic types. So say we have DataType(Of T) that has a .Value property of type T: Dim properties = Ge...

How to make a method take 2 different types

I know this is probably a really simple question but I'm having a brain fart at the moment. I am trying to create a method that can take one of 2 custom types. Basically the body of this method will be identical for both the types as they both have a Name property (I'm doing a comparison on the Name property to use in sorting). How shoul...

Generic return type in C#

Was practicing Generics. Consider a stack method below. What is the best way of doing error checking other than throwing exceptions in a generic method. What if I want to return some result in this method. public T pop() { if (top >= 0) return arr[top--]; return -1 or null; } ...

Constraints on Type parameters

Is there a way to constraint a generic type to only integral type? In example, if I have a method T[] sort<T>(T[] data) where : T ... {} what would I put as constraint if I wanted to ensure the parameter will be some sort of integral type? I have tryed looking at MSDN however it does not seem to mention anything about constraining a...

Subsonic Add or Update methods conversion to Generic Method

So I've got these two methods private static void AddOrUpdate(Computer input) { if (Simple.Repository.Exists<Computer>(o => o.ObjectSid == input.ObjectSid)) { Simple.Repository.Update(input); } else { Simple.Repository.Add(input); } } private static void AddOrUpdate(User input) { if (Simple.R...

Generics Warning T has same name as type from other type

Given the following public class Service<T> : IService<T> { Repository<T> _repository = new Repository<T>(); public T Get<T>(int id) { return _repository.Get<T>(id); } } public interface IService<T> { T Get<T>(int id); } I get the following warning Type parameter 'T' has the same...

Converting a generic collection of a concrete type to a collection of a base type

I've got a number of classes that implement a specific interface (ISearchable) and I'd like to return an IEnumerable of the base type (ISearchable) from a static method, but I'm not sure how to convert it without making intermediate collections. The code is pretty simple, one of the domain objects' implementations is like so: public cl...

How to get value type of a map in Java?

Possible Duplicate: Get generic type of java.util.List I have a Map and I want to get the type of T from an instance of that Map. How can I do that? e.g. I want to do something like: Map<String, Double> map = new HashMap<String, Double>(); ... String vtype = map.getValueType().getClass().getName(); //I want to get Double her...

overriding list result type in java

I would like some variant of this code to compile in java. class X { List<X> getvalue(){...}; } class Y extends X { List<Y> getvalue(){...}; } Javac (1.6) returns an error because List<Y> and List<X> are not compatible. The point is that I would like the compiler to recognize that List<Y> is a compatible return type to List<...

Get actual type of T in a generic List<T>

How do I get the actual type of T in a generic List at run time using reflection? ...

Generics: When to use new() as a constraint of Type Parameters?

The type argument must have a public parameterless constructor. When used together with other constraints, the new() constraint must be specified last. Can you guys give me a sample scenario when this constraint is needed? ...

Why calling ISet<dynamic>.Contains() compiles, but throws an exception at runtime?

Please, help me to explain the following behavior: dynamic d = 1; ISet<dynamic> s = new HashSet<dynamic>(); s.Contains(d); The code compiles with no errors/warnings, but at the last line I get the following exception: Unhandled Exception: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'System.Collections.Generic.ISet<object>'...

Java generic interfaces with typesafe implementations

I'm look for good alternatives to invoking a specific interface from a generic framework. I'll exemplify with code. Look to the question part, the example code is primarily included for thoroughness, and for putting the example into a real scenario. Example Assume we want to build a report based on a list of components. Say we have tw...

C# Generics and Inheritance Problem

Hey, I'd like to know if what I'm trying to do is even possible? Comments in code should give and idea what I'm trying to achive :) interface ITest<T> { T t { get; } bool DoTest(); } public abstract class Test<T> : ITest<T> { public Test (T nt) { this.t = nt; } public Test () { } public T t { ...

How to convert linq selector into predictor

I have a lambda selector, lets say Func<T, TResult>. Is it possible to convert it into a predictor (Func<T, bool>) using a TResult object as reference? For example, convert x => x.Name into x => x.Name == customerName ...

Extension method and type constraints

I am starting to play with extension methods and i came across with this problem: In the next scenario i get a: "extension method has a type constraint that can never be satisfied" Public Interface IKeyedObject(Of TKey As IEquatable(Of TKey)) ReadOnly Property InstanceKey() As TKey End Interface <Extension()> _ Public Function To...

Issues with method overriding and generics in Java

I've been fighting with trying to override a method in a generic abstract class. public abstract class Grandparent<T extends Grandparent> public T set(final T other) //does stuff I don't want to do public abstract class Parent<T extends Parent<T>> extends Grandparent<T> public T set(final Parent<?> other) // does stuff I wan...