generics

how would you rewrite this line of code to be more generic

I got that line of code and it's getting duplicated a lot myString = If(myObj1.myProp Is Nothing, "", If(want = "abr", myObj1.myProp.abr, myObj1.myProp.desc)) that line x n, just change "myObj1" to "anything" and "myProp" to "anything" I tried this Public Function getDesc(Of t)(ByVal obj As t) As String Return If(obj Is Nothing,...

java list of objects that all implement an interface

Suppose I have a class that implements an interface: public class A implements IB and I have a List<A> that I would like to reference to: List<? implements IB> list. So that I can code: for (IB item : list) etc. Can it be done somehow in Java? There is no <? implements ...> possibility. What am I missing? ...

Can generic parameters be specified when using dynamic variables?

Consider the following code: class Program { void Foo<T>() { } static void Main(string[] args) { dynamic p = new Program(); p.Foo(); } } Note surprisingly, the call to p.Foo() is not valid because the dynamic binder has no way of knowing what type to use for T. The specific failure is: "The type argument...

Is there a better way to self reference a type?

I'm finding lately a lot of code smells related to referencing generic classes in C#. My gripes especially apply to those classes which inherit from DependencyObject and contain DependencyProperties. The basic problem is that when declaring a dependency property one generally references the current type which is also known as the owner...

c# Class Design with Generics

How can i make the following class as general as possible (for maximum reuse) without creating too many classes of the same type, albeit with one extra property. I want to avoid writing 3 slightly different versions of the same class 1# Class with No SubContent public class Content { public string PageName { get; set; } ...

How do I creating Generic object from dynamically loaded Type?

I'm getting a Type using Assembly class as follows: var asm = Assembly.GetAssembly(typeof(MyAssembly)); var t=asm.GetType("FULLY QUALIFIED CLASS NAME", true, true); Then I create object from this type: var obj = Activator.CreateObject(t, new []{ params }); Now I want to convert or cast this object to a Generic object (actually SubS...

Generic return value (java)

This works nicely: // Return all objects with code 'code'. static List<? extends ICode> getObjects(String code, List<? extends ICode> list) { <ICode> retValue = new ArrayList<ICode>(); for (ICode item : list) { if (item.getCode().equals(code)) { retValue.add(item); } } return retValue; } The 'singular' v...

How to I find specific generic overload using reflection?

I am attempting to create an Expression that will invoke a specific generic overloaded method (Enumerable.Average in my first test case). The specific type bindings are not known until runtime however so I need to use Reflection to find and create the correct generic method (the Expression is being created from parsed text). So if I kno...

Using Type objects as Type Parameters for Generics in C#

I cannot find a way to use a first-class Type object (System.Type instance) as a Type Parameter in a generic construction in C# 3.0 / .NET 3.5. Below is a simplified example of what I want to do: public void test() { Type someType = getSomeType(); // get some System.Type object MyGeneric<someType> obj = new MyGeneric<someType>(...

How to access Collection Generic Type class

I have a class like this : public class SubClass <T> extends SuperClass<Collection<T>>{ public Class<T> getInner() { // What goes here? } } Where SuperClass is simply a generic class. My question is how to gain access to a Class object in order to return it? I can't seem to be able to do this because of Type erasure. Any ...

Declaring the type for a templatized function in C#

In c# it's possible to create a list of functions like so: var myList = new List< Func<Foo> >(); This will allow functions (delegates) that take no arguments and return a value of type Foo to be added to the list. So something like: Foo myFunc1() { ... } would be a valid member of that list. My question is, how do I declare the typ...

Adding IEnumerable<T> to class derived from CollectionBase

Suppose I have a class TestCollection which is used to hold objects of type Test and is defined as public class TestCollection : CollectionBase This allows me to iterate through the collection either as foreach(object o in collection) ... or foreach(Test t in collection) but does not allow me to use new Linq queries. If I cha...

Generics, Inheritance, and Casting

My question has to do with casting classes inside a generic type. While I realize that casting objects like List<string> to List<object> requires support for covariance to prevent adding object objects to a list that contains strings, I am wondering why a cast like below is not accepted by the compiler and whether it is solvable employin...

Is it suggestable to use generics for large amount of data?

I'm having let's say thousands of Customer records and I have to show them on a webform. Also, I have one CustomerEntity which has 10 properties. So when I fetch data in using a DataReader and convert it into List<CustomerEntity> I am required to loop through the data two times. So is the use of generics suggestable in such a scenario? ...

Generic Methods in C#

Generic Methods in general are new to me. Need a method that returns a Collection of a generic type, but also takes a collection of the same generic type and takes Expression<Func<GenericType, DateTime?>>[] Dates parameter. T throughout the following function should be the same type, so right now I was using (simplified version): ...

How do I extend Java interface containing generic methods in Scala?

Suppose we have the following Java interface: // Java public interface Foo { <T> T bar(Class<T> c); } How should I extend it in Scala? Writing // Scala class FooString extends Foo { override def bar(c: Class[String]): String = "hello, world"; } will cause the compiler to throw "class FooString needs to be abstract, since meth...

Java Creating a new Class with Generics

I'm running into a problem with my program where given an object and an attribute name I want to return the method's return type. public static Class<?> getAttributeType(Object object, String attributeName) { try { Method method = object.getClass().getMethod( "get" + StringUtils.capitalize(attributeName)); ...

Can generics allow the Java compiler to check the type of keys and values in a map?

I am in a situation where I want to have a map where the keys are an interface class, and the corresponding value is a class which implements that interface. In other words the key and value type is related. My current implementation of the method which adds to the map, and gets an instance of the implementation class looks like: // s...

Is there an easy way to cast between two List<T>?

var candidates = (from l in db.GetLeads(lat, lon, role, radius + 10) orderby l.Distance select l); return (List<CandidateResult>)candidates; ...

DefaultModelBinder: IList vs List

I'm not sure if this is a bug, or a feature. I have an action param that takes a ListRequest object with a few string properties. .NET MVC dutifully maps the query string params of the same name to the ListRequest objects' properties. I add a ListRequest.Filters property, which is to be a list of strings taken from the querystring: ?fi...