Beispiel #1
0
        private static IAutomationSource GetAutomationSource(string sourceAssemblyName)
        {
            Assembly sourceAssembly = null;

            try {
                sourceAssembly = Assembly.Load(sourceAssemblyName);
            } catch (Exception e) {
                Log.Warn(string.Format("Error loading UIA source ({0}): {1}",
                                       sourceAssemblyName,
                                       e));
                return(null);
            }

            Type sourceType = null;

            // Quickie inefficent implementation
            Type sourceInterfaceType = typeof(IAutomationSource);

            foreach (Type type in sourceAssembly.GetTypes())
            {
                if (sourceInterfaceType.IsAssignableFrom(type))
                {
                    sourceType = type;
                    break;
                }
            }
            if (sourceType == null)
            {
                Log.Error("No UIA source found in assembly: " +
                          sourceAssemblyName);
                return(null);
            }

            try {
                IAutomationSource source
                    = (IAutomationSource)Activator.CreateInstance(sourceType);
                if (!source.IsAccessibilityEnabled)
                {
                    return(null);
                }

                source.Initialize();
                return(source);
            } catch (Exception e) {
                Log.Error("Failed to load UIA source: " + e);
                return(null);
            }
        }