473,385 Members | 1,331 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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.
Feb 23 '07 #1
5 1567
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.
Feb 24 '07 #2
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]" <mi*****@DONTLIKESPAMwp.plwrote in message
news:F0**********************************@microsof t.com...
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.

Feb 24 '07 #3
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]" <mi*****@DONTLIKESPAMwp.plwrote in message
news:F0**********************************@microsof t.com...
>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
Feb 24 '07 #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]" <mi*****@DONTLIKESPAMwp.plwrote in message
news:F0**********************************@microsof t.com...
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.


Feb 24 '07 #5
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]" <mi*****@DONTLIKESPAMwp.plwrote in message
news:87**********************************@microsof t.com...
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]" <mi*****@DONTLIKESPAMwp.plwrote in message
news:F0**********************************@microso ft.com...
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.



Feb 26 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tongu? Yumruk | last post by:
I have a little proposal about type checking in python. I'll be glad if you read and comment on it. Sorry for my bad english (I'm not a native English speaker) A Little Stricter Typing in Python...
8
by: Wells Caughey | last post by:
Hello everyone, I am trying to load and use a .NET object in Web Page as described by ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpguide/html/cpcondeployingco...
8
by: Rade | last post by:
Following a discussion on another thread here... I have tried to understand what is actually standardized in C++ regarding the representing of integers (signed and unsigned) and their conversions....
1
by: Joe Jax | last post by:
I have an object which has a base object hierarchy as well as a number of implemented interfaces. Given any one property on that object, how do I find out which base class or interface defines that...
10
by: Bob | last post by:
This has been bugging me for a while now. GetType isn't availble for variables decalred as interface types, I have to DirectCast(somevariable, Object). In example: Sub SomeSub(ByVal...
14
by: Craig Buchanan | last post by:
If I have two custom vb.net classes, where 80% of the properties are alike and there is one method with a matching signature, can i cast between one and the other? do i need to have each class...
5
by: SunnyDrake | last post by:
HI! I wrting some program part of it is XML config parser which contains some commands(for flexibility of engenie). how do i more simple(if it possible not via System.Reflection or...
15
by: shuisheng | last post by:
Dear All, Assume I have a class named Obj. class Obj { }; And a class named Shape which is derived from Obj. class Shape: public Obj
3
by: =?Utf-8?B?R3JlZyBN?= | last post by:
Hello, I'm running an asp.net, intranet web application using .net framework 1.1 on IIS5.1 / 6.0. Through the web application, I would like to press a button on the web page, have another window...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.