public static Type GetDigitalGaugeLayerPresentationType( this DigitalGaugeLayerPresentationKind digitalGaugeLayerPresentationKind) => digitalGaugeLayerPresentationKind .GetType() .GetField(digitalGaugeLayerPresentationKind.ToString()) .GetCustomAttributes(typeof(DigitalGaugeLayerPresentationClassAttribute), false) .Cast <DigitalGaugeLayerPresentationClassAttribute>() .FirstOrDefault()?.InstanceType;
private static DigitalGaugeLayerPresentation Convert( DigitalGaugeLayerPresentationKind digitalGaugeLayerPresentationKind) { if (digitalGaugeLayerPresentationKind == DigitalGaugeLayerPresentationKind.None) { throw new ArgumentException( "Cannot create abstract type", nameof(digitalGaugeLayerPresentationKind)); } var layerPresentationType = digitalGaugeLayerPresentationKind.GetDigitalGaugeLayerPresentationType(); var defaultCtorExists = layerPresentationType.GetConstructors() .FirstOrDefault(ctor => ctor.GetParameters().Length == 0); if (defaultCtorExists == null) { throw new ArgumentException( "Default constructor doesn't exixts", nameof(digitalGaugeLayerPresentationKind)); } return((DigitalGaugeLayerPresentation)Activator.CreateInstance(layerPresentationType)); }