Sign in to follow this  
coolblue

[.net] Generics Problem

Recommended Posts

I have the following method: public static IComparer SortDateCreated(SortDirection Direction,Type t) { return (IComparer)new SortHelper(Direction,"FormID",thisType); } The problem is that I would like to get the type (and pass it in via the thisType Parameter) that this static method belongs to but I can't use 'this' in a static method. How do I get the type in a static method?

Share this post


Link to post
Share on other sites
Since SortDateCreated is a static member, it must be a member of some class, for example:

class Foo
{
public static IComparer SortDateCreated(SortDirection Direction,Type t)
{
return (IComparer)new SortHelper(Direction,"FormID",thisType);
}
}


in which case the type of the containing class is known at compile time by the programmer. To get a Type object for a particular known type, use

Type thisType = typeof(Foo);


This doesn't appear to have anything to do with generics.

Share this post


Link to post
Share on other sites
It did have something to do with generics until I changed the code slightly. Unfortunately I still had this in my head when posting.

I do not know the type at compile time as this will be in a base class and therefore will need to react to any classes that inherit from it. I will need to get the inheriting classes type as I am then calling another class which is using reflection to return properties etc.

Share this post


Link to post
Share on other sites
Quote:

I do not know the type at compile time as this will be in a base class and therefore will need to react to any classes that inherit from it. I will need to get the inheriting classes type as I am then calling another class which is using reflection to return properties etc.


Then you have to make it non-static, or manually pass an instance of a derived (as a base) object. The method itself is statically-bound to its class, even if subclasses ultimately exist, as far as the method is concerned, it is a method of the base.

Perhaps you should explain your problem in a larger context, there might be an alternative path.

Share this post


Link to post
Share on other sites
I have almost sorted out the problem. I can use System.Reflection.MethodBase.GetCurrentMethod().DeclaringType but it does not quite work. The problem is that I have a base class called SortableObject and a class called Form. Form Inherits from SortableObject but the sorting methods and hence the MethodBase.GetCurrentMethod().DeclaringType call are in SortableObject. MethodBase.GetCurrentMethod().DeclaringType therefore returns SortableObject as the Type and not Form. Any ideas on how I get Form returned as the Type using reflection?

Share this post


Link to post
Share on other sites
As I said, you can't. The declaring type of SortDateCreated is the base type, it will never be the derived type. SortDateCreated is bound entirely to the base type. You will need an object instance or some other involvement from the derived type (such as a type switching parameter passed to the method, which is ugly) to do what you seem to want.

What's the larger scope of the problem?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this