generics

How to define generic super type for static factory method?

If this has already been asked, please link and close this one. I'm currently prototyping a design for a simplified API of a certain another API that's a lot more complex (and potentially dangerous) to use. Considering the related somewhat complex object creation I decided to use static factory methods to simplify the API and I curre...

c# Generics problem

Can anyone tell me why this does not work? I would have thought the constraint would make it valid. public class ClassA<T> where T : ICommon { public ClassA() { ClassB b = new ClassB(); IEnumerable<T> alist = new List<T>; b.Items = alist; //Error: cannot convert from IEnumerable<T> to IEnumerabl...

Are static members of a generic class tied to the specific instance?

This is more of a documentation than a real question. I noticed the principle it is not described on SO yet (did I miss it?), so here goes: Imagine a generic class that contains a static member: class Foo<T> { public static int member; } Is there a new instance of the member for each specific class, or is there only a single inst...

Access to an inner type

A colleague of mine posted a question on an internal forum which got me thinking about whether this was possible through C#. Basically, he's got an interface as follows: public interface IProvider<T> { T GetT(); } Is it possible to use something that implements that interface as a type parameter to another generic class and have a...

Castle Windsor - Resolving a generic implementation to a base type

I'm trying to use Windsor as a factory to provide specification implementations based on subtypes of XAbstractBase (an abstract message base class in my case). I have code like the following: public abstract class XAbstractBase { } public class YImplementation : XAbstractBase { } public class ZImplementation : XAbstractBase { } public...

Type-safe generic data structures in plain-old C?

I have done far more C++ programming than "plain old C" programming. One thing I sorely miss when programming in plain C is type-safe generic data structures, which are provided in C++ via templates. For sake of concreteness, consider a generic singly linked list. In C++, it is a simple matter to define your own template class, and th...

.NET 2.0: Invoking Methods Using Reflection And Generics Causes Exception

Hi all, I'm new to Stack Overflow, so forgive me. I've just started transititoning over to C# and I am stuck on a problem. I am wanting to pass a generic class in and call a method from that class. So, my code looks as such: public void UpdateRecords<T>(T sender) where T : new() //where T: new() came from Resharper { Type foo = Ty...

Cannot inherit from generic base class and specific interface using same type with generic constraint

Sorry about the strange title. I really have no idea how to express it any better... I get an error on the following snippet. I use the class Dummy everywhere. Doesn't the compiler understand the constraint I've added on DummyImplBase? Is this a compiler bug as it works if I use Dummy directly instead of setting it as a constraint? Err...

Java enums in generic type

Hi, I'd like to create a generic enum-based mapper for IBatis. I'm doing this with the below code. This does have compile time errors, which I don't know how to fix. Maybe my solution is just plain wrong (keep in mind the use of IBatis), in such case please suggest something better. Any help appreciated. What I want to achieve is to de...

How to determine if a List is sorted in Java?

I would like a method that takes a List<T> where T implements Comparable and returns true or false depending on whether the list is sorted or not. What is the best way to implement this in Java? It's obvious that generics and wildcards are meant to be able to handle such things easily, but I'm getting all tangled up. It would also be ...

Converting Generic Type into reference type after checking its type using GetType(). How ?

i am trying to call a function that is defined in a class RFIDeas_Wrapper(dll being used). But when i checked for type of reader and after that i used it to call function it shows me error Cannot convert type T to RFIDeas_Wrapper. EDIT private List<string> GetTagCollection<T>(T Reader) { TagCollection = new ...

Invoking static methods containing Generic Parameters using Reflection.

While executing the following code i gets this error "Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true." class Program { static void Main(string[] args) { MethodInfo MI = typeof(MyClass).GetMethod("TestProc"); MI.MakeGenericMethod(new [] {typeof(string)});...

Multi-tier applications using L2S, WCF and Base Class

Hi all. One day I decided to build this nice multi-tier application using L2S and WCF. The simplified model is : DataBase->L2S->Wrapper(DTO)->Client Application. The communication between Client and Database is achieved by using Data Transfer Objects which contain entity objects as their properties. abstract public class BaseObject ...

How to return a value based on the type of the generic T

I have a method like: public T Get<T>(string key) { } Now say I want to return "hello" if the type is a string, and 110011 if it is type int. how can I do that? typeof(T) doesn't seem to work. I ideally want to do a switch statement, and return something based on the Type of the generic (string/int/long/etc). Is this possible? ...

Generic Func<> as parameter to base method

I might be losing the plot, but I hope someone can point me in the right direction. What am I trying to do? I'm trying to write some base methods which take Func<> and Action so that these methods handle all of the exception handling etc. so its not repeated all over the place but allow the derived classes to specify what actions it wa...

Java Generics Class Parameter Type Inference

Given the interface: public interface BasedOnOther<T, U extends BasedList<T>> { public T getOther(); public void staticStatisfied(final U list); } The BasedOnOther<T, U extends BasedList<T>> looks very ugly in my use-cases. It is because the T type parameter is already defined in the BasedList<T> part, so the "uglyness" com...

IBM RAD With Java 1.5 wont compile code with generics

Hello I have some code that has generic references in it and my IBM RAD IDE will not compile the code, instead treating it as an error. I have checked the version of the JRE its pointing to across all the Enterprise Project's and it is 1.5 which I am told does support generics. Also I checked that all the libraries for WAS were pointi...

Can you cast an object to a long in c#?

I am pulling a 'long' value from Session, using a generic property, and it is crashing. so I have: public static T Get<T>(string key) { if(...) return (T)System.Web.HttpContext.Current.Session[key]; ... } When debugging, the value is 4, and it crashes. ...

Accessing properties through Generic type parameter

I'm trying to create a generic repository for my models. Currently i've 3 different models which have no relationship between them. (Contacts, Notes, Reminders). class Repository<T> where T:class { public IQueryable<T> SearchExact(string keyword) { //Is there a way i can make the below line generic //return db.Co...

Dynamically find the parameter to be passed as <T> to a generic method

A generic method is defined as follows: private static T GetComparisonObject<T>(ComparisonAttribute attribute, object objectToParse) { // Perform a some action return (T)resultObject; } The method is invoked as follows: var srcObjectToCompare = GetComparisonObject<DynamicType>(attributeToCompare, srcObjec...