Good morning Random,
Unfortunately, it seems you misunderstood the concept of generics.
Simplifying, generics are strictly linked to compilation, not runtime.
Therefore it has nothing to do with “passing†types at runtime. When you
declare a function with generic parameters, for instance:
Public shared class FooClass
Public shared function Foo(Of T)(byval arg as T) as T
Return arg.ToString()
End function
End class
T argument is just a “placeholder†for any type, since we haven’t applied
any constraints (I will explain constraints in a while). If you want to use
this function, you have to specify a type T. Let’s consider we are interested
in executing function with integer:
…
Dim myArg as Integer = 10
Dim result as Integer = FooClass.Foo(Of Integer)(myArg)
…
Now, when compiler sees this code, it prepares a function based on generic
template we provided above (it’s called resolving). Obviously “template†is a
better name, but Microsoft could have used it because “template(s)†as a word
and concept have been in use for quite long time, since C++ was born. It’s
definitely easier to understand the idea behind generics if you treat them as
a “templates†with T as placeholder. Knowing this it’s easy to guess our
generic function is resolved as:
Public shared class FooClass
Public shared function Foo(byval arg as Integer) as Integer
Return arg.ToString()
End function
End class
during compilation. Simple right? Whilst our function accepts all possible
types, sometime it’s necessary to put some constraints on T, for example,
template parameter T must implement particular interface i.e. IDisposable:
Public shared class FooClass
Public shared function Foo(Of T as IDisposable)(byval arg as T) as T
Dim result as string = arg.ToString()
arg.Dispose()
End function
End class
Having applied constraint, any attempt to resolve function with a class
which doesn’t implement IDisposable interface will result in compilation
error:
Dim str as string = FooClass.Foo(Of MyClassWhichDoesNotImplementIDisposable)
won’t compile.
Correct me if I’m wrong but I suspect you’re not familiar with interfaces
too. You cannot instantiate interface because interface it’s just a
definition (it provides a “description†how an object should look like
meaning what methods and properties it should have). Example:
Public interface IAeroplane
Readonly Property EngineCount as Integer
Readonly property MaximumRange() as Integer
Sub Fly()
End interface
As you can see it’s just a description of an aeroplane.
Possible implementation would be:
Public class Boeing747
Implements IAeroplane
Public readonly property EngineCount as Integer _
Implements Iaeroplane.EngineCount
Get
Return 4
End get
End Property
Public readonly property MaximumRange as Integer _
Implements Iaeroplane.MaximumRange
Get
Return 4000 ‘just guessing
End get
End Property
Public sub Fly()
‘ implement some flying logic here
End sub
‘ other methods go here
Private _series as Beoing747Series = Beoing747Series.Series400
Public property Series as Beoing747Series
Get
Return _series
End get
Set(byval value as Beoing747Series)
_series = value
End set
End property
Public enum Beoing747Series
Series400,
Series800
End enum
Public readonly property Capacity as Integer
Get
Select case _series
Case Beoing747Series.Series400
Return 300
Case Beoing747Series.Series800
Return 500
End select
End get
End property
End class
Now I can use it like this
Dim boeing747 as new Boeing747()
Dim aeroplane as IAreoplane = Ctype(boeing747, IAreoplane )
Console.WriteLine(aeroplane.MaximumRange)
I see what you are trying to do, so your code should be rewritten to this:
Public shared function CreateAnObject(Of T as IGenericInterface)() as T
Return new T()
End function
And what’s missing here is a class that implements the IGenericInterface
interface:
‘ for example your interface is declared as following
Public Interface IGenericInterface
Function DoSomething() as String
End interface
Public class ClassThatImplementsIGenericInterface
Implements IGenericInterface
Public function DoSomething() as String _
Implements IGenericInterface.Do Something _
Return DateTime.Now.ToString()
End function
…
End class
IGenericInterface var = CreateAnObject(Of
ClassThatImplementsIGenericInterface)()
String result as string = Var.DoSomething()
Hope it’s clear now. I wrote everything in notepad ;-) so if something is
not compiling you should be able to fix it yourself