How do I know type of implementing object?

I

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!
 
J

Jesse Houwing

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.
 
J

Jesse Houwing

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.
 
I

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.
 
I

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.
 
J

Jesse Houwing

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
 
J

Jesse Houwing

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
 
P

Paul

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>

{

}

}

}
 
P

Paul

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>

{

}

}

}
 
J

Jesse Houwing

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>

{

}

}

}

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
 
J

Jesse Houwing

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>

{

}

}

}

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
 
I

Ivan

Looks like it is what I need, now if you can explain what this means:

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

:))

Thank you,
Ivan

Paul said:
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 said:
Hello Ivan,


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
 
I

Ivan

Looks like it is what I need, now if you can explain what this means:

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

:))

Thank you,
Ivan

Paul said:
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 said:
Hello Ivan,


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
 
I

IntoDevel

It means that T must implement BaseClass<T>.

For example

public class ChildClass : BaseClass<ChildClass>{}

http://social.msdn.microsoft.com/forums/en-US/vstscode/thread/87d01a55-9296-4b2c-ba6d-2d746f5fd32a
shows an example of the usage.


Ivan said:
Looks like it is what I need, now if you can explain what this means:

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

:))

Thank you,
Ivan

Paul said:
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 said:
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



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.
 
I

IntoDevel

It means that T must implement BaseClass<T>.

For example

public class ChildClass : BaseClass<ChildClass>{}

http://social.msdn.microsoft.com/forums/en-US/vstscode/thread/87d01a55-9296-4b2c-ba6d-2d746f5fd32a
shows an example of the usage.


Ivan said:
Looks like it is what I need, now if you can explain what this means:

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

:))

Thank you,
Ivan

Paul said:
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 said:
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



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.
 
I

Ivan

I want to ask for more.. Not only I want type of implementing class. I want
reference to that implementing class to be available in base class.
Possible?


Paul said:
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 said:
Hello Ivan,


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
 
I

Ivan

I want to ask for more.. Not only I want type of implementing class. I want
reference to that implementing class to be available in base class.
Possible?


Paul said:
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 said:
Hello Ivan,


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
 
B

Ben Voigt [C++ MVP]

Ivan said:
I want to ask for more.. Not only I want type of implementing class. I
want reference to that implementing class to be available in base class.
Possible?

As long as you have an instance method rather than a static method (or
property), then you will have that reference in the "this" keyword.
 
B

Ben Voigt [C++ MVP]

Ivan said:
I want to ask for more.. Not only I want type of implementing class. I
want reference to that implementing class to be available in base class.
Possible?

As long as you have an instance method rather than a static method (or
property), then you will have that reference in the "this" keyword.
 
P

Paul

I presume you mean like a factory method but not.

try this...

Changes in the Generics where clause to allow you to create new objects of
T, and a get instance method and some methods so we can identify the
intances.

Hope this helps
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.WriteLine((A.GetInstance().Name()));

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

Console.ReadLine();

}

public abstract class BaseClass<T> where T : BaseClass<T>,new()

{

public new static Type GetType()

{

return typeof(T);

}

public static BaseClass<T> GetInstance()

{

return new T();

}

public abstract string Name();


}



public class A : BaseClass<A>

{



public override string Name()

{

return "A";

}

}

public class B : BaseClass<B>

{

public override string Name()

{

return"B";

}

}

}

}
 

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

Top