Enforce interface implementation on propperty
Hi All,
I'm in a situation I need to Expose an object.
the object I need to expose in a namespace I'm not allowed to reference directly as the application is modular and the module can be loaded or not.
In a class I've got this Propperty and function:
Code:
Public Property ObjectToExpose As Object Implements IExposedObject.ObjectToExpose
Get
Return _ObjectToExpose
End Get
Set(value As Object)
If value IsNot Nothing Then
If HasInterfaces(value) Then
_ObjectToExpose = value
_id = value.id
Else
Throw New Exception("Item doesn't support Idirty or IselectionListData")
End If
End If
End Set
End Property
Private Function HasInterfaces(ByVal Item As Object) As Boolean
Dim SelctionInterface As String = "ISelectionListData"
Dim DirtyInterface As String = "IDirty"
Dim result As Boolean
If Item.GetType.GetInterface(DirtyInterface) IsNot Nothing Then
result = (Item.GetType.GetInterface(SelctionInterface) IsNot Nothing)
End If
Return result
End Function
How can I enforce that the object has these interfaces in this line?
Public Property ObjectToExpose As Object
Something like:
Public Property (T as {"Idrty","ISelectionListData"}) ObjectToExpose As Object
Re: Enforce interface implementation on propperty
If the interface defines that property then anything that implements the interface must implement the property. So if your object implements IExposedObject it must, in turn implement the ObjectToExpose property (assuming that's a property on the interface). You don't need to do anything. I have a feeling I might be miss-understanding the question though.
Re: Enforce interface implementation on propperty
Hi FunkyDexter,
No, IExposedObject is the interface that's implemented. But you did set me on the right way for an escape route
Code:
Public Property ObjectToExpose As IObjecttoExpose Implements IExposedObject.ObjectToExpose
Interface IObjectToExpose
Inherits Idirty, IselectionListdata
end interface
The disadvantage is that I need to rewrite every class I want to expose to implement IObjecttoExpose instead of Idirty and ISelectionlistData.