Getting an Interface from a Type

Asked

Viewed 30 times

2

I want to see if a Type is implemented by the interface System.IDirectOutput and access their methods, but I don’t know how, I’m trying to:

 If GetType(IDirectOutput).IsAssignableFrom(Expression) Then
      Return CType(Expression, IDirectOutput).InstanceOutput
 Else
      Throw New InvalidOperationException($"The specified type is not implemented from {NameOf(IDirectOutput)}.")
 End If

but it doesn’t work :(. Help me!

Updating

I tried that method and it worked: (in response to qmechanik)

 If TypeOf Expression Is IDirectOutput Then
     'statements...
 End If
  • 1

    Have you tried using the TypeOf? If TypeOf IDirectOutput Is Expression Then.

  • 1

    That’s what I just tried :P

  • 1

    :D in C#, if you’re interested, you can do (haven’t tested): if (typeof(IDirectOutput).IsAssignableFrom(Expression)) { ... }

  • Could be, I don’t like programming in C# even :P

  • I posted an answer, when possible, I put up a verifiable example.

1 answer

2


The documentation of method Object.GetType suggests that the operator TypeOf to check whether an object corresponds to a particular guy.

 If TypeOf Expression Is IDirectOutput Then
    Return CType(Expression, IDirectOutput).InstanceOutput
 Else
    Throw New InvalidOperationException($"The specified type is not implemented from {NameOf(IDirectOutput)}.")
 End If

Browser other questions tagged

You are not signed in. Login or sign up in order to post.