默认值
TLB importer
-或等效的Visual Studio UI操作-创建
Interop.UIAutomationClient
程序集使用“
[out, retval]
“签名布局,而不是使用
Preservesig
属性(此处详细介绍
http://blogs.msdn.com/b/adam_nathan/archive/2003/04/30/56646.aspx
).
例如,这里声明
IUIAutomationGridPattern
类似于(简化版):
[Guid("414C3CDC-856B-4F5B-8538-3131C6302550"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IUIAutomationGridPattern
{
UIAutomationClient.IUIAutomationElement GetItem(int row, int column);
...
}
而不是:
[Guid("414C3CDC-856B-4F5B-8538-3131C6302550")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IUIAutomationGridPattern
{
[PreserveSig]
int GetItem(int row, int column, out UIAutomationClient.IUIAutomationElement element);
...
}
虽然两者都有效,但如果您想仔细处理异常,后一种更好。第一个做了一些魔术,不幸的是把这里有趣的东西变成了不那么有趣的东西。
所以,如果您使用
PreserveSig
版本,您可以替换GridItem中的代码。cs类似:
public AutomationElement GetItem(int row, int column)
{
try
{
UIAutomationClient.IUIAutomationElement element;
int hr = _pattern.GetItem(row, column, out element);
if (hr != 0)
throw Marshal.GetExceptionForHR(hr); // note this uses COM's EXCEPINFO if any
return AutomationElement.Wrap(element).GetUpdatedCache(CacheRequest.Current);
}
catch (System.Runtime.InteropServices.COMException e)
{
Exception newEx; if (Utility.ConvertException(e, out newEx)) { throw newEx; } else { throw; }
}
}
现在您应该看到原始异常。
因此,要修复代码,您必须手动重新定义所有涉及的接口(或者在这里
http://clrinterop.codeplex.com/releases/view/17579
可以使用PreserveSig创建签名的更新的tlbimp-未测试)。您还必须更改UIAComWrapper代码。前面还有很多工作要做。