generics

How to Create a Generic Method and Create Instance of The Type

Hi Guys I want to create a helper method that I can imagine has a signature similar to this: public static MyHtmlTag GenerateTag<T>(this HtmlHelper htmlHelper, object obj) { // how do I create an instance of MyAnchor? // this returns MyAnchor, which has a MyHtmlTag base } When I invoke the method, I want to specify a type of...

How to Pass Parameters to Activator.CreateInstance<T>()

Hi guys I want to create an instance of a type that I specify in a generic method that I have. This type has a number of overloaded constructors. I'd like to be able to pass arguments to the constructors, but Activator.CreateInstance<T>() doesn't see to have this as an option. Is there another way to do it? Thanks Dave ...

Generic <T> how cast ?

Hi, I have a "Product" base class, some other classes "ProductBookDetail","ProductDVDDetail" inherit from this class. I use a ProductService class to make operation on these classes. But, I have to do some check depending of the type (ISBN for Book, languages for DVD). I'd like to know the best way to cast "productDetail" value, I recei...

Weird compile-time behavior when trying to use primitive type in generics

import java.lang.reflect.Array; public class PrimitiveArrayGeneric { static <T> T[] genericArrayNewInstance(Class<T> componentType) { return (T[]) Array.newInstance(componentType, 0); } public static void main(String args[]) { int[] intArray; Integer[] integerArray; intArray = (int[]) Array....

Struts2 <s:bean/> tag, used to instanciate a Parametric Bean

Hi, After looking a while other google, and the web, I decided to post my question here. The question is quite really basic, and simple : How do I use the struts2 tag <s:bean ... /> to instanciate a Parametric Bean ? For example imagine I have : public class GenericBean<T> { ... How will I instanciate this bean with a BeanType for...

Binary search an ordered List<int>

Given a List<int> myValues which I know to be ordered, what is the quickest way to determine if X is in the given list? Do I really have to write my own binary search? ...

Is this the best approach using generics to check for nulls?

public static T IsNull<T>(object value, T defaultValue) { return ((Object.Equals(value,null)) | (Object.Equals(value,DBNull.Value)) ? defaultValue : (T)value); } public static T IsNull<T>(object value) where T :new() { T defaultvalue = new T(); return IsNull(value, defaultvalue); } Have tested, and can use against d...

Why is TreeSet<T> an internal type in .NET?

So, I was just digging around Reflector trying to find the implementation details of HashSet (out of sheer curiosity based on the answer to another question here) and noticed the following: internal class TreeSet<T> : ICollection<T>, IEnumerable<T>, ICollection, IEnumerable, ISerializable, IDeserializationCallback Without looking ...

How to create a generic type that takes either T or IEnumerable<T>?

Let's say I have a type: public class Transformer<T, TResult> where T : IMessage where TResult : IMessage { private Func<T, IEnumerable<TResult>> _transformer; public Transformer(Func<T, TResult> transformer) { _transformer = null // ? } public Transformer(Func<T, IEnumerable<TResult>> transformer) ...

generic programming in C with void pointer.

Hi everyone, even though it is possible to write generic code in C using void pointer(generic pointer), I find that it is quite difficult to debug the code since void pointer can take any pointer type without warning from compiler. (e.g function foo() take void pointer which is supposed to be pointer to struct, but compiler won't compla...

C# Get Keys from a Dictionary<string, Stream>

Suppose I have a Dictionary like so: Dictionary<string, Stream> How can I get a list (or IEnumerable or whatever) of JUST the Keys from this dictionary? Is this possible? I could enumerate the dictionary, and extract the keys one by one, but I was hoping to avoid this. In my instance, the Dictionary contains a list of filenames (fil...

.net Generic Calls <T>

I have a function that accepts a generic parameter T that is of type class like so : public Func<T, bool> MyMethod<T>(string paramName, object value) where T : class But when calling the function I do not have direct access to the class that needs to be the parameter. MyMethod<foo>("foo1", "foo2") Is there a way I can get the clas...

What's the most efficient way to combine two List(Of String)?

Let's say I've got: Dim los1 as New List(Of String) los1.Add("Some value") Dim los2 as New List(Of String) los2.Add("More values") What would be the most efficient way to combine the two into a single List(Of String)? Edit: While I love the solutions everyone has provided, I probably should have also mentioned I'm stuck using the .N...

How can I define multiple types with the same name and different type parameters using Reflection Emit?

How can I generate types like these using the System.Reflection.Emit libraries: public class Test<T> {} public class Test<T1, T2> {} When I call ModuleBuilder.DefineType(string) with the second type declaration, I get an exception because there is already another type in the module with the same name (I've already defined the type par...

Java generic Comparable where subclasses can't compare to eachother

public abstract class MyAbs implements Comparable<MyAbs> This would work but then I would be able to compare class A and B with each other if they both extend MyAbs. What I want to accomplish however is the exact opposite. So does anyone know a way to get the generic type to be the own class? Seemed like such a simple thing at first....

casting Collection<SomeClass> to Collection<SomeSuperClass>

Hi all, I'm sure this has been answered before, but I really cannot find it. I have a java class SomeClass and an abstract class SomeSuperClass. SomeClass extends SomeSuperClass. Another abstract method has a method that returns a Collection<SomeSuperClass>. In an implementation class, I have a Collection<SomeClass> myCollection I un...

What is the "Dispatcher" design pattern?

What is the "dispatcher" pattern and how would I implement it in code? I have a property bag of generic objects and would like to have the retrieval delegated to a generic method. Currently, I have properties looking for a specific key in the bag. For example: private Dictionary<String, Object> Foo { get; set; } private const String...

How can the generic method called know the type of the generic return?

I couldn't find a duplicate for this question for Java, although there are a lot of them for C#. I have this method: public <T> T getSomething() { // } According to the type of T, I will have a different return. For example: String a = getSomething(); int b = getSomething(); For a, my method will return a specific String. For b, ...

Generics: How to derive from one of two classes?

I have the following c# classes: class A : Object { foo() {} } class B : Object { foo() {} } I want to write a generic method that applies to both: void bar<T>(T t) { t.foo(); } this does not compile complaining the foo() is not a member of T. I can add a constraint for T to derive from one of the classes: void bar<T>(T t...

'Why' and 'Where' Generics is actually used?

Hi all I know that generics are used to achieve type safety and i frequently read that they are largely used in custom collections. But why actually do we need to have it generic? For example, Why cant i use string[] instead of List<string> Lets consider i declare generic class and it has a property X T x; If i provide a method fo...