Instantiating new objects with Generics

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
 
S

Simon Woods

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
 
B

Bill McCarthy

From generic code the only constraint available for creating instances is
New
 
S

Simon Woods

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)
 
B

Bill McCarthy

The reflection call should work. You want to include the
System.Reflection.BindingFlags.CreateInstance flag
 
S

Simon Woods

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
 
S

Simon Woods

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
 
B

Bill McCarthy

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)
 
S

Simon Woods

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!)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Help re Generics 5
Generics 6
Instantiating object returns nothing 7
(New Object).Method 4
Generic Linked List 9
Instantiating class variables in parent constructor. 19
Generics Question. 3
Converting c# to vb.net 1

Top