In this section of a function (.NET 2.0):
public void AttachInput<T>(T input) where T : struct
{
if (input is int)
{
Inputs.Add((int)input);
}
}
The compiler shows the error "Cannot convert type 'T' to 'int'.
So, I used Convert.ToInt32() which worked - but does it box input to an object? Is there a better solution?...
I am trying out a little reflection and have a question on how the cast the result object to an IList.
Here is the reflection:
private void LoadBars(Type barType)
{
// foo has a method that returns bars
Type foo = typeof(Foo);
MethodInfo method = foo.GetMethod("GetBars")
.MakeGenericMethod(bar);
object obj = m...
I'm trying to use Enumerable.ToList() in PowerShell. Apparently, to do that, I have to explicitly convert the object to IEnumerable<CustomType>, but I am unable to do that. It seems I can't correctly write IEnumerable<CustomType> in PowerShell. Both IEnumerable<string> and CustomType itself work correctly (the custom type I'm trying to u...
Say, for instance, I have a class:
public class MyFoo : IMyBar
{
...
}
Then, I would want to use the following code:
List<MyFoo> classList = new List<MyFoo>();
classList.Add(new MyFoo(1));
classList.Add(new MyFoo(2));
classList.Add(new MyFoo(3));
List<IMyBar> interfaceList = new List<IMyBar>(classList);
But this produces the e...
In my utility method:
public static <T> T getField(Object obj, Class c, String fieldName) {
try {
Field field = c.getDeclaredField(fieldName);
field.setAccessible(true);
return (T) field.get(obj);
} catch (Exception e) {
e.printStackTrace();
fail();
return null;
}
}
The line ...
base class:
Class List(Of T)
Function Contains(ByVal value As T) As Boolean
derived class:
Class Bar : List(Of Exception) ' Exception type as example '
Function Contains(Of U)(ByVal value As U) As Boolean
compiler tells me that that two are the same, so I need to declare Overloads/new this second function.
But I want use U...
Just I am learning Generics.When i have an Abstract Method pattern like :
//Abstract Product
interface IPage
{
string pageType();
}
//Concerete Product 1
class ResumePage : IPage
{
public string pageType()
{
return "Resume Page";
}
}
//Concrete Product 2
class SummaryPage : IPage
{
public string pageType()
...
I am trying to do the following in Delphi 2010:
TDataConverter = class abstract
public
function Convert<T>(const AData: T): string; virtual; abstract;
end;
However, I keep getting the following compiler error:
E2533 Virtual, dynamic and message methods cannot have type parameters
I don't quite understand the reason why I can't d...
If I have a method in MyClass such as
setSuperClassList(List<Superclass>)
...should I be able to do this:
new MyClass().setSuperClassList(new ArrayList<Subclass>())
It appears this won't compile. Why?
...
Hello,
Sometimes, when using Java reflection or some special storing operation into Object, you end up with unchecked warnings. I got used to it and when I can't do anything about it, I document why one call is unchecked and why it should be considered as safe.
But, for the first time, I've got an error about a unchecked call. This fun...
See code just bellow
Our generic interface
public interface Repository<INSTANCE_CLASS, INSTANCE_ID_CLASS> {
void add(INSTANCE_CLASS instance);
INSTANCE_CLASS getById(INSTANCE_ID_CLASS id);
}
And a single class
public class Order {
private Integer id;
private Integer orderNumber;
// getter's and setter's
...
This may be a bit of an abstract question, so apologies in advance.
I am looking into generics in .NET, and was wondering about the where T : struct constraint.
I understand that this allows you to restrict the type used to be a value type. My question is, without any type constraint, you can do a limited number of operations on T.
D...
Hi everyone,
My problems is in a Struts2 action, where
I have a class :
public class MyAction<T> extends ActionSupport
with a private member like this :
private T myData;
And I would like to declare this aciton in the struts.xml file,
how can i manage to do so ?
Thanks for the answer.
Ps : I've tried without declaration of T...
I'm trying to create a data model for WCF based off of interfaces from my core object model, but I'm having trouble with some of the associations
Currently I have this in my core data model
public class A : IA {
public string name { /* ... */ }
public EntitySet<B> children { /* ... */ }
}
public class B : IB {
publ...
I would like to do this but it does not work.
bool TryGetEnum<TEnum, TValue>(TValue value, out TEnum myEnum)
{
value = default(TEnum);
if (Enum.IsDefined(typeof(TEnum), value))
{
myEnum = (TEnum)value;
return true;
}
return false;
}
Example usage:
MyEnum mye;
bool success = this.TryGetEnum<MyEnum,...
Visual sutdio 2008 has the ability to automatically create unit test stubs. I have used this to create some basic unit tests, but I am confused by something:
private class bla : BaseStoreItem
{
//
}
/// <summary>
///A test for StoreData
///</summary>
public void StoreDataTestHelper<T>() where T : BaseStoreItem
...
Hi,
Why doesn't the add() method here compile?
Vector<?> vectorList[] = new Vector<?>[10];
vectorList[0].add("elem1");
Thanks
...
Hello,
I am wondering if there's an elegant solution for doing this in Java (besides the obvious one - of declaring a different/explicit function. Here is the code:
private static HashMap<String, Integer> nameStringIndexMap
= new HashMap<String, Integer>();
private static HashMap<Buffer, Integer> nameBufferIndexMap
= ...
How to get generic interface type for an instance ?
Suppose this code:
interface IMyInterface<T>
{
T MyProperty { get; set; }
}
class MyClass : IMyInterface<int>
{
#region IMyInterface<T> Members
public int MyProperty
{
get;
set;
}
#endregion
}
MyClass myClass = new MyClass();
/* returns the ...
Is there a limit on the amount of generic parameters you can have on a type in .NET? Either hard limit (like 32) or a soft limit (where it somehow effects performance to much, etc.)
What I'm referring to is:
class Foo<T0, T2, T3, T4, etc.> {
}
...