PC Review


Reply
Thread Tools Rate Thread

How do I know type of implementing object?

 
 
Ivan
Guest
Posts: n/a
 
      11th May 2009
Here is what I try to accomplish.
I have class that implements base class. "GetyType" static property is in
BaseClass object but will be accessed through implementing objects. How do I
know type of actual object?
So, I want to return "MyClassX" if I do MyClassX.GetType

public abstract class BaseClass

{

public static string GetType

{get{return typeof(??????); }}

}

public class MyClassX: BaseClass

{}





Thanks for any help!


 
Reply With Quote
 
 
 
 
Jesse Houwing
Guest
Posts: n/a
 
      11th May 2009
Hello Ivan,

> Here is what I try to accomplish.
> I have class that implements base class. "GetyType" static property is
> in
> BaseClass object but will be accessed through implementing objects.
> How do I
> know type of actual object?
> So, I want to return "MyClassX" if I do MyClassX.GetType
> public abstract class BaseClass
>
> {
>
> public static string GetType
>
> {get{return typeof(??????); }}
>
> }
>
> public class MyClassX: BaseClass
>
> {}
>
> Thanks for any help!


Static methods will not be inherited, so this won't work.

And why are you creating this method anyway, if you have an instance, you
can call myInstance.GetType(); and if you need to get the type statically
in code you can use typeof(MyType). And then query the Name property of the
resulting Type object. you could just use this.GetType().Name in every base
class to get their actual type name.

If you want to do it (even though I see no good reason and absolutely do
not reccommend it), then you'll have to implement a static GetType method
or property on each and every class.

--
Jesse Houwing
jesse.houwing at sogeti.nl


 
Reply With Quote
 
Jesse Houwing
Guest
Posts: n/a
 
      11th May 2009
Hello Ivan,

> Here is what I try to accomplish.
> I have class that implements base class. "GetyType" static property is
> in
> BaseClass object but will be accessed through implementing objects.
> How do I
> know type of actual object?
> So, I want to return "MyClassX" if I do MyClassX.GetType
> public abstract class BaseClass
>
> {
>
> public static string GetType
>
> {get{return typeof(??????); }}
>
> }
>
> public class MyClassX: BaseClass
>
> {}
>
> Thanks for any help!


Static methods will not be inherited, so this won't work.

And why are you creating this method anyway, if you have an instance, you
can call myInstance.GetType(); and if you need to get the type statically
in code you can use typeof(MyType). And then query the Name property of the
resulting Type object. you could just use this.GetType().Name in every base
class to get their actual type name.

If you want to do it (even though I see no good reason and absolutely do
not reccommend it), then you'll have to implement a static GetType method
or property on each and every class.

--
Jesse Houwing
jesse.houwing at sogeti.nl


 
Reply With Quote
 
Ivan
Guest
Posts: n/a
 
      12th May 2009
Jesse,

Example of course is simplified. I have many different objects inheriting
from BaseClass and they all need same functionality but it depends on
specific class name. To be more specific, it's user based permissions. So,
some users have access to MyClassX and don't have access to MyClassY. Code
to check is all the same except that I need to pass Typename to DB function.
Don't ask, I can't change architecture. All I want is to avoid programming
"GetType" or actual code in every child object.

"Jesse Houwing" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello Ivan,
>
>> Here is what I try to accomplish.
>> I have class that implements base class. "GetyType" static property is
>> in
>> BaseClass object but will be accessed through implementing objects.
>> How do I
>> know type of actual object?
>> So, I want to return "MyClassX" if I do MyClassX.GetType
>> public abstract class BaseClass
>>
>> {
>>
>> public static string GetType
>>
>> {get{return typeof(??????); }}
>>
>> }
>>
>> public class MyClassX: BaseClass
>>
>> {}
>>
>> Thanks for any help!

>
> Static methods will not be inherited, so this won't work.
>
> And why are you creating this method anyway, if you have an instance, you
> can call myInstance.GetType(); and if you need to get the type statically
> in code you can use typeof(MyType). And then query the Name property of
> the resulting Type object. you could just use this.GetType().Name in every
> base class to get their actual type name.
>
> If you want to do it (even though I see no good reason and absolutely do
> not reccommend it), then you'll have to implement a static GetType method
> or property on each and every class.
>
> --
> Jesse Houwing
> jesse.houwing at sogeti.nl
>
>



 
Reply With Quote
 
Ivan
Guest
Posts: n/a
 
      12th May 2009
Jesse,

Example of course is simplified. I have many different objects inheriting
from BaseClass and they all need same functionality but it depends on
specific class name. To be more specific, it's user based permissions. So,
some users have access to MyClassX and don't have access to MyClassY. Code
to check is all the same except that I need to pass Typename to DB function.
Don't ask, I can't change architecture. All I want is to avoid programming
"GetType" or actual code in every child object.

"Jesse Houwing" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello Ivan,
>
>> Here is what I try to accomplish.
>> I have class that implements base class. "GetyType" static property is
>> in
>> BaseClass object but will be accessed through implementing objects.
>> How do I
>> know type of actual object?
>> So, I want to return "MyClassX" if I do MyClassX.GetType
>> public abstract class BaseClass
>>
>> {
>>
>> public static string GetType
>>
>> {get{return typeof(??????); }}
>>
>> }
>>
>> public class MyClassX: BaseClass
>>
>> {}
>>
>> Thanks for any help!

>
> Static methods will not be inherited, so this won't work.
>
> And why are you creating this method anyway, if you have an instance, you
> can call myInstance.GetType(); and if you need to get the type statically
> in code you can use typeof(MyType). And then query the Name property of
> the resulting Type object. you could just use this.GetType().Name in every
> base class to get their actual type name.
>
> If you want to do it (even though I see no good reason and absolutely do
> not reccommend it), then you'll have to implement a static GetType method
> or property on each and every class.
>
> --
> Jesse Houwing
> jesse.houwing at sogeti.nl
>
>



 
Reply With Quote
 
Jesse Houwing
Guest
Posts: n/a
 
      12th May 2009
Hello Ivan,

> Jesse,
>
> Example of course is simplified. I have many different objects
> inheriting from BaseClass and they all need same functionality but it
> depends on specific class name. To be more specific, it's user based
> permissions. So, some users have access to MyClassX and don't have
> access to MyClassY. Code to check is all the same except that I need
> to pass Typename to DB function. Don't ask, I can't change
> architecture. All I want is to avoid programming "GetType" or actual
> code in every child object.


So you need access to the type, before you have created an instance? Do I
understand you correctly?

The only way to do that is to use:
typeof(MyClassY).Name

Or you could use Type.GetType(string name) to get access to the Type object,
but then you'd already have it's name as a string...

The alternative is to implement a static function in every class returning
the name (probably using the above syntax).

If you have access to an instance, before checking security, you can call:

this.GetType().Name

and you could put that in a public or protected method of your base class,
so that you have easier access like so:

public string GetTypeName()
{
return this.GetType().Name;
}

I'd use a method, as it fits nicely besides the already existing GetType()
method inherited from Object.

No need for any override or abstract methods.

Jesse


> "Jesse Houwing" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>> Hello Ivan,
>>
>>> Here is what I try to accomplish.
>>> I have class that implements base class. "GetyType" static property
>>> is
>>> in
>>> BaseClass object but will be accessed through implementing objects.
>>> How do I
>>> know type of actual object?
>>> So, I want to return "MyClassX" if I do MyClassX.GetType
>>> public abstract class BaseClass
>>> {
>>>
>>> public static string GetType
>>>
>>> {get{return typeof(??????); }}
>>>
>>> }
>>>
>>> public class MyClassX: BaseClass
>>>
>>> {}
>>>
>>> Thanks for any help!
>>>

>> Static methods will not be inherited, so this won't work.
>>
>> And why are you creating this method anyway, if you have an instance,
>> you can call myInstance.GetType(); and if you need to get the type
>> statically in code you can use typeof(MyType). And then query the
>> Name property of the resulting Type object. you could just use
>> this.GetType().Name in every base class to get their actual type
>> name.
>>
>> If you want to do it (even though I see no good reason and absolutely
>> do not reccommend it), then you'll have to implement a static GetType
>> method or property on each and every class.
>>
>> --
>> Jesse Houwing
>> jesse.houwing at sogeti.nl

--
Jesse Houwing
jesse.houwing at sogeti.nl


 
Reply With Quote
 
Jesse Houwing
Guest
Posts: n/a
 
      12th May 2009
Hello Ivan,

> Jesse,
>
> Example of course is simplified. I have many different objects
> inheriting from BaseClass and they all need same functionality but it
> depends on specific class name. To be more specific, it's user based
> permissions. So, some users have access to MyClassX and don't have
> access to MyClassY. Code to check is all the same except that I need
> to pass Typename to DB function. Don't ask, I can't change
> architecture. All I want is to avoid programming "GetType" or actual
> code in every child object.


So you need access to the type, before you have created an instance? Do I
understand you correctly?

The only way to do that is to use:
typeof(MyClassY).Name

Or you could use Type.GetType(string name) to get access to the Type object,
but then you'd already have it's name as a string...

The alternative is to implement a static function in every class returning
the name (probably using the above syntax).

If you have access to an instance, before checking security, you can call:

this.GetType().Name

and you could put that in a public or protected method of your base class,
so that you have easier access like so:

public string GetTypeName()
{
return this.GetType().Name;
}

I'd use a method, as it fits nicely besides the already existing GetType()
method inherited from Object.

No need for any override or abstract methods.

Jesse


> "Jesse Houwing" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>> Hello Ivan,
>>
>>> Here is what I try to accomplish.
>>> I have class that implements base class. "GetyType" static property
>>> is
>>> in
>>> BaseClass object but will be accessed through implementing objects.
>>> How do I
>>> know type of actual object?
>>> So, I want to return "MyClassX" if I do MyClassX.GetType
>>> public abstract class BaseClass
>>> {
>>>
>>> public static string GetType
>>>
>>> {get{return typeof(??????); }}
>>>
>>> }
>>>
>>> public class MyClassX: BaseClass
>>>
>>> {}
>>>
>>> Thanks for any help!
>>>

>> Static methods will not be inherited, so this won't work.
>>
>> And why are you creating this method anyway, if you have an instance,
>> you can call myInstance.GetType(); and if you need to get the type
>> statically in code you can use typeof(MyType). And then query the
>> Name property of the resulting Type object. you could just use
>> this.GetType().Name in every base class to get their actual type
>> name.
>>
>> If you want to do it (even though I see no good reason and absolutely
>> do not reccommend it), then you'll have to implement a static GetType
>> method or property on each and every class.
>>
>> --
>> Jesse Houwing
>> jesse.houwing at sogeti.nl

--
Jesse Houwing
jesse.houwing at sogeti.nl


 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      12th May 2009
Does this work for you???


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Generics_test

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine(A.GetType().Name);

Console.WriteLine(B.GetType().Name);

Console.ReadLine();

}

public abstract class BaseClass<T> where T : BaseClass<T>

{

public new static Type GetType()

{

return typeof(T);

}

}



public class A : BaseClass<A>

{

}

public class B : BaseClass<B>

{

}

}

}



"Jesse Houwing" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello Ivan,
>
>> Jesse,
>>
>> Example of course is simplified. I have many different objects
>> inheriting from BaseClass and they all need same functionality but it
>> depends on specific class name. To be more specific, it's user based
>> permissions. So, some users have access to MyClassX and don't have
>> access to MyClassY. Code to check is all the same except that I need
>> to pass Typename to DB function. Don't ask, I can't change
>> architecture. All I want is to avoid programming "GetType" or actual
>> code in every child object.

>
> So you need access to the type, before you have created an instance? Do I
> understand you correctly?
>
> The only way to do that is to use:
> typeof(MyClassY).Name
>
> Or you could use Type.GetType(string name) to get access to the Type
> object, but then you'd already have it's name as a string...
>
> The alternative is to implement a static function in every class returning
> the name (probably using the above syntax).
>
> If you have access to an instance, before checking security, you can call:
>
> this.GetType().Name
>
> and you could put that in a public or protected method of your base class,
> so that you have easier access like so:
>
> public string GetTypeName()
> {
> return this.GetType().Name;
> }
>
> I'd use a method, as it fits nicely besides the already existing GetType()
> method inherited from Object.
>
> No need for any override or abstract methods.
>
> Jesse
>
>
>> "Jesse Houwing" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>
>>> Hello Ivan,
>>>
>>>> Here is what I try to accomplish.
>>>> I have class that implements base class. "GetyType" static property
>>>> is
>>>> in
>>>> BaseClass object but will be accessed through implementing objects.
>>>> How do I
>>>> know type of actual object?
>>>> So, I want to return "MyClassX" if I do MyClassX.GetType
>>>> public abstract class BaseClass
>>>> {
>>>>
>>>> public static string GetType
>>>>
>>>> {get{return typeof(??????); }}
>>>>
>>>> }
>>>>
>>>> public class MyClassX: BaseClass
>>>>
>>>> {}
>>>>
>>>> Thanks for any help!
>>>>
>>> Static methods will not be inherited, so this won't work.
>>>
>>> And why are you creating this method anyway, if you have an instance,
>>> you can call myInstance.GetType(); and if you need to get the type
>>> statically in code you can use typeof(MyType). And then query the
>>> Name property of the resulting Type object. you could just use
>>> this.GetType().Name in every base class to get their actual type
>>> name.
>>>
>>> If you want to do it (even though I see no good reason and absolutely
>>> do not reccommend it), then you'll have to implement a static GetType
>>> method or property on each and every class.
>>>
>>> --
>>> Jesse Houwing
>>> jesse.houwing at sogeti.nl

> --
> Jesse Houwing
> jesse.houwing at sogeti.nl
>
>



 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      12th May 2009
Does this work for you???


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Generics_test

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine(A.GetType().Name);

Console.WriteLine(B.GetType().Name);

Console.ReadLine();

}

public abstract class BaseClass<T> where T : BaseClass<T>

{

public new static Type GetType()

{

return typeof(T);

}

}



public class A : BaseClass<A>

{

}

public class B : BaseClass<B>

{

}

}

}



"Jesse Houwing" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello Ivan,
>
>> Jesse,
>>
>> Example of course is simplified. I have many different objects
>> inheriting from BaseClass and they all need same functionality but it
>> depends on specific class name. To be more specific, it's user based
>> permissions. So, some users have access to MyClassX and don't have
>> access to MyClassY. Code to check is all the same except that I need
>> to pass Typename to DB function. Don't ask, I can't change
>> architecture. All I want is to avoid programming "GetType" or actual
>> code in every child object.

>
> So you need access to the type, before you have created an instance? Do I
> understand you correctly?
>
> The only way to do that is to use:
> typeof(MyClassY).Name
>
> Or you could use Type.GetType(string name) to get access to the Type
> object, but then you'd already have it's name as a string...
>
> The alternative is to implement a static function in every class returning
> the name (probably using the above syntax).
>
> If you have access to an instance, before checking security, you can call:
>
> this.GetType().Name
>
> and you could put that in a public or protected method of your base class,
> so that you have easier access like so:
>
> public string GetTypeName()
> {
> return this.GetType().Name;
> }
>
> I'd use a method, as it fits nicely besides the already existing GetType()
> method inherited from Object.
>
> No need for any override or abstract methods.
>
> Jesse
>
>
>> "Jesse Houwing" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>
>>> Hello Ivan,
>>>
>>>> Here is what I try to accomplish.
>>>> I have class that implements base class. "GetyType" static property
>>>> is
>>>> in
>>>> BaseClass object but will be accessed through implementing objects.
>>>> How do I
>>>> know type of actual object?
>>>> So, I want to return "MyClassX" if I do MyClassX.GetType
>>>> public abstract class BaseClass
>>>> {
>>>>
>>>> public static string GetType
>>>>
>>>> {get{return typeof(??????); }}
>>>>
>>>> }
>>>>
>>>> public class MyClassX: BaseClass
>>>>
>>>> {}
>>>>
>>>> Thanks for any help!
>>>>
>>> Static methods will not be inherited, so this won't work.
>>>
>>> And why are you creating this method anyway, if you have an instance,
>>> you can call myInstance.GetType(); and if you need to get the type
>>> statically in code you can use typeof(MyType). And then query the
>>> Name property of the resulting Type object. you could just use
>>> this.GetType().Name in every base class to get their actual type
>>> name.
>>>
>>> If you want to do it (even though I see no good reason and absolutely
>>> do not reccommend it), then you'll have to implement a static GetType
>>> method or property on each and every class.
>>>
>>> --
>>> Jesse Houwing
>>> jesse.houwing at sogeti.nl

> --
> Jesse Houwing
> jesse.houwing at sogeti.nl
>
>



 
Reply With Quote
 
Jesse Houwing
Guest
Posts: n/a
 
      12th May 2009
Hello Paul,

Nice one ... Hadn't thought of that...

Jesse

> Does this work for you???
>
> using System;
>
> using System.Collections.Generic;
>
> using System.Linq;
>
> using System.Text;
>
> namespace Generics_test
>
> {
>
> class Program
>
> {
>
> static void Main(string[] args)
>
> {
>
> Console.WriteLine(A.GetType().Name);
>
> Console.WriteLine(B.GetType().Name);
>
> Console.ReadLine();
>
> }
>
> public abstract class BaseClass<T> where T : BaseClass<T>
>
> {
>
> public new static Type GetType()
>
> {
>
> return typeof(T);
>
> }
>
> }
>
> public class A : BaseClass<A>
>
> {
>
> }
>
> public class B : BaseClass<B>
>
> {
>
> }
>
> }
>
> }
>
> "Jesse Houwing" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>> Hello Ivan,
>>
>>> Jesse,
>>>
>>> Example of course is simplified. I have many different objects
>>> inheriting from BaseClass and they all need same functionality but
>>> it depends on specific class name. To be more specific, it's user
>>> based permissions. So, some users have access to MyClassX and don't
>>> have access to MyClassY. Code to check is all the same except that I
>>> need to pass Typename to DB function. Don't ask, I can't change
>>> architecture. All I want is to avoid programming "GetType" or actual
>>> code in every child object.
>>>

>> So you need access to the type, before you have created an instance?
>> Do I understand you correctly?
>>
>> The only way to do that is to use:
>> typeof(MyClassY).Name
>> Or you could use Type.GetType(string name) to get access to the Type
>> object, but then you'd already have it's name as a string...
>>
>> The alternative is to implement a static function in every class
>> returning the name (probably using the above syntax).
>>
>> If you have access to an instance, before checking security, you can
>> call:
>>
>> this.GetType().Name
>>
>> and you could put that in a public or protected method of your base
>> class, so that you have easier access like so:
>>
>> public string GetTypeName()
>> {
>> return this.GetType().Name;
>> }
>> I'd use a method, as it fits nicely besides the already existing
>> GetType() method inherited from Object.
>>
>> No need for any override or abstract methods.
>>
>> Jesse
>>
>>> "Jesse Houwing" <(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed)...
>>>
>>>> Hello Ivan,
>>>>
>>>>> Here is what I try to accomplish.
>>>>> I have class that implements base class. "GetyType" static
>>>>> property
>>>>> is
>>>>> in
>>>>> BaseClass object but will be accessed through implementing
>>>>> objects.
>>>>> How do I
>>>>> know type of actual object?
>>>>> So, I want to return "MyClassX" if I do MyClassX.GetType
>>>>> public abstract class BaseClass
>>>>> {
>>>>> public static string GetType
>>>>>
>>>>> {get{return typeof(??????); }}
>>>>>
>>>>> }
>>>>>
>>>>> public class MyClassX: BaseClass
>>>>>
>>>>> {}
>>>>>
>>>>> Thanks for any help!
>>>>>
>>>> Static methods will not be inherited, so this won't work.
>>>>
>>>> And why are you creating this method anyway, if you have an
>>>> instance, you can call myInstance.GetType(); and if you need to get
>>>> the type statically in code you can use typeof(MyType). And then
>>>> query the Name property of the resulting Type object. you could
>>>> just use this.GetType().Name in every base class to get their
>>>> actual type name.
>>>>
>>>> If you want to do it (even though I see no good reason and
>>>> absolutely do not reccommend it), then you'll have to implement a
>>>> static GetType method or property on each and every class.
>>>>
>>>> --
>>>> Jesse Houwing
>>>> jesse.houwing at sogeti.nl

>> --
>> Jesse Houwing
>> jesse.houwing at sogeti.nl

--
Jesse Houwing
jesse.houwing at sogeti.nl


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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
How do I know type of implementing object? Ivan Microsoft C# .NET 0 11th May 2009 10:45 PM
"gridview Object does not match target type" error during binding collection of different type object gui.besse@gmail.com Microsoft ASP .NET 0 9th Mar 2006 10:28 AM
Re: Implementing on an inherited type Jay B. Harlow [MVP - Outlook] Microsoft VB .NET 1 24th Sep 2004 07:29 AM
Implementing on an inherited type =?Utf-8?B?bGpsZXZlbmQ=?= Microsoft VB .NET 0 22nd Sep 2004 10:49 PM
Issue implementing type converter Kerry Sanders Microsoft C# .NET 6 13th Nov 2003 06:28 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:26 AM.