How can I force a cast from EnvDTE.Thread to System.Threading.Thread?
In a VS extension project, I am trying to create a mapping of the process threads, cast as both EnvDTE.Thread (to access the Freeze and Thaw methods), and System.Threading.Thread (to access the ManagedThreadId property).
It should ideally be as follow, but the cast will not compile, saying that it cannot cast from System.Threading.Thread to EnvDTE.Thread.
var threads = new Dictionary<EnvDTE.Thread, Sy开发者_开发技巧stem.Threading.Thread>();
foreach (System.Threading.Thread thread in this.dte.Debugger.CurrentProgram.Threads) {
threads.Add((EnvDTE.Thread)thread, thread);
}
How can I force the cast, knowing that it will not throw an exception (unless I am missing something here)?
Edit: it does throw an InvalidCastException.
have you tried casting back to an object first?
threads.Add((EnvDTE.Thread)(object)thread, thread);
The compilation error you're getting is a bit of a red herring.
Debugger.CurrentProgram.Threads
already returns a collection of EnvDTE.Thread
objects, so your foreach
will fail when attempting to cast them to System.Threading.Thread
since there is no relation between those classes beyond the name.
EnvDTE.Thread
has an ID
property. Would that do what you want without needing to convert to a System.Threading.Thread
?
精彩评论