// The Structure of the Container and the items
public interface IContainer <TItem> where TItem : IItem
{
}
public class AContainer : IContainer<ItemA>
{
}
public interface IItem
{
}
public class ItemA : IItem
{
}
// Client app
[Test]
public void Test ()
{
IContainer<IItem> container = new AContainer();
}
Question : In test t...
I've got a generic range class and I'm trying to add a comparison operator so I can test whether one range is equal to another. It fails to compile and I'm not sure how to fix the issues it's complaining about. Have I missed something obvious? Here's a snippet of the code:
generic<typename T>
public ref class Range
{
protected:
T m_...
This is a simplified version of the code in question, one generic class uses another class with generic type parameters and needs to pass one of the generic types to a method with varargs parameters:
class Assembler<X, Y> {
void assemble(X container, Y... args) { ... }
}
class Component<T> {
void useAssembler(T something) {
...
Hello,
I have following code
public interface IEntity
{
int Id { get; set; }
}
public interface ICriteria<T> where T : class,IEntity
{
T GetResult(int id);
}
public class DummEntity : IEntity
{
public int Id { get; set; }
}
public class SimpleCriteria<T>:ICriteria<T> where T:class,IEntity
{
public T GetResult(int i...
Is it possible to write a method that could create instances of any specified type?
I think java generics should help, so it might be something like this:
public <U> U getObject(Class klass){
//...
}
Could anyone help me?
...
I've a base type from which 3 different objects are inherited. Lets call the base object B, and inherited ones X, Y, and Z. Now I've dictionaries with an int as key and respectively X, Y, Z as value.
I've a control that will need to do lookups in one and only one of the specific dictionaries to draw itself but to be able to work with al...
The method is this one, EntityBase.Get:
public class EntityBase
{
public int Id;
public static T Get<T>(int id) where T : EntityBase
{
DataContextExtender extender = new DataContextExtender();
return extender.DataContext.GetTable<T>().Where(t => t.Id == id).FirstOrDefault();
}
}
How I want to use it:
...
Currently, generics in C# do not allow any sane way to perform arithmetic. There are awkward workarounds available, but none of them are very neat and all of them reduce performance.
According to this interview, an interface with arithmetic types is not possible to implement, and so one such workaround is suggested.
But what you coul...
Is it possible to create a list of anonymous delegates in C#? Here is code I would love to write but it doesn't compile:
Action<int> method;
List<method> operations = new List<method>();
...
I am using a hierarchy of generic collection classes that derive from an abstract base class to store entity items that also derive from an abstract base class:
abstract class ItemBase { }
class MyItem : ItemBase
{
public MyItem()
{
}
}
abstract class CollectionBase<T> : Collection<T> where T : ItemBase, new() { }
class ...
Is this possible? When i compile i get an error saying cannot convert Component to TComponent even with the constraint
public interface IComponent<TKey, TComponent> where TComponent : IComponent<TKey, TComponent>
{
TComponent Parent { get; }
void Register(TKey key, TComponent component);
void RegsiterWith(TKey key, TCompone...
Not sure why I'm getting this error. Project is converted from VS 2005, to VS 2008, but remains with 2.0 framework..
image of the error message
Here is the interface with my generic type and constraint....
public interface ITableAdapter<DT> where DT:System.Data.DataTable
{
/// <summary>
/// Must be called immediately after...
Hi,
I feel like I'm so close to working this out and have read dozens of articles and existing questions.
I have a method like this:
public T DoStuff(object arg1, object arg2)
{
/* Do stuff and return a T */
}
And I need to be able to pass this method to another class for callback purposes.
However, when this other class calls bac...
In Java, the Map interface is defined as,
public interface Map<K,V> {
...
V get(Object key);
...
}
Why not?
V get(K key);
I just bumped into a nasty bug because wrong type key is used. I thought the purpose of the generics is to catch type error early during compiling. Does this defeat that purpose?
...
I am having a problem with the return type of a method.
The method returns a linq object which at present returns type tblAppointment. This method is shown below:
public tblAppointment GetAppointment(int id)
{
var singleAppointment = (from a in dc.tblAppointments
where a.appID == ...
I have an extension method that I would like to overload so it can handle both reference types and nullable value types. When I try to do this, however, I get "Member with the same signature is already declared." Can C# not use the where qualifier on my generic methods to distinguish them from each other? The obvious way to make this w...
Hello, I have a question involving calling a class's generic method with a type parameter that is known at runtime.
In specific, the code looks like so:
FieldInfo[] dataFields = this.GetType().GetFields( BindingFlags.Public | BindingFlags.Instance );
// data is just a byte array used internally in DataStream
DataStream ds = new DataS...
I have a class with Maps from Ks to Set<V>s, for several different Vs. I'd like to use a generic factory ala:
protected static <T> Set<T> SetFactory(int size) {
return new HashSet<T>(size);
}
to do something like
for (K key : keySet) map.put(key, SetFactory());
and have the generic part work (I get a compile error, type misma...
Hi all,
I have a pretty simple case where I do some basic generic assignment:
final Detail detail = field.getAnnotation(Detail.class);
final String example = detail.example();
final Class<?> type = field.getType();
if (List.class.isAssignableFrom(type))
...
else if (Enum.class.isAssignableFrom(type))
setValue(c...
Public Class c1(Of T)
End Class
Public Class c2
Inherits c1(Of Integer)
End Class
For the following peice of code, if we try to get the CodeClass.Base.Fullname, it gives the fullname as "c1(Of T)". Ideally it should give the fullname of the instance i.e. "c1(Of Integer)". The CodeModel API behaves differently for VB and C# codes. ...