public static IAutomationBridge SetUpEnvironment() { Type interopProviderType = typeof(AutomationInteropProvider); // Set the bridge assembly name to a value that will // fail when the static constructor attempts to load it. Environment.SetEnvironmentVariable("MONO_UIA_BRIDGE", "Ignore this intentional error"); // Reset element IDs to 0 for predictable results Type providerWrapperType = Type.GetType("Mono.UIAutomation.UiaDbusBridge.Wrappers.ProviderElementWrapper, UiaDbusBridge, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f4ceacb585d99812"); FieldInfo idCountField = providerWrapperType.GetField("idCount", BindingFlags.NonPublic | BindingFlags.Static); idCountField.SetValue(null, 0); // Inject a UiaDbusBridge that we have a reference to. Type bridgeType = Type.GetType("Mono.UIAutomation.UiaDbusBridge.AutomationBridge, UiaDbusBridge, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f4ceacb585d99812"); IAutomationBridge bridge = (IAutomationBridge)Activator.CreateInstance(bridgeType); FieldInfo bridgeField = interopProviderType.GetField("bridges", BindingFlags.NonPublic | BindingFlags.Static); List <IAutomationBridge> bridges = new List <IAutomationBridge> (); bridges.Add(bridge); bridgeField.SetValue(null, bridges); bridge.Initialize(); return(bridge); }
private static IAutomationBridge GetAutomationBridge(string bridgeAssemblyName) { Assembly bridgeAssembly = null; try { bridgeAssembly = Assembly.Load(bridgeAssemblyName); } catch (Exception e) { Console.WriteLine(string.Format("Error loading UIA bridge ({0}): {1}", bridgeAssemblyName, e)); return(null); } Type bridgeType = null; // Quickie inefficent implementation Type bridgeInterfaceType = typeof(IAutomationBridge); foreach (Type type in bridgeAssembly.GetTypes()) { if (bridgeInterfaceType.IsAssignableFrom(type)) { bridgeType = type; break; } } if (bridgeType == null) { Console.WriteLine("No UIA bridge found in assembly: " + bridgeAssemblyName); return(null); } try { IAutomationBridge bridge = (IAutomationBridge)Activator.CreateInstance(bridgeType); if (!bridge.IsAccessibilityEnabled) { return(null); } bridge.Initialize(); return(bridge); } catch (Exception e) { Console.WriteLine("Failed to load UIA bridge: " + e); return(null); } }