/// <summary> /// Returns a strongly typed collection of specific sensor types. /// </summary> /// <typeparam name="T">The type of the sensors to retrieve.</typeparam> /// <returns>A strongly types list of sensors.</returns> public static SensorList <T> GetSensorsByTypeId <T>() where T : Sensor { object[] attrs = typeof(T).GetCustomAttributes(typeof(SensorDescriptionAttribute), true); if (attrs != null && attrs.Length > 0) { SensorDescriptionAttribute sda = attrs[0] as SensorDescriptionAttribute; ISensorCollection nativeSensorCollection = null; HResult hr = sensorManager.GetSensorsByType(sda.SensorTypeGuid, out nativeSensorCollection); if (hr == HResult.ElementNotFound) { throw new SensorPlatformException(LocalizedMessages.SensorsNotFound); } return(nativeSensorCollectionToSensorCollection <T>(nativeSensorCollection)); } return(new SensorList <T>()); }
public static SensorList <S> GetSensorsByTypeId <S>( ) where S : Sensor { object[] attrs = typeof(S).GetCustomAttributes(typeof(SensorDescriptionAttribute), true); if (attrs != null && attrs.Length > 0) { SensorDescriptionAttribute sda = attrs[0] as SensorDescriptionAttribute; ISensorCollection nativeSensorCollection = null; HRESULT hr = sensorManager.GetSensorsByType(sda.SensorTypeGuid, out nativeSensorCollection); if (hr == HRESULT.E_ELEMENTNOTFOUND) { throw new SensorPlatformException("No sensors found. Are any sensor drivers installed?"); } return(nativeSensorCollectionToSensorCollection <S>(nativeSensorCollection)); } else { return(new SensorList <S>()); } }
/// <summary> /// Interrogates assemblies currently loaded into the AppDomain for classes deriving from <see cref="Sensor"/>. /// Builds data structures which map those types to sensor type GUIDs. /// </summary> private static void BuildSensorTypeMap( ) { Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies( ); foreach (Assembly asm in loadedAssemblies) { try { Type[] exportedTypes = asm.GetExportedTypes( ); foreach (Type t in exportedTypes) { if (t.IsSubclassOf(typeof(Sensor)) && t.IsPublic && !t.IsAbstract && !t.IsGenericType) { object[] attrs = t.GetCustomAttributes(typeof(SensorDescriptionAttribute), true); if (attrs != null && attrs.Length > 0) { SensorDescriptionAttribute sda = (SensorDescriptionAttribute)attrs[0]; SensorTypeData stm = new SensorTypeData(t, sda); guidToSensorDescr.Add(sda.SensorTypeGuid, stm); sensorTypeToGuid.Add(t, sda.SensorTypeGuid); } } } } catch (System.NotSupportedException) { // GetExportedTypes can throw this if dynamic assemblies are loaded // via Reflection.Emit. } catch (System.IO.FileNotFoundException) { // GetExportedTypes can throw this if a loaded asembly is not in the // current directory or path. } } }
public SensorTypeData(Type sensorClassType, SensorDescriptionAttribute sda) { sensorType = sensorClassType; this.sda = sda; }
public void Construction(string sensorTypeGuid) { SensorDescriptionAttribute a = new SensorDescriptionAttribute(sensorTypeGuid); Assert.Equal<string>(sensorTypeGuid, a.SensorType); }