Stack trace for generic method - what was T at runtime?


  • Hi,

    I couldn't see this question already asked, but apologies if it has.

    I have a generic method  in my DAL Select<T>(int id), that selects an object based on an ID.

    Currently this method has its own error handler, and on exception, logs the error and returns null.

    While debugging an error recently I noted the stack trace is:
       at DirectDataAccessor.Select[T](Int32 id) in <path>DirectDataAccessor.cs:line 149
       at DataType.Select[T](Int32 id) in <path>\AbstractBaseDataType.cs:line 117

    Note that it shows Select<T>. What I was wondernig, is that as this occurs at runtime, can't the exception identify what the runtime type of T is for the particular call, and include this?

    Obviously knowing this type would be useful, but if there isn't built in support for this, I'll have to think about changing how I'm handling exceptions here.
    Tuesday, January 27, 2009 3:11 PM


  • > can't the exception identify what the runtime type of T is for the particular call

    No, it does not identify it.  Even Exception.TargetSite seems to reference the "generic method definition" (with the T) instead of the constructed generic method.

    Note that these stack traces do not include the values for ordinary arguments either, so this is not terribly different.

    On the other hand, the Visual Studio debugger will display argument values for the current call stack -- include the types bound to generic arguments like T.  This is an unmanaged debugger capability and AFAIK not something that you can do from within your code.

    Wednesday, January 28, 2009 1:13 AM