generics

CSharp Parameterized Property class implemented via Generics?

I would like to build a generic class that I can use to implement Parameterized Properties in C#. The basic code I want is this: public class ParameterizedProperty<typeReturn, typeIndexer> { private typeReturn oReturn = default(typeReturn); public typeReturn this[typeIndexer oIndexer] { get { return oReturn[oIndexer]; }...

How to implement an IN clause in LinQ

Hi there, I have two ILIst of these objects: class ProductionMachineType { string code { get; set; } IEnumerable<string> ProductionToolsLink { get; set; } } class ProductionTools { string code { get; set; } } I am looking for a fast Linq method that make me able to query the IList<Produ...

how to pass a converter (constraints not applicable for generic arguments)

I want to pass a converter to a method, and the constraint must be that the converter always gets a string as argument. I tried the following, but it won't compile: class Test { public void Foo(string val, Converter<Tin,Tout> conv) where Tin:string { myObj = conv(val); } } ...

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 ...

subList for arrays - ClassCastException when casting to Generic Type

I've written a small utility method but it always produces a ClassCastException, any ideas why? and how to fix it? <T> T[] subArray(int begin, int end, T[] array) { int size = end - begin; Object[] newArray = new Object[size]; for (int i = 0; i < size; i++) { newArray[i] = array[begin + i]; } return (T[]) new...

C# Generic Class Question

I am working on a class library and am having some trouble with generics. I have a ITransaction interface which has a collection of ITransactionItem. Each ITranscation can be either a CapitalCall or Distribution. A CapitalCall is a ITransaction but has a few additional properties. A CapitalCallItem is a ITransactionItem with a few additi...

How do you extract derived types from a generic list?

I have a class with these properties: public List<CommitmentItem<ITransaction, ITransactionItem>> CommitmentItems; public List<CapitalCallCommitmentItem> CapitalCallCommitmentItems; CapitalCallCommitmentItem inherits CommitmentItem. I want the CapitalCallCommitmentItems property to return all CommitmentItems where the type is of Capit...

Check if object inherits from generic class

I have a generic list class: TMyObjectlist<T: TMyObject> = class(TObjectList<T>); and a derived list class: TMyDerivedObjectList = class(TMyObjectList<TMyDerivedObject>); I want to check if an instance MyList of TMyDerivedObjectList inherits from TMyObjectList, however: MyList.InheritsFrom(TMyObjectlist<TMyObject>) returns False...

List of Generic Lists

I would like to create a list of objects in which there is a generic list. So what I have is this object: public class agieDBColumn where dataTp:IComparable{ private string _header; private string _longHeaderName; private List _data; public string header { get { return _header; } set { _header = value; } } public string longH...

Java generics and inheritance

I have the following abstract classes: public abstract class AbSuperClass1<K,S> { //class definition } and: public abstract class AbSuperClass2<K,S> { public abstract <Q extends AbSuperClass1<K,S>> void method(Q arg); ... } I then have two concrete implementations public class Concrete1 extends AbSuperClass<String, Str...

Java generics and varargs

I'd like to implement a function with both generics and varargs. public class Question { public static <A> void doNastyThingsToClasses(Class<A> parent, Class<? extends A>... classes) { /*** something here ***/ } public static class NotQuestion { } public static class SomeQuestion extends Question { } ...

How can I refactor our the type parameter from this code?

I want to write an extension method that tests if an attribute is applied on a method call, and I'd like to specify the method as a lambda expression. Currently, I have the following (working) approach, but I really don't like the way this code looks: // Signature of my extension method: public static bool HasAttribute<TAttribute, TDele...

Force Loading of all Related Entities within Entity without knowing Entity Class Types

I'm trying to serialize an entity and all its related entities for storing as xml before physically deleting the entity (leaving an audit trail). I'm using a DataContractSerializer which seems to be getting around the shallow serialization performed when using an XmlSerializer. The only trouble is that only related entities that have b...

MEF export of Repository<T> : IRepository<T>

I am trying to use MEF to Export the following: [Export(typeof(IRepository<>))] public class Repository<T> : IRepository<T> where T : class { With an import of [Import(typeof(IRepository<>))] private IRepository<Contact> repository; But I keep getting an error message when composing MEF of: ====================================...

How Jersey extracts generic type from Collections to invoke javax.ws.rs.ext.MessageBodyWriter#writeTo()?

In a Rest Service using the JAX-RS specification, I can define a generic service like @GET @Path("something") @Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) public List<MyPojo> getMyPojoList() { ... } Something magic happens in Jersey because when invoking javax.ws.rs.ext.MessageBodyWriter#writeTo(T t, ...

How to pass a delegate to create an expression tree that is a MethodCallExpression

I'm looking to 'generalise' some code in a .NET 3.5 MVC application and have stumbled into a problem. Background I have a SomeController class with some actions: public ActionResult Renew(string qualification, int tierId) { ... } public ActionResult Reinstate(string qualification, int tierId) { ... } public ActionResult Withdraw(strin...

C# generics and collection

Hi I have two objects MetaItems and Items. MetaItem is template for objects and Items contains actual values. For example "Department" is treated as meta-item and "Sales", "UK Region", "Asia Region" are treated as items. Additionally I want to maintain parent-child relation on these meta-items and items. I have following code for s...

Generic with Linq DataContext

Assume I have a table called User. Using LINQ desinger, I will end up with the following: A file called User.dbml A data context class called UserDataContext which subclasses from System.Data.Linq.DataContext A class called User which is mapped from the User table. A UserDataContext object will have a property called Users which is...

Why is one class valid while the other is not?

As you can see, having a non void return type is important. class TestValid { public String f(List<String> list) { return null; } public Integer f(List<Integer> list) { return null; } public void test() { f(Arrays.asList("asdf")); f(Arrays.asList(123)); } } class TestInvalid { public void f(List<String> list) { ...

Why won't this cast work?

I have the following code: var commitmentItems = new List<CommitmentItem<ITransaction>>(); commitmentItems.Add(new CapitalCallCommitmentItem()); And I get the following error: Argument '1': cannot convert from 'Models.CapitalCallCommitmentItem' to 'Models.CommitmentItem<Models.ITransaction>' However, CapitalCallCommitmentItem ...