Reflection
-
Hi All, I've not used reflection before so this is all really new to me. I have an object that I want to build in some automating dynamic behaviour based on what 'called it'. What's the best way to get the Type name of the calling type in this objects constructor? Cheers,
Jammer Going where everyone here has gone before! :) My Blog
-
Hi All, I've not used reflection before so this is all really new to me. I have an object that I want to build in some automating dynamic behaviour based on what 'called it'. What's the best way to get the Type name of the calling type in this objects constructor? Cheers,
Jammer Going where everyone here has gone before! :) My Blog
You could pass it to the constructor for the new object and then use the System.Object.GetType() method to get it's type (of the object that constructed it) and do further processing from there. There's a bunch of different ways. And it depends on how many "calling" objects you're dealing with.
-
You could pass it to the constructor for the new object and then use the System.Object.GetType() method to get it's type (of the object that constructed it) and do further processing from there. There's a bunch of different ways. And it depends on how many "calling" objects you're dealing with.
-
Hi All, I've not used reflection before so this is all really new to me. I have an object that I want to build in some automating dynamic behaviour based on what 'called it'. What's the best way to get the Type name of the calling type in this objects constructor? Cheers,
Jammer Going where everyone here has gone before! :) My Blog
You can look at the collection of StackFrame objects returned by System.Diagnostics.StackTrace. The first frame should be your constructor, and the second the method that called it.
-
You can look at the collection of StackFrame objects returned by System.Diagnostics.StackTrace. The first frame should be your constructor, and the second the method that called it.
-
You can look at the collection of StackFrame objects returned by System.Diagnostics.StackTrace. The first frame should be your constructor, and the second the method that called it.
Unfortunately this does not work in all cases and is not guaranteed to work in future versions of the framework. See Mike Stall's blog [^]for more info. The question one should be asking is "why does the constructor need to know the declaring type of its calling method?"
-
Unfortunately this does not work in all cases and is not guaranteed to work in future versions of the framework. See Mike Stall's blog [^]for more info. The question one should be asking is "why does the constructor need to know the declaring type of its calling method?"
Thanks, I didn't know that. Ya, if the question is unavoidable, I suppose passing the result of this.GetType() to the constructor is nice and clear about what is going on.