Instantiating new objects with Generics

  • Thread starter Thread starter Simon Woods
  • Start date Start date
S

Simon Woods

Hi

I have a factory class which I want to use to create new objects which
inherit a certain base type so I have a declaration

Public Class MyOwnFactory(Of T As MyOwnBase)

As I understand it, a new MyOwnFactory will get instantiated for every
different type of class implementing MyOwnBase. (This is what I want
anyway as the factory is holding a cache of previously created objects
of this type)


In the class, I have some methods :


Public Function GetMyObject(ByVal p_key As String, ByVal p_reload As
Boolean) As T

....

Dim l_def As T = CreateMyObject(l_dto)

...

Return l_def
End Function

Private Function CreateMyObject() As T

Dim l_def As T = New T()

If LoadCommonData(l_def, l_data) then

endif

Return l_def

End Function

I'm getting the error

"'New' cannot be used on a type parameter that does not have a 'New'
constraint." against the line

Dim l_def As T = New T()

Because it is a factory, I want to keep the New-ing local to the factory
(so I don't want public constructors on the types inheriting MyOwnBase)
and I wondering if someone could explain a way to do this or perhaps an
alternative way of looking at this.

Thx in advance

Simon
 
Thx Bill

When I introduce the New constraint, an error is generated

"'MyOwnClasses.MyOwnClass' must have a public parameterless instance
constructor to satisfy the 'New' constraint for type parameter 'T'."

where the factory is instantiated with

Dim l_factory As MyOwnClasses.Factory(Of MyOwnClasses.MyOwnClass) _
= MyOwnClasses.Factory(Of MyOwnClasses.MyOwnClass).Instance

(It's a singleton btw)

I'm not too sure how to get round this, as it seems that it is looking
for a public New constructor which I don't want to supply because I want
the factory to be doing all the newing

Thanks again

S
 
So is it a case of using Reflection to lookup the type of T and create
an object of that type and return it as a T ... or will I suffer from
the same problem that Reflection also needs a public constructor.

I have been looking at this approach, but have encountered a similar
problem (but this time at runtime presumably because of its latebound
nature) when trying to use the following - ...

Dim l_myOwnClass As T = CType(Activator.CreateInstance(GetType(T), _
'
System.Reflection.BindingFlags.NonPublic Or _
'
System.Reflection.BindingFlags.Instance), T)
 
The reflection call should work. You want to include the
System.Reflection.BindingFlags.CreateInstance flag
 
Ok ... thx very much Bill

So I've tried this cmd with a protected, private and friend constructor
on MyOwnClass

Return CType(Activator.CreateInstance(GetType(T), _
System.Reflection.BindingFlags.NonPublic Or
_
System.Reflection.BindingFlags.CreateInstance), T)

but it's erroring with

System.MissingMethodException: Constructor on type
'MyOwnClasses.MyOwnClass' not found..

Any other suggestions ? I also tried putting an explicit constructor on
MyOwnBase Because MyOwnClass is inheriting it but got the same error

thx again
 
Simon said:
Ok ... thx very much Bill

Actually Bill, ... I'm not going with the Reflection approach because it
seems noticably slower even to report an error compare to the New T()
approach ... so I'll have a rethink about how to re-engineer this so I
get all early binding.

thx very much again...

Simon
 
Right as it should. There is no overload that matches that .

CType(Activator.CreateInstance(GetType(T), CreateInstance Or [Public] Or
Instance, Nothing, Nothing, Nothing), T)
 
Bill said:
Right as it should. There is no overload that matches that .

CType(Activator.CreateInstance(GetType(T), CreateInstance Or [Public] Or
Instance, Nothing, Nothing, Nothing), T)

That did it ... thx (I was in a vb6 mode thinking the params were optional!)
 
Back
Top