I have a set of some classes which are all capable of being constructored with an argument being an instance of a particular interface. Since they all can be constructed by this same object (and the process during which this construction happens is largely the same in all cases), I figured perhaps templating would work. Basically, I wa...
Hello everybody, it's my first question here, glad to have found this site.
My question deals with the new Generics feature in Delphi 2009. Basically I tried to write a generic wrapper class for an existing hash map implementation. The existing implementation stores (String, Pointer) pairs, so in the wrapper class I have to cast between...
Hi,
My application is processing IList's. ILists of different user defined types. I'm thinking that i can use reflection to to see what type of object the IList contains and then create a new instance of that type and subsequently add that to the IList itself?
So at any one time I might be processing
IList<Customer> l;
and I'd like ...
The code segment below prints out "The types ARE NOT the same.". Why? I am aware of the fact that using interfaceOnMyType.GetGenericTypeDefinition() will solve the problem, but why should I have to do that?
class Program
{
static void Main(string[] args)
{
var myType = typeof(Baz<>);
var interfaceOnMyType = myType.Ge...
Hello everyone,
I am wondering if the usage of a generic TList<T> where T is any interface type (except IUnknown/IInterface) might be dangerous. I am heavily using interfaces and store them in lists. Some interfaces are my own, some are provided by some COM-interfaces, so COM is involved.
I see a potential problem where checks for inst...
I have a base class that inherits List<> which has multiple classes derived from it.
I want to create a method that can add items to any class that has inherited from this base class, is this possible ?
pseudo code:
public class BaseList<T> : List<T> where T: baseItem
{
}
public BaseList<T> AddItems<T>(int someArg) where T: new(), ba...
I have class which has a method that needs to return three DataTables. I thought I could use Generics but honestly I've never used them, so I'm trying figure it out. It may not be the right thing here.
I have in my class Employee:
public List<Employee> GetEmployees()
{
//calls to other methods in my class;
//psuedocode
GetDataT...
A variable of the type Int32 won't be threated as Int32 if we cast it to "Object" before passing to the overloaded methods below:
public static void MethodName(int a)
{
Console.WriteLine("int");
}
public static void MethodName(object a)
{
Console.ReadLine();
}
To handle it as an Int32 even if it is cast to "Object" can be achieved ...
Can you explain me code below :
private static List<Post> _Posts;
public static Post GetPost(Guid id)
{
return _Posts.Find(delegate(Post p)
{
return p.Id == id;
});
}
What is the point to find an object in a generic list by that way ? He can simply iterate the list.
How this delegated method called for each element ...
I have:
class Car {..}
class Other{
List<T> GetAll(){..}
}
I want to do:
Type t = typeof(Car);
List<t> Cars = GetAll<t>();
How can I do this?
I want to return a generic collection from the database of a type that I discover at runtime using reflection.
...
I have a more complicated issue (than question 'Java map with values limited by key's type parameter' question) for mapping key and value type in a Map. Here it is:
interface AnnotatedFieldValidator<A extends Annotation> {
void validate(Field f, A annotation, Object target);
Class<A> getSupportedAnnotationClass();
}
Now, I want to...
I have a base class which is non-generic with a derived generic class. The AbstractRepository and ILoggingManager are provided by the IoC framework.
Base Class
public abstract class EventHandlerBase
: AbstractDataProvider
, IEventHandler
{
public EventHandlerBase(
AbstractRepository data,
ILoggingManager log...
Is there any (technical) reason that C# requires all generic type parameters to be declared with their enclosing class' names?
For example, I'd like to declare this:
public class FruitCollection<TFruit> : FoodCollection<TFoodGroup>
where TFruit : IFood<TFoodGroup>
where TFoodGroup : IFoodGroup { }
public class AppleCollection ...
I have a method like this:
public static <T> boolean isMemberOf(T item, T[] set)
{
for (T t : set) {
if (t.equals(item)) {
return true;
}
}
return false;
}
Now I try to call this method using a char for T:
char ch = 'a';
char[] chars = new char[] { 'a', 'b', 'c' };
boolean member = isMemberOf(ch, ...
I've occasionally heard that with generics, Java didn't get it right. (nearest reference, here)
Pardon my inexperience, but what would have made them better?
...
If I had generic class:
public class GenericTest<T> : IGenericTest {...}
and I had an instance of Type, which I got through reflection, how could I instantiate GenericType with that Type? For example:
public IGenericTest CreateGenericTestFromType(Type tClass)
{
return (IGenericTest)(new GenericTest<tClass>());
}
Of course, the ...
I have heard people state that Code Generators and T4 templates should not be used. The logic behind that is that if you are generating code with a generator then there is a better more efficient way to build the code through generics and templating.
While I slightly agree with this statement above, I have not really found effective wa...
This is a compiler error (slightly changed for readability).
This one always puzzled me. FxCop tells that this is a bad thing to return List and classes that are\derived from Collection<T> should be preferrable as return types.
Also, FxCop says that it is OK to use List<T> for internal data storage\manipulation.
Ok, I get it, but what ...
I have a generic class, but I want my type to be forced to inherit from either one or the other interface. For example:
public class MyGeneric<T> where T : IInterface1, IInterface2 {}
The above will force T to inherti from both IInterface1 and IInterface2 but can I force T to inhert from IInterface1 OR IInterface2 (or both)?
...
I'm trying to use XML documentation on the members of a class, and in a certain case I would like to reference a generic type using a tag. The problem here is that a warning is generated when multiple generic type parameters are specified. It was easy enough to find how to reference a generic type with a single parameter, as such:
<see...