how to find the type of an object represented by an interface

R

Random

How can I use reflection (or some other method) to find the type of an
object that has been passed in to my method under an interface definition?

I try to use GetType, but that won't work.
 
R

Random

Okay. Actually, I found the answer (seems I always stumble on it shortly
after posting), but it led to another problem.

Sub GetNewObjectOfType(ByVal sender As IGenericInference)
Dim o As Object = sender
Dim T As Type = o.GetType()

'now, what I need to do is call a method that takes a generics argument
Dim newobject as Object = SomeObjectOfType(Of T)()
End Sub

Function SomeObjectOfType(Of T As IGenericInference)() As T
(code to create new object of type T)
End Function

Looks like I can't call SomeObjectOfType(Of T)() with a variable as a
reference. Weird, because if T was passed into the Sub via a generics Of
statement I would be able to pass it on.
 
G

Guest

Good morning Random,

Unfortunately, it seems you misunderstood the concept of generics.
Simplifying, generics are strictly linked to compilation, not runtime.
Therefore it has nothing to do with “passing†types at runtime. When you
declare a function with generic parameters, for instance:

Public shared class FooClass

Public shared function Foo(Of T)(byval arg as T) as T
Return arg.ToString()
End function

End class

T argument is just a “placeholder†for any type, since we haven’t applied
any constraints (I will explain constraints in a while). If you want to use
this function, you have to specify a type T. Let’s consider we are interested
in executing function with integer:

…
Dim myArg as Integer = 10
Dim result as Integer = FooClass.Foo(Of Integer)(myArg)
…

Now, when compiler sees this code, it prepares a function based on generic
template we provided above (it’s called resolving). Obviously “template†is a
better name, but Microsoft could have used it because “template(s)†as a word
and concept have been in use for quite long time, since C++ was born. It’s
definitely easier to understand the idea behind generics if you treat them as
a “templates†with T as placeholder. Knowing this it’s easy to guess our
generic function is resolved as:


Public shared class FooClass

Public shared function Foo(byval arg as Integer) as Integer
Return arg.ToString()
End function

End class

during compilation. Simple right? Whilst our function accepts all possible
types, sometime it’s necessary to put some constraints on T, for example,
template parameter T must implement particular interface i.e. IDisposable:

Public shared class FooClass

Public shared function Foo(Of T as IDisposable)(byval arg as T) as T
Dim result as string = arg.ToString()
arg.Dispose()
End function

End class

Having applied constraint, any attempt to resolve function with a class
which doesn’t implement IDisposable interface will result in compilation
error:

Dim str as string = FooClass.Foo(Of MyClassWhichDoesNotImplementIDisposable)

won’t compile.

Correct me if I’m wrong but I suspect you’re not familiar with interfaces
too. You cannot instantiate interface because interface it’s just a
definition (it provides a “description†how an object should look like
meaning what methods and properties it should have). Example:

Public interface IAeroplane

Readonly Property EngineCount as Integer
Readonly property MaximumRange() as Integer
Sub Fly()

End interface

As you can see it’s just a description of an aeroplane.

Possible implementation would be:

Public class Boeing747
Implements IAeroplane

Public readonly property EngineCount as Integer _
Implements Iaeroplane.EngineCount
Get
Return 4
End get
End Property

Public readonly property MaximumRange as Integer _
Implements Iaeroplane.MaximumRange
Get
Return 4000 ‘just guessing
End get
End Property

Public sub Fly()
‘ implement some flying logic here
End sub

‘ other methods go here

Private _series as Beoing747Series = Beoing747Series.Series400
Public property Series as Beoing747Series
Get
Return _series
End get
Set(byval value as Beoing747Series)
_series = value
End set
End property

Public enum Beoing747Series
Series400,
Series800
End enum

Public readonly property Capacity as Integer
Get
Select case _series
Case Beoing747Series.Series400
Return 300
Case Beoing747Series.Series800
Return 500
End select
End get
End property

End class



Now I can use it like this

Dim boeing747 as new Boeing747()
Dim aeroplane as IAreoplane = Ctype(boeing747, IAreoplane )

Console.WriteLine(aeroplane.MaximumRange)


I see what you are trying to do, so your code should be rewritten to this:


Public shared function CreateAnObject(Of T as IGenericInterface)() as T
Return new T()
End function

And what’s missing here is a class that implements the IGenericInterface
interface:

‘ for example your interface is declared as following

Public Interface IGenericInterface
Function DoSomething() as String
End interface


Public class ClassThatImplementsIGenericInterface
Implements IGenericInterface

Public function DoSomething() as String _
Implements IGenericInterface.Do Something _
Return DateTime.Now.ToString()
End function
…
End class

IGenericInterface var = CreateAnObject(Of
ClassThatImplementsIGenericInterface)()
String result as string = Var.DoSomething()

Hope it’s clear now. I wrote everything in notepad ;-) so if something is
not compiling you should be able to fix it yourself :D
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

But you are not using any variable as reference, you are using "T" which
has not been defined in that scope. Specify the actual type of the
object that you want to create.
 
R

Random

You're right. I wasn't thinking clearly in that regard. Of course, if the
compiler can't determine the type at that time, then I can't use it in that
fashion. I may have to pass type as a parameter... didn't want to do that,
though.
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top