generics

Get generic type object (e.g. MyObject<T>) from remote machine

I have two applications that are communicating through WCF. On the server the following object exists: public class MyObject<T> { ... public Entry<T> GetValue() } Where Entry<T> is another object with T Data as a public property. T could be any number of types (string, double, etc) On the client I have ClientObject<T> that ...

Can't Use Generic C# Class in Using Statement

I'm trying to use a generic class in a using statement but the compiler can't seem to treat it as implementing IDisposable. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Objects; namespace Sandbox { public sealed class UnitOfWorkScope<T> where T : ObjectContext, IDisposable,...

Java Generics Issue (w/ Spring)

I think I may be a victim of type erasure but thought I'd check with others here first. I have the requirement to do something like this: public interface FooFactory { public <T extends Bar> Foo<T> createFoo( Class<T> clazz ); } It is perfectly valid to write this code. However, I'm trying to implement this functionality using a S...

How to create a generic list in this weird case in c#

In my program, I have a class A which is extended by B, C and many more classes. I have a method GetInstance() which returns a instance of B or C (or of one of the other child), but I don't know which one, so the return type of the method is A. In the method CreateGenericList(), I have a variable v of type A, which is in fact either a B...

Code Analysis Warning CA1004 with generic method

I have the following generic method: // Load an object from the disk public static T DeserializeObject<T>(String filename) where T : class { XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); try { TextReader textReader = new StreamReader(filename); var result = (T)xmlSerializer.Deserialize(textRead...

Compilation error when using Generics C# 3.5

The following code fails to compile: class MyClass<T> : where T : MyClass <T>{} Is there any way to solve this? I have used the following workaround but I was wondering if there is a better way class MyClass <T> : IMyClass where T : IMyClass {} interface IMyClass {} ...

Generics and Derived Classes .NET 3.5

Consider the following where class "Circle" inherits from "Shape": dim objListOfCircles as new List(of Circle) DrawShapes(objListOfCirlces) Private sub DrawShapes(byref objListOfShapes as List(of Shape)) for each objShape as Shape in objListOfShapes objShape.Draw() next end sub I can't get this to work. What is the explainati...

is this possible: c# collection of Type with constrains, or collection of generic type?

I'm trying to store types in a collection, so that i can later instantiate objects of the types in the collection. But I'm not sure how to do this the best way. What i have so far: List<Type> list = new List<Type>(); list.Add(typeof(MyClass)); var obj = (MyClass)Activator.CreateInstance(list[0]); I would like to have some constrains ...

Getting type from class name with Java generics

I have following Class, I need to get type in constructor, how can I do that? public abstract class MyClass<T> { public MyClass() { // I need T type here ... } } EDIT: Here is concrete example what I want to achieve: public abstract class Dao<T> { public void save(GoogleAppEngineEntity entity) { ...

Generic multi-layer data access pattern?

I've been playing around with some new patterns for n-layer data access, and came across one that seems very flexible and easy to implement. Basically, I needed a solution that could make various data layers pluggable/swapabale on the fly - i.e., base data access from DB, distributed caching, local caching, etc. The code below is easily...

MEF Generic Class With Constructor as Type Contract

I have a generics class, that uses TBase as the type parameter. Using MEF, I wanted a list of Generic Type that it should Import. I tried to use this : 1) [ImportMany(typeof(TBase))] public List<TBase> ObjectList { get; set; } 2) Type IValueType = typeof(TBase) [ImportMany(IValueType)] public List<TBase> ObjectList{ get; set; } 3)...

Shallow copying a list with downcasting

I have the class herichary as follows CEntity---->CNode--->CElement I have a class Nodes : List<Cnode> and Class Elements : List<Element> Node class contain common item common across different project Element class has item specific to a project. I have to shallow copy the element list into the node list (basically down casti...

Generic constraints, where T : struct and where T : class

I would like to differentiate between following cases: A plain value type (e.g. int) A nullable value type (e.g. int?) A reference type (e.g. string) - optionally, I would not care if this mapped to (1) or (2) above I have come up with the following code, which works fine for cases (1) and (2): static void Foo<T>(T a) where T : stru...

c# generic method

I have a method that receives messages from a queue of msmq. I have 6 different queues in msmq and i d like a single generic method that would receive the messages. THis work, but i need to write 6 methods for each queue. I d like to make it more generic. public List<QueMessage> getMessagesFromObj1Queue() { List<QueMessage> message...

How do I get generics to work with return values that require careful casting?

I've got some data access layer code calls a stored proc and returns scalar values of various datatypes. The syntax is ExecuteDecimal, ExecuteString, etc. I want it to be Execute<string> or Execute<decimal> I try this implementation and I can't compile, unless I'm doing casting using "(T) value", if I try to check the type and call a ...

Can wpf bind to Generics Classes?

Can WPF bind to a generic class I created? It is basically Class<T> where T:MyInterface Can I Bind to Class<T> and use MyInterface to Bind It? ...

How does .Net implement generics based on C++ template knowledge?

I have the knowledge of C++ template, but don't know Java. Would someone explain to me? ...

Java generics conversion

I have build a generic datacontainer and now I want to manipulate data depending on their type. However, I get an incompatable types warning. What am I doing wrong? Type _Value; public void set(Type t) throws Exception { if (_Value instanceof Integer && t instanceof Integer) { _Value = (((Integer) t - _MinValue +...

Using generic type from other generic parameter

Hi, I have a question about .net generics. Consider the following code: public abstract class Test<TKey> { TKey Key { get; set; } } public class Wrapper<TValue, TKey> where TValue : Test<TKey> { public TValue Value { get; set; } } Now, when using this code, I could do something like this: Wrapper<Test<int>, int> wrapper...

Weird generics compile error.

I have two classes, a base class and a child class. In the base class i define a generic virtual method: protected virtual ReturnType Create<T>() where T : ReturnType {} Then in my child class i try to do this: protected override ReturnTypeChild Create<T>() // ReturnTypeChild inherits ReturnType { return base.Create<T> as ReturnTyp...