[.net] Generics Problem
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?
Since SortDateCreated is a static member, it must be a member of some class, for example:
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
This doesn't appear to have anything to do with generics.
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.
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.
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.
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.
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?
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?
What's the larger scope of the problem?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement