How do I know type of implementing object?

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";

}

}

}

}
 
P

Paul

Sorry misread your question.

Well you can add a generic parameter onto the calling method and pass in the
calling objects type. Rinse and repeat.

public static BaseClass<T> GetInstance<CallingCodeT>()

{

Console.WriteLine("Called by " + typeof(CallingCodeT).Name);

return new T();

}

You can only pass the calling object in if you have an instance of it
though. In my example Main is static so I do not have an instance to pass.



BTW the where clause is useful in many circumstances firstly it stops you
passing an incorrect type. It will also allow you to access properties and
methods of that generic type at design time in the code, also specifies that
the object supports new which allows us to then create instances of it.

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?


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

Paul

Sorry misread your question.

Well you can add a generic parameter onto the calling method and pass in the
calling objects type. Rinse and repeat.

public static BaseClass<T> GetInstance<CallingCodeT>()

{

Console.WriteLine("Called by " + typeof(CallingCodeT).Name);

return new T();

}

You can only pass the calling object in if you have an instance of it
though. In my example Main is static so I do not have an instance to pass.



BTW the where clause is useful in many circumstances firstly it stops you
passing an incorrect type. It will also allow you to access properties and
methods of that generic type at design time in the code, also specifies that
the object supports new which allows us to then create instances of it.

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?


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

Thanks a lot!

I guess I need to get better grasp on this. Right now its fuzzy in my head,
but I'm getting there.

Paul said:
Sorry misread your question.

Well you can add a generic parameter onto the calling method and pass in
the calling objects type. Rinse and repeat.

public static BaseClass<T> GetInstance<CallingCodeT>()

{

Console.WriteLine("Called by " + typeof(CallingCodeT).Name);

return new T();

}

You can only pass the calling object in if you have an instance of it
though. In my example Main is static so I do not have an instance to pass.



BTW the where clause is useful in many circumstances firstly it stops you
passing an incorrect type. It will also allow you to access properties and
methods of that generic type at design time in the code, also specifies
that the object supports new which allows us to then create instances of
it.

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?


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>

{

}

}

}



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

Thanks a lot!

I guess I need to get better grasp on this. Right now its fuzzy in my head,
but I'm getting there.

Paul said:
Sorry misread your question.

Well you can add a generic parameter onto the calling method and pass in
the calling objects type. Rinse and repeat.

public static BaseClass<T> GetInstance<CallingCodeT>()

{

Console.WriteLine("Called by " + typeof(CallingCodeT).Name);

return new T();

}

You can only pass the calling object in if you have an instance of it
though. In my example Main is static so I do not have an instance to pass.



BTW the where clause is useful in many circumstances firstly it stops you
passing an incorrect type. It will also allow you to access properties and
methods of that generic type at design time in the code, also specifies
that the object supports new which allows us to then create instances of
it.

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?


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>

{

}

}

}



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.
 

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