Are there any clever layers out there to sit on top of the System.Web.Caching.Cache class to handle simple scenarios to fetch items using generics.
I'd like to maybe provide a delegate function to retrieve an item, and be able to fetch an item with a strongly typed GetItem and a delegate function to retrieve an item if it isnt found.
F...
The Java type system supports only invariant types. So a List<String> is not an List<Object>. A List<String> is not a List<Object> as it is not valid to insert an Integer into a List<String>. However, there are types for which such a covariant type conversion is valid.
Given the classes A, B and Producer:
class A{}
class B{}
interface ...
I have an interface as follows:
public interface ITestCase<T> where T : HtmlControl
{
IEnumerable<T> Execute(Manager manager);
}
HtmlControl is a base class from a 3rd party assembly.
I have two implementations of this interface.
One of them is returning null (specified by me), until I nail the logic required (not a difficult ta...
I am using C# and targeting the .NET Framework 3.5. I'm looking for a small, succinct and efficient piece of code to copy all of the items in a ListBox to a List<String> (Generic List).
At the moment I have something similar to the below code:
List<String> myOtherList = new List<String>();
// Populate our colCriteria ...
Hello! I'm supposed to write unit tests for a project whose code another colleague has written. He created a generic list class. Now i'm thinking about, the best way to create the unittests for the class and how to avoid much repetitive code. The List class can be instaniated with String, Integer and some other Types. How can i avoid to ...
Trying to do something like:
public interface Order {
public List<? extends OrderItem> getItems();
public void setItems(List<? extends OrderItem> items);
}
public interface OrderItem {
// stuff
}
public calss OrderItemImp implements OrderItem {
// stuff
}
public class OrderImp implements Order {
...
Before I look through my generic data structure for a value's index, I'd like to see if it is even an instance of the type this has been parametrized to. But Eclipse complains when I do this:
@Override
public int indexOf(Object arg0) {
if (!(arg0 instanceof E)) {
return -1;
}
What is the better way to do it?
This is the error mes...
var x = new { a = "foobar", b = 42 };
List<x.GetType()> y;
Is there a different way to do what I want to do here?
If there's not, I don't really see all that much point in implicit types...
...
What is the best way to retrieve the runtime value of a generic parameter for a generic class? For example:
public class MyClass<T> {
public void printT() {
// print the class of T, something like:
// System.out.println(T.class.getName());
}
}
So if I call
new MyClass<String>().printT()
it will print "String"
...
I need to know if the type of a property in a class is a generic collection (List, ObservableCollection) using the PropertyInfo class.
enter code here
foreach (PropertyInfo p in (o.GetType()).GetProperties())
{
if(p is Collection<T> ????? )
}
...
I have this function
public DataSet Fetch(string EntityName, ObjectParameter[] parameters, int pagesize, int pageindex)
{
Assembly asm = Assembly.Load("NCR.WO.PLU.ItemEDM");
Type _type = asm.GetTypes().Where(t => t.Name.Equals(EntityName)).ToList().FirstOrDefault();
object obj = Activator.CreateInstance(_typ...
Let me describe the situation, and I'm sure I'm just thinking about this problem incorrectly. I have a concrete class that will implement an interface. I want to enforce in the contract that the class must have a constructor with a specific type. So for instance:
interface MyInterface {}
public class MyClass implements MyInterface {
...
I'm new to Generics implementation and need inputs on 2 issues I face :
I have an interface ICommand defined as :
public ICommand
{
List<T> Execute<T>() where T : IValidationResult;
IDomain<T> GetDO<T>() where T : IValidationResult;
}
intentionally I have it as non-generic as I have to add a collection of different commands.
...
I have a generic class like this:
class MyGenericClass<T> { }
On the other hand I have an aggregator class that I use to obtain singleton instances of MyGenericClass and of derived classes, where each instance is identified by the concrete class type and by the parameter type. That is, MyGenericClass<int> is different from MyGenericCl...
Consider the following code:
public class SystemManager<T> where T : ISettings
{
public SystemManager()
{
T implicit1 = default(T);
T implicit2 = default(T);
if (implicit1 != implicit2)
{
// This will not compile saying the compiler cannot compare
// using '!=' two objects ...
I was doing some unit testing on code that could throw a number of exceptions depending on the inputs. So I tried something like the below code: (simplified for the example)
static void Main(string[] args)
{
RunTest<ArgumentException>();
}
static void RunTest<T>() where T : Exception, new()
{
try
...
Is there a common way to pass a single item of type T to a method which expects an IEnumerable<T> parameter? Language is C#, framework version 2.0.
Currently I am using a helper method (it's .Net 2.0, so I have a whole bunch of casting/projecting helper methods similar to LINQ), but this just seems silly:
public static class IEnumerab...
I have the following domain object:
public class DomainObject<T,TRepo>
where T : DomainObject<T>
where TRepo : IRepository<T>
{
public static TRepo Repository { get;private set; }
}
A repository interface:
public interface IRepository<T> //where T : DomainObject<T> // The catch 22
{
void Save(T domainObject);
}
An im...
For example, in Java, if I have a parameterized class, is it possible to do the following?
public class MyClass<T>{
public void aMethod {
..
T object = new T();
..
}
..
}
In Java, I think it is not possible, because the compiler doesn't know what constructor to call. But in C#? I don'...
I've read the Beautiful code with Google Collections, Guava and static imports article about Java collections, and the following snippet got my attention:
Map<String, Map<Long, List<String>>> map = Maps.newHashMap();
The thing is, I don't understand how it's possible that the newHashMap method can return a Map<String,Map<Long, List<St...