.net-generics

Serializing Generic Classes

I have read around that serializing generic classes is not supported out of the box with XamlWriter. First I would like to know why? What is harder about generic classes that makes them non-plug-and-play like all the other classes are. Second, is there a framework that will allow me to serialize my generic class without much work. (M...

Open generic interface types of open implementation don't equal interface type?

Here's a test that should, in my opinion be passing but is not. [TestMethod] public void can_get_open_generic_interface_off_of_implementor() { typeof(OpenGenericWithOpenService<>).GetInterfaces().First() .ShouldEqual(typeof(IGenericService<>)); } public interface IGenericService<T> { } public class OpenGenericWithOpenService...

F# Class with Generics : 'constructor deprecated' error

I am trying to create a a class that will store a time series of data - organized by groups, but I had some compile errors so I stripped down to the basics (just a simple instantiation) and still can't overcome the compile error. I was hoping some one may have seen this issue before. Clas is defined as: type TimeSeriesQueue<'V, 'K whe...

how do I best create a set of list classes to match my business objects

I'm a bit fuzzy on the best way to solve the problem of needing a list for each of my business objects that implements some overridden functions. Here's the setup: I have a baseObject that sets up database, and has its proper Dispose() method All my other business objects inherit from it, and if necessary, override Dispose() Some of th...

In VB.NET how do you specify Inherits/implements on a generic class with multi-constraints

When I write the following statement in VB.Net (C# is my normal language), I get an "end of statement expected" referring to the "Implements" statement. <Serializable()> _ <XmlSchemaProvider("EtgSchema")> _ Public Class SerializeableEntity(Of T As {Class, ISerializable, New}) _ Implements IXmlSerializable, ISerializable ... End Class ...

C# generics - Can I make T be from one of two choices?

Suppose I have the following class hierarchy: Class A {...} Class B : A {...} Class C : A {...} What I currently have is Class D<T> where T : A {...} but I'd like something of the form Class D<T> where T in {B,C} This is due to some odd behavior I'm not responsible for where B and C have common methods which aren't in A, but ...

Remove objects of some kind of type from a List<T> in C# using extension methods?

I wonder if its possible to remove all the objects from the same kind from a generic List using extension methods. something like this code: public static Remove<T>(this List<[any type]> list) { // some code to remove the objects of type T from the list } I can do this by using the following code: public static Remove<T, K>(this ...

C# Extension Method on Type With Generic Type Argument

I’m looking at ways to improve the consistency, brevity, and readability of some code in the application I’m working on. The starting code looked something like this: context.GetGraphType<Bar>().Subscribe<Fizz>( (instance, evt) => e.Execute((Bar)instance.Instance) ); There are a number of nearly identical lines of code like the ...

Help Required to Convert a Method to a Generic Method

Hi, I have a method that is structured like this: public object Get(int intId, int intWeekNumber) { try { if(intWeekNumber > -1) { switch(intId) { case 1: return ReportDB.GetReport1(intWeekNumber);//return object ...

In c#, is there way to write a generic such that Object<Child> : Object<Parent> ?

Is there a way to do something like this in c#? Consider the following example and assume that Child1, Child2, Child3 are all children of Parent - class Class1 { SomeObject< Parent > mSomeObject; Class1() { if (condition1) mSomeObject = new SomeObject<Child1>(); else if (condition2) mSomeObje...

C# generic How to define that T is Base<Tp> : where Tp : Base<Tp> and Call Base<Tp> method

Hi! I have confusing situation. Base Generic Type and successor public abstract class BaseType<TEntity> : where TEntity : BaseType<TEntity> public class AnyType : BaseType<AnyType> It looks like a generic loop))) I need Method like public void Method<T>(T data) { if(typeof(T).IsSubclassOf(BaseType<????>)) convert data to BaseType<...

In a generic list, is there a way to copy one property to another in a declarative /LINQ manner?

I have a class with two properties, say public class Book { public string TitleSource { get; set; } public string TitleTarget { get; set; } } I have an IList<Book> where the TitleTarget is null and for each item in the list, I need to copy the TitleSource property to the TitleTarget property. I could do this through a loop, sure, but ...

Generic Methods pass in a delegate to the method?

Is it possible to have a generic method that requires a delegate(?) for a method or just a code block to be passed in as a parameter? Say I have AddJob() and AddJob2(). I want these passed into a Generic method that runs some skeleton code but then executes the AddJob or AddJob2. Thanks! Edit: I'm on .net 2.0. ...

Convert nested type in C# using delegates / LINQ

How can I convert a List<List<string>> to a List<string[]> in C# in a concise way using delegates/LINQ? ...

Cast an Object to a generic class when the generic type isn't known

In a Silverlight project, I've got the following class defined. public class ThemeResource<T> { public string Name { get; set; } public string Description { get; set; } public Type Type { get { return typeof(T); } } public string TypeName { get { return Type.FullName; } } public T Resource { get { return (T)Applicati...

Creating a variable that can store different instances of generic types and calling a given method on the variable, regardless of the type

I am trying to create a generic simulation runner. Each simulation implements a variety of interfaces. Eventually, it will pick up simulation types via DLLs at run time, so I will have no way of knowing the types beforehand. My Currrent Code: public class SimulationRunner<TSpace, TCell> where TSpace : I2DState<TCell> where TCe...

Silverlight : Create Generic Control from backgroundWorkder Thread

Hi, I'm in trouble when using the background worker to create my object model. As I understand why, I'm unable to find a workaround. Here is the pseudo logic : Call Webservice async When received, open a background worker, and load data into controls in the background in the Load method, search for an existing object and if not foun...

Looking for generic way to implement function in base class

I'm trying to do a simple implementation of the Specification pattern in my domain layer. If I have a static class full of specifications like this: public static class FooSpecifications { public static Func<Foo, bool> IsSuperhuman { get { return foo => foo.CanShootLasersOutOfItsEyes && foo.CanFly; } } } Then ...

If I create a new List<T> from an existing list, are the members equal?

Assuming I have a List<Stuff> listA that has some items in it. I create a second list as follows: List<Stuff> listB = new List<Stuff>(listA); Let's say I have an item from listA, and I try to remove it from listB: Stuff itemFromA = listA[0]; listB.Remove(itemFromA); Assuming Stuff is a Class, should the item be successfully removed...

Create generic class with internal constructor

Is it possible to construct an object with its internal constructor within a generic method? public abstract class FooBase { } public class Foo : FooBase { internal Foo() { } } public static class FooFactory { public static TFooResult CreateFoo<TFooResult>() where TFooResult : FooBase, new() { return new TFooResult(...