Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > how to find the type of an object represented by an interface

Reply
Thread Tools

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

 
 
Random
Guest
Posts: n/a
 
      02-23-2007
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.


 
Reply With Quote
 
 
 
 
=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
Posts: n/a
 
      02-24-2007
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.
>
>
>

 
Reply With Quote
 
 
 
 
Random
Guest
Posts: n/a
 
      02-24-2007
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]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>>
>>
>>



 
Reply With Quote
 
=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
Posts: n/a
 
      02-24-2007
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


--
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]" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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.
> >>
> >>
> >>

>
>
>

 
Reply With Quote
 
=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
Posts: n/a
 
      02-24-2007
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]" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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.
>>>
>>>
>>>

>
>



--
Gran Andersson
_____
http://www.guffa.com
 
Reply With Quote
 
Random
Guest
Posts: n/a
 
      02-26-2007
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]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>
> --
> 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]" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> > 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.
>> >>
>> >>
>> >>

>>
>>
>>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How can this Java method header be represented in Ruby Abder-Rahman Ali Ruby 7 09-20-2010 02:00 PM
How to get the minimum number that can be represented? Peng Yu Python 8 09-21-2009 05:27 AM
how is a struct represented internally. xpika C Programming 5 01-14-2008 05:42 AM
How do I check the type of the value represented in a system.type? Don ASP .Net 5 09-13-2005 03:57 PM



Advertisments