Using Reflection, I'm trying to obtain from a referenced assembly the types that implements IDisposable
interface, then I would try to instance the type and dispose it.
The problem I have is that on many types I get a System.MissingMethodException
exception with this exception message (home-made translation to English):
There is no a defined constructor without parameters for this object.
However, that is not True, the tested types YES has defined a parameterless constructor!.
This is an example of a type that when evaluated, I mean whe I try to instantiate, it throws that exception:
Public NotInheritable Class ControlDragger : Inherits AestheticObject : Implements IDisposable
<DebuggerStepThrough>
Public Sub New()
End Sub
<DebuggerStepThrough>
Public Sub New(ctrl As Control,
Optional enabled As Boolean = False,
Optional cursor As Cursor = Nothing)
End Sub
<DebuggerStepThrough>
Public Sub New(ctrls As IEnumerable(Of Control))
End Sub
<DebuggerNonUserCode>
Private Sub New(controlInfo As ControlDragInfo,
mouseCoordinates As Point,
location As Point)
End Sub
End Class
Why?.
Note: AestheticObject
is just a class that hides some base class members like GetHashCode()
, ToSttring()
etc., nothing relevant I think (all the other IDisposable
tested classes inherits too from AestheticObject
class and doesn't throws this exception, well, some of them throws it, and some not)
This is the method that I use to evaluate the types:
Private Sub EvaluateDisposableType(ByVal t As Type)
Dim ctor As ConstructorInfo =
(From con As ConstructorInfo In t.GetConstructors()
Where con.GetParameters().Count = 0).
DefaultIfEmpty(Nothing).
SingleOrDefault()
If (ctor IsNot Nothing) Then
Dim instance As IDisposable =
DirectCast(Activator.CreateInstance(t.GetType()), IDisposable)
' ...
End If
End Sub
Note: The method accepts a Type but in other part of the code I ensure to pass only the types that implements IDisposable
.
Aucun commentaire:
Enregistrer un commentaire