generics

Java: Generic JAXB Serialization

I'm looking for a generic way to serialize objects in Java using JAXB XML Serialization. I'd like to have something like this: public static <T> String serializeUsingJAXB(T entity) { JAXBContext context = JAXBContext.newInstance(T.class); // ... } However, it looks as though due to type erasure, T.class doesn't work. What wi...

Registering generic types in Windsor container

I need to register open generic of a type in my Windsor Container. How does this thing works? container.Register(Component.For<IRepository<??>>().UsingFactoryMethod(x => x.Resolve<IDataContext>().GetRepository<??>())); Basically, I want to register IRepository as open generic and then create the repository from the IDataContext based...

Generic Collections - Ensuring Collections Contain Only One Type of Object?

Hey all, Let me start with the definitions of the objects with which I am working and then I'll do my best to explain what I'm after: class CacheItem<T> where T : EntityBase class CacheCollection : List<CacheItem<EntityBase>> OK, so my generic collection, CacheCollection should be a list of CacheItems which I can manipulate accordin...

Asp.net MVC 2 Entity Framework Generic Repository Method. how to Update a specific Collumn

I have 10 tables with the same design. Each of them has an IsActive Collumn. EX: Category CatID CatName IsActive Product PrdID PrdName IsActive Is there a way to create a generic method to update the IsActive column. public void Deactivate<T>(T TEntity) { //Put the code to update //IsActive } I Read ...

Instantiating a generic type?

I'm currently working on a generic collection class and I'd like to create a method which returns an object from the collection. If the specific object does not exist in the collection then the object should be created, added to the collection, and then returned. I'm accounting a few problems though. The generic collection if of a typ...

Combining generic MVP pattern with abstract factory pattern

Is there some way to solve my code below? I'm kinda stuck. How can I use a factory to create generic presenters, is it even possible without a non generic base class? public abstract class Presenter<T> {} public SomePresenter : Presenter<ISomeVew> {} public SomeOtherPresenter : Presenter<ISomeOtherView> {} public class Factory() { ...

c# reflection on generic template class

Hi guys, Following a sample of my code: public abstract class<T> { public List<T> GetSomething(string q) { **List<T> list = new List<T>();** Type type = typeof(T); PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance); foreach (PropertyInfo info ...

Java: One function with many return types... Is it possible with generics?

I have a few procedures that, for simplicity's sake, look like the following: public String fetchValueAsString(String key); public DateTime fetchValueAsDateTime(String key); I want something like public <X is a String or a DateTime> X fetchValue(String key); // pseudo-code that I could call like this (without casting; the type is ...

How should I call the generic function without knowing the type at compile time?

Lets say, If I have a situation like the following. Type somethingType = b.GetType(); // b is an instance of Bar(); Foo<somethingType>(); //Compilation error!! //I don't know what is the Type of "something" at compile time to call //like Foo<Bar>(); //Where: public void Foo<T>() { //impl } How should I call the gen...

Is is possible to implement setters on properties of anonymous types

Consider the following: var o = new { Foo = "foo", Bar = "bar" }; This instance is read-only, since the anonymous type doesn't implement setters like a class does: public class O { public String Foo { get; set; } public String Bar { get; set; } } Is it possible to "open up" the anonymous instance and allow it's properties t...

Using a default class literal value on an annotation.

I want to annotate some of the fields of a given bean class with the following annotation: @Target({FIELD}) @Retention(RUNTIME) public @interface Process { Class<? extends ProcessingStrategy> using() default DefaultImplStrategy.class; } Without going into the domain too much, each annotated property needs to have a ProcessingStr...

C# Generic object instantiation with generic type

I'm working on a generic delegate function and declaring a return type of type List. public static List<T> PerformOperationOnGenericArray<T>(IList<T> myList, FunctionForGenericArray<T> operation) Am I able to use a generic return type instead of List that also specifies a generic type, i.e. S public static S<T> PerformOperationOn...

What is mean by Collection<? extends EmpApp>?

In my code i am using two different class objects empobj & employee. Eclipse asks to change the code empobj.addAll(employee); to empobj.addAll((Collection<? extends EmpApp>) employee); What does it mean? I cannot understand the concept here. Can I get any clarifications about this? ...

Cast T parameter in generic method to DateTime

Hello, I have the following (simplified) method: private static string GetStringFromValue<T>(T val) { if (typeof(T) == typeof(DateTime)) { return string.Format("{0}", ((DateTime)val).Year.ToString("0000")); } return string.Empty; } At the cast "(DateTime)val" I get the following error: Cannot cast expressi...

Select a model property using a lambda and not a string property name.

I'm building a list of properties of a type to include in an export of a collection of that type. I'd like to do this without using strings for property names. Only certain properties of the type are to be included in the list. I'd like to do something like: exportPropertyList<JobCard>.Add(jc => jc.CompletionDate, "Date of Completion...

Project generic type into a KeyValuePair

I'm sure what I'm trying to do is fairly simple but I'm struggling. I simply want to project an IQueryable into a key value pair. This is part of my class: public DropDownListActionResult(IQueryable<T> dataItems, Func<T, int> keySelector, Func<T, string> valueSelector, int? selectedID) { _dataItems = dataItems; _keySelector = ...

C# Type Conversions for Generics Parameter

This is a rather elementary C# question; my apologies, but I haven't been able to find this elsewhere. What is the best way to convert from Object<class> to Object<interface>? I.e. //fake interface interface ignu {} //fake class which implements fake interface class bee : ignu {} //function which accepts a generic with the interface...

Adding EventHandler according to a Type

I have list of objects that contain information about which classes static EventHandler should be listen to whitch folder. I know this doesn't work but you'll get the idea.. (eventhandler doesn't have to be static, class can also be a singleton, but somehow I have to add an EventHandler based on the type specified) foreach (Service s i...

Please help me understand polymorphism when using generics in c#.

I am having a problem understanding how polymorphism works when using generics. As an example, I have defined the following program: public interface IMyInterface { void MyMethod(); } public class MyClass : IMyInterface { public void MyMethod() { } } public class MyContainer<T> where T : IMyInterface { public IList...

Determining the type of List entries

I have a List<?> listin Java. Is there a way to determine the type of the contents of that list at runtime when the list is empty? ...