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

Discussion in 'ASP .Net' started by Random, Feb 23, 2007.

  1. Random

    Random Guest

    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.
     
    Random, Feb 23, 2007
    #1
    1. Advertising

  2. Show us the code
    --
    Milosz


    "Random" wrote:

    > 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.
    >
    >
    >
     
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Feb 24, 2007
    #2
    1. Advertising

  3. Random

    Random Guest

    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.

    "Milosz Skalecki [MCAD]" <> wrote in message
    news:...
    > Show us the code
    > --
    > Milosz
    >
    >
    > "Random" wrote:
    >
    >> 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.
    >>
    >>
    >>
     
    Random, Feb 24, 2007
    #3
  4. 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


    --
    Milosz


    "Random" wrote:

    > 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.
    >
    > "Milosz Skalecki [MCAD]" <> wrote in message
    > news:...
    > > Show us the code
    > > --
    > > Milosz
    > >
    > >
    > > "Random" wrote:
    > >
    > >> 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.
    > >>
    > >>
    > >>

    >
    >
    >
     
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Feb 24, 2007
    #4
  5. 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.

    Random wrote:
    > 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.
    >
    > "Milosz Skalecki [MCAD]" <> wrote in message
    > news:...
    >> Show us the code
    >> --
    >> Milosz
    >>
    >>
    >> "Random" wrote:
    >>
    >>> 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.
    >>>
    >>>
    >>>

    >
    >



    --
    Göran Andersson
    _____
    http://www.guffa.com
     
    =?ISO-8859-1?Q?G=F6ran_Andersson?=, Feb 24, 2007
    #5
  6. Random

    Random Guest

    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.

    "Milosz Skalecki [MCAD]" <> wrote in message
    news:...
    > 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
    >
    >
    > --
    > Milosz
    >
    >
    > "Random" wrote:
    >
    >> 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.
    >>
    >> "Milosz Skalecki [MCAD]" <> wrote in message
    >> news:...
    >> > Show us the code
    >> > --
    >> > Milosz
    >> >
    >> >
    >> > "Random" wrote:
    >> >
    >> >> 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.
    >> >>
    >> >>
    >> >>

    >>
    >>
    >>
     
    Random, Feb 26, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Don
    Replies:
    5
    Views:
    634
    S. Justin Gengo
    Sep 13, 2005
  2. xpika

    how is a struct represented internally.

    xpika, Jan 3, 2008, in forum: C Programming
    Replies:
    5
    Views:
    351
    David Thompson
    Jan 14, 2008
  3. Peng Yu
    Replies:
    8
    Views:
    329
    Simon Forman
    Sep 21, 2009
  4. Abder-Rahman Ali
    Replies:
    7
    Views:
    118
    Abder-Rahman Ali
    Sep 20, 2010
  5. Replies:
    41
    Views:
    408
Loading...

Share This Page