Friday, April 3, 2009

Problem in using Information.TypeName Property

Hi All,

Whenever we debug the code in Dot Net while using InterOP, it is very frequent to notice a proxy object rather than the actual InterOP object. When you try to get the properties of an InterOP Variable, it shows you some proxy object, and not the actual InterOP object. For example, if I declare a variable like following :

Dim xlApp as XL.Application (The excel Application object)
set xlApp = ME.Globals.ThisAddin.Application

Now, in the debug mode, after executing the above line, if I want to see what values are stored in xlApp variable, the dot net editor will show the variable as a Runtime.Proxy object most of the times. The reason is, that normally, the InterOP layer creates a wrapper object to avoid any problems running in non EN-US locales.

Due to this behavior, I faced one problem. I was not able to obtain the Type Name of application' selection object because the selection object was coming as a proxy wrapper. I tried something like this :

Dim selectionType as string

selectionType = Information.TypeName(xlApp.Selection)

The above line was giving a System.Reflection.TargetInvocationException. I had to change my project's setting to ensure that the InterOp variables are not proxy objects. For that, I went to MyProject folder in my solution, opened the AssemblyInfo.vb file, change the attribute < Assembly: ExcelLocale1033(True) > set to < Assembly: ExcelLocale1033(False) > and saved the file. And see the difference, it didn't give me a proxy object :)

See the following thread For the example:
MSDN VSTO Thread Link.

No comments: