Typically, I've seen people use the class literal like this:
Class<Foo> cls = Foo.class;
But what if the type is generic, e.g. List? This works fine, but has a warning since List should be parameterized:
Class<List> cls = List.class
So why not add a <?>? Well, this causes a type mismatch error:
Class<List<?>> cls = List.class
I ...
I have a method
String Foo<T> where T: WebControl
Now I do have a string like "hyperlink". What is want is to call Foo<Hyperlink> based on a mapping from the string to the generic.
How does the dictionary have to look like?
It ain't:
private Dictionary<string, Type> _mapping = new Dictionary<string, Type>()
{
{"hyperlink", ty...
Is it possible to enumerate which types that is "available" in a generic constraint?
T MyMethod<t>() where T : int, double, string
Why I want to do this is that I have a small evaluator engine and would like to
write code like this:
bool expression.Evaluate<bool>();
or
int expression.Evaluate<int>();
but i want to prohibit
MyC...
I have a function written in C# which has one parameter type as Dictionary<string , string>. When i try to call this function in VB.Net it doesn't show the type as Dictionary<string , string> it shows as string. Below is my function
public bool RegisterCustomerAttribute(int CustomerId
, Diction...
Usually, when A is inheriting from B, all the members of A are automatically visible to B's functions, for example
class A {
protected:
int a;
};
class B : public A {
int getA() {return a;}
//no need to use A::a, it is automatically visible
};
However when I'm inheriting with templates, this code becomes illegal (at least...
I am trying to do this:
List<Parent> test = new List<Child>();
The complete code of my class is this:
class Program
{
public static void Main(string[] args)
{
List<Parent> test = new List<Child>();
test.Add(new Child());
test.Add(new AnotherChild());
}
}
class Parent { }
class Child : Parent { ...
I could use some really good links that explain Generics and how to use them. But I also have a very specific question, relater to working on a current project.
Given this class constructor:
public class SecuredDomainViewModel<TDomainContext, TEntity> : DomainViewModel<TDomainContext, TEntity>
where TDomainContext : Doma...
In .NET, the Generics Lists have a sort function that accepts IComparer or Comparison. I'd like to sort just part of a list. Hopefully I can specify the start index, count of elements to sort, and a lambda function. It looks like you can only use lambda functions to do this if you're sorting the entire list. Is that right or did I mi...
I want to get type of TKey and TValue given a Dictionary<TKey,TValue> type.
eg. If type is Dictionary<Int32,String> I want to know how to get
keyType = typeof(Int32) and
valueType = typeof(String)
...
Hello,
I want to make a generic string to numeric converter, and provide it as a string extension, so I wrote the following code:
public static bool TryParse<T>( this string text, out T result, IFormatProvider formatProvider ) where T : struct
try
{
result = (T)Convert.ChangeType( text, typeof( T ), formatProvider );
return true;...
I was developing a small function when trying to run an enumerator across a list and then carry out some action. (Below is an idea of what I was trying to do.
When trying to remove I got a "Collection cannot be modified" which after I had actually woken up I realised that tempList must have just been assigned myLists reference rather t...
I'm attempting to call a constructor method that looks like:
public static SomeWrapper<T> method(Class<T> arg);
When T is an unparameterized type like String or Integer, calling is straightforward:
SomeWrapper<String> wrapper = method(String.class);
Things get tricky when T is a parameterized type like List<String>. The following ...
I'm hashing a file with one or more hash algorithms. When I tried to parametrize which hash types I want, it got a lot messier than I was hoping.
I think I'm missing a chance to make better use of generics or LINQ. I also don't like that I have to use a Type[] as the parameter instead of limiting it to a more specific set of type (H...
This question might seem trivial and also stupid at the first glance, but it is much more than this.
I have an array of any type T (T[]) and I want to convert it into a List generic (List<T>). Is there any other way apart from creating a Generic list, traversing the whole array and adding the element in the List?
Present Situation:
st...
I'm using LINQtoSQL and I want to return a list of matching records for a CSV contains a list of IDs to match. The following code is my starting point, having turned a CSV string in a string array, then into a generic list (which I thought LINQ would like) - but it doesn't:
Error
Error 22 Operator '==' cannot be applied to operands of ...
What is the correct way to return a Void type, when it isn't a primitive? Eg. I currently use null as below.
interface B<E>{ E method(); }
class A implements B<Void>{
public Void method(){
// do something
return null;
}
}
...
I want to write a generic function to calculate factorial in C# ... like:
static T Factorial<T>(T n)
{
if (n <= 1)
return 1;
return Factorial<T>(n - 1);
}
but obviously having restriction that we can't perform operations on type 'T'. any alternative?
...
I'm working with Google Web Toolkit, and I'm having problems implementing a generic interface. I'm not really familiar with generics, doing an upgrade on someone else's code here.
Here's what I want to do: I want to have an implementation of a generic callback interface that does some logging, and then subclass that implementation in or...
I want to create a generic method to serizlize a class to text (for use as part of a networking component)
The method should be something like:
public string SerializeToText<T>(T DataToSerialize);
The method contents would simply perform the xml serialization and I can do this. What I want to know is if I can check whether T can be s...
Consider I have the following interface:
public interface A { public void b(); }
However I want each of the classes that implement it to have a different return type for the method b().
Examples:
public class C {
public C b() {}
}
public class D {
public D b() {}
}
How would I define my interface so that this was possibl...