generics

Generics and Casting

Why does the following compile? public IList<T> Deserialize<T>(string xml) { if (typeof(T) == typeof(bool)) return (IList<T>)DeserializeBools(xml); return null; } private static IList<bool> DeserializeBool(string xml) { ... do stuff ... } But this doesn't public MyClass<T> GetFromDb<T>(string id) { ...

Comparator for Generic Interface Without Warnings

Given: public interface PrimaryKey<Key extends Comparable> { Key getKey(); } and public class PrimaryKeyComparator implements Comparator<PrimaryKey> { public int compare(PrimaryKey first, PrimaryKey second) { return first.getKey().compareTo(second.getKey()); } } This combination works, but gives warnings about r...

About serilalize the object having generic type in WCF

I implemented a generic way to get the object by id from Entities defined in Entity Framework. But the problem is the object I got has a very weird type like this {System.Data.Entity.DynamicProxies.MyEntity_C71732021C3A9D6A58BDB6087D29E98CFDE09DA9D53AF0892AFB7918AEF7E61F} And WCF will fail when serialize this object as the type of MyE...

Why use generic and nongeneric function variants?

I've been studying code of some different libraries, and notice that some will provide equivalent generic and non-generic functions in the same class. One example is the IServiceLocator interface of the Common Service Locator project: public interface IServiceLocator { object GetInstance(Type serviceType); object GetInstance(Ty...

Why can't a list of Interfaces use an implementing type?

There must be something fundamental about interfaces/generics I have not yet learned. I hope to learn it now. Here is the scenario: I have this interface and class: public interface IInterface { string TestValue { get; set; } } public class RealValue: IInterface { public string TestValue { get; set; } } If I create a met...

Generics: How to check the exact type of T, without object for T.

Hi guys, How can i check/evaluate the exact type of T without an object for T. I know my question maybe confusing but consider this... public abstract class Business { public abstract string GetBusinessName(); } public class Casino : Business { public override string GetBusinessName() { ...