generics

Generic implementation of interface

Hi everyone! Why isn't this working ? public interface IPoint { // not important } public interface IPointList { List<IPoint> Points { get; } } public abstract class Point : IPoint { // implemented IPoint } public abstract class PointList<TPointType> : IPointList where TPointType: IPoint { public abstract List<TPointType> P...

How would you refactor a "switch on Type" to be polymorphic if you don't control the types involved?

I'm writing a microformats parser in C# and am looking for some refactoring advice. This is probably the first "real" project I've attempted in C# for some time (I program almost exclusively in VB6 at my day job), so I have the feeling this question may become the first in a series ;-) Let me provide some background about what I have so...

Implementing IEnumerable

I am using LINQ and am having a hard time understanding how I can make new "domain model" classes work within LINQ while querying across tables. I am using Linq to SQL, and C# in .NET 3.5. Suppose I want an Extended Client class: public class ExtendedClient { public int ID { get; set; } public string Name { get; set; } pu...

C# Reflection & Generics

Hi, Got a complex reflection question. Given the code below how would you implement the pseudo so that given an instance of Parent it will enumerate over the types Properties find child objects with a Property of the same type as Parent and set the reference to the provided p. Hope that makes sense. Also I need this to work with Generic...

C#, Linq2SQL: Creating a predicate to find elements within a number of ranges

Lets say I have something called Stuff in my database, with a property called Id. From the user I get a sequence of selected Range objects (or rather I create them from their input) with the Ids they want. A stripped down version of that struct looks like this: public struct Range<T> : IEquatable<Range<T>>, IEqualityComparer<Range<T>> {...

How do generics (Vector) work inside the AVM?

Support for generics (currently only Vector.<*>, and called 'postfix type parameters' by Adobe) was added in Flash Player 10, but the only AVM2 documentation does not describe how these objects are accessed. Specifically, I noticed a new opcode (0x53) and a new multiname kind (0x1D) that seem relevant, but their usage is not documented....

Upcasting to a generic object from system.object

Hello! I have some simple code which demonstrates my problem: private void InitializeOther() { List<Foo> list = new List<Foo>(); Casting(list); } //in the "real" case I have no knowledge of o, other than it could be a List<> private void Casting(object o) { Type t = o.GetType(); while (t.BaseType != typeof(Object)) { if ...

C# generic list <T> how to get the type of T?

Hello, Let say I have a List< T > abc = new List< T >; inside a class public class MyClass<T>//.... Later, when I initialize the class the T because MyTypeObject1. So I have a generic list of List< MyTypeObject1 >. I would like to know, what type of object the list of my class contain. Example, the list called abc contain what type of...

Retrieving type parameters from an instance of a generic base interface

Hello, Given 2 interfaces: public interface BaseInterface<T> { } public interface ExtendedInterface<T0, T1> extends BaseInterface<T0> {} and a concrete class: public class MyClass implements ExtendedInterface<String, Object> { } How do I find out the type parameter passed to the BaseInterface interface? (I can retrieve the Extend...

What is the easiest way to get 4(or any count) random unique ids from an id collection with c#?

Assume that you have an idCollection IList<long> and you have a method to get 4 unique ids.Every time you call it, it gives you random 4 unique ids ? var idCollec = new[] {1,2,3,4,5,6,7,8,9,10,11,12}.ToList(); For example {2,6,11,12} {3,4,7,8} {5,8,10,12} ... .. What is the smartest way...

Generic Method Fail

I wrote this simple test code, by adapting a piece from a book, to help me understand the working of generic methods in Java. The line that I marked is supposed to work, why isn't it? import java.util.*; class Test { public static void main(String args[]){ List<Number> input = null; List<Number> output=null; output = Test...

In C#, are the values in a List<struct> boxed?

Suppose I declare a generic List containing values of a struct type: struct MyStruct { public MyStruct(int val1, decimal val2) : this() { Val1 = val1; Val2 = val2; } public int Val1 {get; private set;} public decimal Val2 {get; private set;} } List<MyStruct> list; Does the List<> store each individual ...

How do I instantiate an Object that uses generics with Spring framwork?

I have a class that looks like this: class Dao<T>{ ... } I want to do this new Dao<Student>(); from the Spring XML configuration. Can that be done? How? ...

Byte width of a value type

I'd like to pass a value type to a function and set it to a repeating bit pattern (FF, AA, etc.) across the entire width of the variable. Right now, I'm passing the value with void foo(T val) where T : struct so I can use any value type. The problem is, the compiler won't let me use sizeof(T), because T could be a reference type (exc...

Generic method is picking up type of base class

I have the following classes (trimmed to only show the basic structure): public abstract class BaseModel { public bool PersistChanges() { // Context is of type "ObjectContext" DatabaseHelper.Context.SafelyPersistChanges(this); } } public static class ObjectContextExtensions { public static bool SafelyPersist...

C# Generic Lists

I have a class that is maps to a field in a database. The class only cares about the name of the field and its related .NET type. The type can be string, int, datetime, etc. class Foo() { string Name { get; set; } Type FooType { get; set; } } I have another class that inherits from Foo that adds a property for a value. Righ...

Generic List to DataTable

Hi, I have few methods that returns different Generic Lists. Exists in .net any class static method or whatever to convert any list into a datatable? The only thing that i can imagine is use Reflection to do this. IF i have this: List<Whatever> whatever=new List<Whatever>(); (This next code doesnt work of course, but i would like...

C#: Alternative to GenericType == null

I need to check a generic object for null, or default(T). But I have a problem... Currently I have done it like this: if (typeof(T).IsValueType) { if(default(T).Equals(thing)) // Do something else // Do something else } else { if(thing == null) // Do something else // Do something else } But then I end up repea...

Can anything make life easier for a dotnet 3.5 coder trapped in a 1.1 world?

I have to spend a fair portion of my time developing in dotnet 1.1, and as I'm sure anyone in a similar position will appreciate, the more I get used dotnet 2.0 and above, the more annoying it is to go back to the early version. I'm getting increasingly fed up of messing about with ArrayLists and the like when I want to be working with G...

Best practice when returning an array of values (.NET)

Usually my methods are as the following: public List<int> Method1(int input) { var output = new List<int>(); //add some items to output return output; } But FxCop advises another IList implementation instead of List, but I can't remember which. The alternatives include returning it as an IList, ICollection or IEnumerable f...