generics

How to specify a generic type should be marked with an attribute?

I know I can: public class SampleClass<TSerializable> where TSerializable : ISerializable How can I write a SampleClass that accepts only classes marked with the SerializableAttribute instead? ...

Using custom TypeConverters on complex custom objects that contain generic collections

I have a class, Questionnaire, which contains a collection class that wraps a generic list, EntityCollection. I am using ViewState to persist the Questionnaire and its collection of Questions. I am using custom type converters to reduce the ViewState size. Everything works without a type converter for EntityCollection<T>. Would things...

How to configure a WCF endpoint for a generic service with a specified type?

I have the following WCF service host console application: static void Main(string[] args) { ServiceHost serviceHost = new ServiceHost(typeof(MyServiceName<int>)); serviceHost.Open(); Console.ReadLine(); } I tried to configure an endpoint for it: <services> <service name="MyNamespace.MyServiceName&lt;int&gt;"> ...

Casting a list of objects to their interface type in C#

I know I can cast an object from its own type to its interface type like so: IMyInterface myValue = (IMyInterface)MyObjectThatImplementsMyInterface; How can I cast IList<MyClassThatImplementMyInterface> to IList<IMyInterface>? ...

C# How to make a generic class?

How can I make this generic? class AtomicReference { private Object _value; public AtomicReference() { _value = new Object(); } public AtomicReference(Object value) { OptimisticSet(value); } public Object CompareAndSet(Object newValue) { return Interlocked.Exchange(ref _valu...

Why does this static factory method involving implied generic types, work?

Consider public class Tuple<T1, T2> { public Tuple(T1 v1, T2 v2) { V1 = v1; V2 = v2; } public T1 V1 { get; set; } public T2 V2 { get; set; } } public static class Tuple { // MAGIC!! public static Tuple<T1, T2> New<T1, T2>(T1 v1, T2 v2) { return new Tuple<T1, T2>(v1, v2); } } ...

Is it possible to refactor this to use generics?

Using LINQ to Entities, I have the following code: public Foo GetFooByID(int id) { return _db.Foo.First(m => m.ID == id); } public Bar GetBarByID(int id) { return _db.Bar.First(m => m.ID == id); } Is there a way to refactor this using generics? ...

Why can't I pass List<Customer> as a parameter to a method that accepts List<object>?

The following code gives me this error: Cannot convert from 'System.Collections.Generic.List' to 'System.Collections.Generic.List'. How can I indicate to the compiler that Customer indeed inherits from object? Or does it just not do inheritance with generic collection objects (sending a List<string> gets the same error). usi...

Why doesn't strings.Cast<object> cast List<string> to List<object>?

It was suggested in this question, that I could cast a generic collection upward to a collection of objects with .Cast<object>. After reading up a bit on .Cast<>, I still can't get it a generic collection to cast into another generic collection. Why doesn't the following work? using System.Collections.Generic; using System.Linq; using S...

Type inference over IEnumerable<T>

There are a few posts already on stack overflow about this sort of thing but not exactly the same - so apologies in advance if this is something that has already been answered. Why does this not work: public class MyBase { } public class MyUtils { public bool Foo<T> (T myObject) { return true; } public bool Foo (MyBase myBaseObject)...

Java: Simple issue with Interfaces and Generics

I made an interface to work with JGraphT. My intended use is like Comparable, in that implementing Comparable allows objects to be used with certain data structures. Simiarly, I have a JGraphT function that I want to work with anything that is Distanceable. public interface Distanceable<E> { /** * A representation of the dista...

Generics Question

Can anyone explain the problem with this? The 'foo' class fails to compile with an 'incompatible types' error. If you replace 'UniqueList' with 'ArrayList', it works correctly, even though UniqueList is an unmodified subclass of 'ArrayList'. 'Column' is a class defined in the app, and I don't see how its details would matter. public cla...

SSL Link Not Working In IE7

Can anyone tell why visiting the below link might lead IE7 to say they can't load the page? https://www.stlmag.com/media/St-Louis-Magazine/Store/At-Home-Subscriptions/ I thought it might be an SSL redirect issue, the link before was just http:// ... and then it automatically redirected to https:// but that didn't help. I'm thinking it...

C# - Can a List<MyClass> be seemlessly cast to a List<Interface> or similar?

I have a DataSource in my control which is always a List<T> where T has to inherit from IEntity. public class MyClass<T> where T : IEntity { public List<T> DataSource { get; set; } } Now, obviously you can't cast a List<T> to a List<IEntity> doing the following: List<IEntity> wontWork = (List<IEntity>)this...

Why does Generic class signature requires specifying new() if type T needs instantiation ?

I'm writing a Generic class as following. public class Foo<T> : where T : Bar, new() { public void MethodInFoo() { T _t = new T(); } } As you can see the object(_t) of type T is instantiated at run-time. To support instantiation of generic type T, language forces me to put new() in the class signature. I'd agr...

Generics and the question mark

Hi! I'd like to use a generic list, but the initialization method only returns a List. The following code works well: List tmpColumnList = aMethodToInitializeTheColumnList(); tmpColumnList.add("ANICELITTLECOLUMN"); Java accuses that I'm using a raw type and I should paramerize the list. So I added the question mark parameterize this ...

Less defined generics in c#?

Is there a way to use a collection of a generic class, without supplying the underlying type ? Let's explain : Here is what I'd like to have : class TimeSerie<TValue> { enter code here } List<TimeSerie<?>> blah; Here is what I have to do so far : class TimeSerie {} class TypedTimeSerie<TValue> : TimeSerie {} List<TimeSerie> blah; ...

Clarification about generic types

Given the code block below (source: http://www.asp.net/learn/mvc/tutorial-31-cs.aspx)... I receive this error: Using the generic type 'System.Collections.Generic.List' requires '1' type arguments I can pacify this by simply modifying my declaration to read as: var groups = new List<string>(); Is this just a case of invalid syntax in the...

Why cannot C# generics derive from one of the generic type parameters like they can in C++ templates?

Why cannot C# generics derive from one of the generic type parameters like they can in C++ templates? I mean I know it impossible because CLR does not support this, but why? I am aware of the profound differences between C++ templates and C# generics - the former are compile time entities and must be resolved during the compilation, whi...

Help to create a generic class to avoid code duplication

I have a simple problem try to stay DRY using Appengine. The 2 functions below are identical except for the object sent as parameter. In reality I have 15 functions like this. I am try to find a way to create a super class or a generic to achieve this. public void deleteRecord(Person s) { PersistenceManager pm = PMF.get().getPersist...