Hi Clint,
Your second approach should work out fine. I use that kind of thing when I
want to work with lists of generic types that have different generic
parameters.
interface IProperty
{
object Value {get; set;}
Type PropertyType {get;}
}
interface IProperty<T> : IProperty
{
new T Value {get; set;}
}
class Property<T> : IProperty<T>
{
public T Value {get;set;}
public Type PropertyType {get { return typeof(T); }}
object IProperty.Value { get { return this.Value; } set { this.Value =
(T)value } }
}
That kind of thing. Hopefully you can at read C# - if not, I can translate
for you, but am better at C# now than VB.
It also sounds like you're doing something akin to dependency injection. If
you have more detail re the problem you want to solve I could make further
suggestions.
Cheers.
James.
From: [email protected] [mailto:[email protected]]
On Behalf Of Clint Colefax
Sent: Friday, 5 November 2010 09:30
To: ozDotNet
Subject: RE: It's a question of Generics...
Thanks James,
In the end, I just added the extra generic parameters and continued, it's
not a pretty as I had hoped, but it does work.
In the end, my question boiled down to, it I create an interface that has
generic parameters
Interface IMyInterface(of T)
which I then write an implementing class such as
Class MyClass implements IMyInterface(of SomeObject)
Where I've defined the type of that generic parameter,
Then I want a function to return any implementation of the interface
Function MyFunction() As IMyInterface(of T)
I can't, as like you said, I've not defined the generic parameter
If I change this to
Function MyFunction(Of T)() As IMyInterface(Of T)
Then it will work. However, I wanted to be able to set the T to an
implementation of the interface, which has T defined in it, and have it work
it out, like
Function MyFunction(Of P As IMyInterface(Of T))() As IMyInterface(Of T)
I could then call it like
MyFunction(Of MyClass)()
and it would return an instance of MyClass.
Anyway, this was an exploratory exercise, and I'm kind of moving away from
this. One thought I did have was to have 2 interfaces, a non generic one
that defined nothing, and the generic one that inherited from it
Interface IMyInterface
Interface IMyGenericInterface (Of T)
Inherits IMyInterface
Then my function could return any instance of the non generic interface, but
I've not tried this as I've moved on.
Thanks for the feedback.
From: [email protected] [mailto:[email protected]]
On Behalf Of James Chapman-Smith
Sent: Thursday, 4 November 2010 4:36 PM
To: 'ozDotNet'
Subject: RE: It's a question of Generics...
Hi Clint,
What you're suggesting here can't work. Type inference only works when
passing a parameter to a function (not a generic parameter). If you want to
return an object of type `IBase(Of T, E)` then the function definition must
include the `T` & `E` types. Your function only has a single generic
parameter so it won't work.
Can you post a version of your original code? Or maybe more detail of your
intent? If you can, I might be able to suggest what to do.
Cheers.
James.
From: [email protected] [mailto:[email protected]]
On Behalf Of Clint Colefax
Sent: Thursday, 4 November 2010 09:55
To: [email protected]
Subject: It's a question of Generics...
Just playing around with some generics, and I'm getting one bit that I'm not
happy with. I thought I had this working, but I've changed something and now
it doesn't.
I have an interface
Interface IBase(Of T, E)
And a child interface
Interface ILevel1
And a Class
Class Level1
Implements ILevel1
Implements IBase(of string, integer)
Then I have a factory with a method as such
Function GetALevel(Of TLevel(Of T, E)() As IBase(Of T, E)
In another class, I want to call the following
Dim foo as Factory = new Factory
Dim bar as foo.GetALevel(Of ILevel1)()
I think that the type of T and E should be inferred from the ILevel
interface. I'm sure I had this working, then a made a number of changes all
around this code, then noticed it wasn't working anymore.
Can anyone explain this better?
Thanks
Clint Colefax