Given a generic array T[], where T extends java.lang.Number, I would like to write the array to a byte[], using ByteArrayOutputStream. java.io.DataOutput (and an implementation such as java.io.DataOutputStream appears close to what I need, but there is no generic way to write the elements of the T[] array. I want to do something like
By...
Lets say we have a program which contains such classes:
public interface AbstractItem {
}
public SharpItem implements AbstractItem {
}
public BluntItem implements AbstractItem {
}
public interface AbstractToolbox {
//well the problem starts here...
public List<AbstractItem> getItems();
}
public ExpensiveToolbox implements Abstr...
I'm reading the msdn library topic about genrics . There is an example of declaring event wiht generic delegates, but is it correct?
// Code block 8. Generic event handling
public delegate void GenericEventHandler<S,A>(S sender,A args);
public class MyPublisher
{
public event GenericEventHandler<MyPublisher,EventArgs> MyEvent;
pu...
I've got a generic method:
Func<IEnumerable<T>, bool> CreateFunction<T>()
where T can be any number of different types. This method does a bunch of stuff using reflection and if T is an IDictionary, regardless of the the dictionary's TKey and TValue I need to execute dictionary specific code.
So the method could be called:
var f = C...
I've got a generic type:
class DictionaryComparer<TKey, TValue> : IEqualityComparer<IDictionary<TKey, TValue>>
And a factory method that will (should) create an instance of this class for a given dictionary type.
private static IEqualityComparer<T> CreateDictionaryComparer<T>()
{
Type def = typeof(DictionaryComparer<,...
Can anyone help me in using Where for generic types?
I was trying to create a function which does ST with a number of type double or int, so I said it should be generic function. But when I try to assign a value to variables of that generic type, I can't because it's not a numerical type. Also, I can't use Where to inherit generic type f...
I have to pass a primitive 2d array to a filtering routine.The algorithm for filtering(median filter) is same irrespective of the type of the array.Is there a way to pass any type of array in a generic manner or should I overload the same same function with different array types.In the second case the same code will have to be repeated f...
I have a "manager" class with a number of sub-classes. I find one particular method being duplicated in all or almost all of the sub-classes, so I want to generalize it. In one case, it looks like this:
var Results =
from j in Job.All( )
where guids.Contains( j.Job...
Hi,
I am trying to write a generic function that will accept any enum, and put the values into a map for use in a drop down.
This is what I have so far, (for a specific enum), can my function enumToMap be rewritten generally to accept any enum type?
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import ...
Given a structure like this:
class Parent { }
class Child : Parent { }
I have a method that takes a generic type with a constraint that the object is of type Child
static void doSomething<T>() where T : Child
{
if (typeof(T) == typeof(Parent))
{
/* ... */
}
else if (typeof(T) == typeof(Child))
{
/...
Hi folks,
I have an object (form) which contains a collection (.Fields) which I want to contain instances of a generic class (FormField).
The FormField, simply, is defined as such:
public class FormField<T>
{
private Form Form;
public T Value { get; set; }
public string Name { get; set; }
public void Process()
{
...
I have an interface Producer<T> and a concrete FooProducer that implements Producer<Foo>. Binding this in guice looks ugly as sin:
bind(new TypeLiteral<Producer<Foo>>() {}).to(FooProducer.class);
I have lots of these such bindings. I have tried the following:
static <T> TypeLiteral<Producer<T>> producer() {
return new TypeLiteral...
What is the difference between the signatures of the two methods below?
public static <T> ReturnContainer test(Class<T> incomingClass)
{
List<TestTuple<T>> tupelo = new ArrayList<TestTuple<T>>();
ReturnContainer rc = new ReturnContainer(tupelo, incomingClass);
return rc;
}
What's the difference between requiring a return t...
I am fairly new to C# coming from Java, and I'm wondering if there's a simple way to avoid code repetition involving primitive types like this:
private Boolean AtLeastOneBufferItemIsNonZero(int[] Buffer)
{
Boolean result = false;
foreach (int Item in Buffer)
{
result = !(Item == (int)0);
if (result) break;
...
I'm trying to do something like this:
public interface IRepository<T>
{
T Get<T>(int id);
}
public interface IFooBarRepository : IRepository<Foo>, IRepository<Bar>
{
}
IFooBarRepository repo = SomeMethodThatGetsTheActualClass();
Foo foo = repo.Get<Foo>(1);
I'm getting a warning:
Type parameter 'T' has the same name as the type pa...
I have written an extension method in csharp for an MVCContrib Html helper and was surprised at the form of the generic constraint, which on the face of it seems to circularly reference itself through the type parameter.
This being said the method compiles and works as desired.
I would love to have someone explain why this works and i...
Possible Duplicate:
Why can't the C# constructor infer type?
Why is the following true:
var foo = new KeyValuePair(3,4); //doesn't compile!
var boo = new KeyValuePair<int,int>(3,4); //works fine!
I would think both lines would be legal, since the type can be (should be) inferred from the parameters. Explanation?
...
Consider the following program:
import java.util.List;
import java.util.ArrayList;
public class TypeTest {
public static class TypeTestA extends TypeTest {
}
public static class TypeTestB extends TypeTest {
}
public static final class Printer {
public void print(TypeTest t) {
System.out.prin...
Take the following example (created purely to demonstrate the point). What I am trying to do is isolate one part of the system from another and only want to expose a specific subset of the functionality externally from the assembly while internally working against the full objects methods.
This code compiles, but I get an invalid cast ...
Hi All,
Bit of a puzzler, I have a generic class
public abstract class MyClass<T> : UserControl
{
}
and I have got a type like this
Type type = Type.GetType("Type From DB as String", true, true);
and I want to create and instance of MyClass using the type... But this doesn't work.
MyClass<type> control = (MyClass<type>)LoadCon...