Exemplo n.º 1
0
 public int AddConversion<T> (Func<T, string> conversion)
 {
     int supportedTypeIndex = 0;
     lock (_mapSyncRoot)
     {
         if (_map.Any (c => c.SupportedType.Equals (typeof (T)))
         {
             // already exists! what do we do? meh, maybe return
             // existing index, maybe overwrite, maybe throw
             // exception? up to you :)
             // 
             // actually, you probably want to add a soft means of
             // interrogation or addition, like add a ConversionExists
             // method, or a TryAddConversion method, and throw
             // exception if exists
         }
         else
         {
             ConversionRule conversionRule = new ConversionRule 
             {
                 SupportedType = typeof (T),
                 Conversion = (s) => conversion (s),
             };
             _map.Add (conversionRule);
             supportedTypeIndex = _map.Count - 1;
         }
     }
     return supportedTypeIndex;
 }
        public IActionResult DeleteConversionRule([FromBody] ConversionRule conversionRule)
        {
            DBResponse dbResponse = ToolInventoryRepo.DeleteConversionRule(conversionRule);

            return(StatusCode(StatusCodes.Status200OK, new APIResponse
            {
                ResponseCode = 0,
                ResponseText = "Setup Sheet updated"
            }));
        }
Exemplo n.º 3
0
        public APIResponse DeleteConversionRule(ConversionRule conversionRule)
        {
            DBResponse dbResponse = ToolInventoryRepo.DeleteConversionRule(conversionRule);

            return(new APIResponse
            {
                ResponseCode = 0,
                ResponseText = "Setup Sheet updated"
            });
        }
Exemplo n.º 4
0
 public T Convert<T> (string stringValue, int supportedTypeIndex)
 {
     T value = default (T);
     ConversionRule conversionRule = null;
     lock (_mapSyncRoot)
     {
         if ((supportedTypeIndex > 0) && 
             (supportedTypeIndex < _map.Count))
         {
             if (_map[supportedTypeIndex].SupportedType == typeof (T))
             {
                 conversionRule = _map[supportedTypeIndex];
             }
             else 
             {
                 // oh nos! someone supplied a bad index! or type!
             }
         }
         else 
         {
             // arg, definitely a bad index
         }
     }
     object untypedValue = null;
     try
     {
         untypedValue = conversionRule.Conversion (stringValue);
     }
     // alright, catching all exceptions is bad, but how do you constrain
     // or set reasonable expectations on a user-submitted lambda expression?
     catch (Exception exception)
     {
         throw new ArgumentException (
             string.Format (
             "Cannot convert type [{0}] with value [{1}] to type [{2}]." + 
             " Converter threw exception.",
             stringValue.GetType (),
             stringValue,
             typeof (T)),
             exception);
     }
     // NOTE: proper use of `is` operator
     if (untypedValue is T)
     {
         value = (T)(untypedValue);
     }
     else
     {
         // i think you know what to do ;)
     }
     return value;
 }
        public MainWindowViewModel(GraphEditorViewModel graphViewModel)
        {
            this.graphViewModel = graphViewModel;

            InsertNodeCommand = new RelayCommand(InsertNode)
            {
                CanExecute = true
            };

            // Load in usable nodes
            string[]        dllFile    = Directory.GetFiles(App.PluginFolderPath, "*.dll");
            List <Assembly> assemblies = new List <Assembly>(dllFile.Length);

            foreach (string file in dllFile)
            {
                try
                {
                    Assembly assembly = Assembly.LoadFrom(file);
                    Type[]   types    = assembly.GetTypes();

                    // Look for NodeBase types
                    foreach (Type type in types)
                    {
                        if (type.IsSubclassOf(typeof(NodeBase)))
                        {
                            // Get unique name
                            LoadedNodes.Add(type);

                            // If this is a conversion node tell our graph about it
                            ConversionRule conversionRule = type.GetCustomAttribute(typeof(ConversionRule)) as ConversionRule;

                            if (conversionRule != null)
                            {
                                graphViewModel.AddConversionRule(conversionRule.InputType, conversionRule.OutputType, type);
                            }
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("Error loading nodes.\nPlease ensure there are libraries in the /NodePlugins folder and that they are unblocked.");
                }
            }
        }
Exemplo n.º 6
0
        protected virtual (PropertyInfo, ConversionRule) FindCorrespondingProperty(PropertyInfo fromProperty, IEnumerable <PropertyInfo> toProperties)
        {
            var            rtn            = toProperties.SingleOrDefault(x => x.Name == fromProperty.Name);
            ConversionRule conversionRule = null;

            if (rtn == null)
            {
                foreach (var rule in conversionRules)
                {
                    rtn = toProperties.SingleOrDefault(toProperty => rule.QualifierMethod(fromProperty, toProperty));
                    if (rtn != null)
                    {
                        conversionRule = rule;
                        break;
                    }
                }
            }
            return(rtn, conversionRule);
        }
 public void AddConversion <T> (Func <S, T> conversion)
 {
     lock (_syncRoot)
     {
         if (_map.Any(c => c.SupportedType.Equals(typeof(T))))
         {
             throw new ArgumentException(
                       string.Format(
                           "Conversion from [{0}] to [{1}] already exists. " +
                           "Cannot add new conversion.",
                           typeof(S),
                           typeof(T)));
         }
         ConversionRule conversionRule = new ConversionRule
         {
             SupportedType = typeof(T),
             Conversion    = (s) => conversion(s),
         };
         _map.Add(conversionRule);
     }
 }
    public Func <S, T> GetConversion <T> ()
    {
        Func <S, T> conversionMethod = null;

        lock (_syncRoot)
        {
            ConversionRule conversion = _map.
                                        SingleOrDefault(c => c.SupportedType.Equals(typeof(T)));
            if (conversion == null)
            {
                throw new NotSupportedException(
                          string.Format(
                              "Conversion from [{0}] to [{1}] is not supported. " +
                              "Cannot get conversion.",
                              typeof(S),
                              typeof(T)));
            }
            conversionMethod =
                (value) => ConvertWrap <T> (conversion.Conversion, value);
        }
        return(conversionMethod);
    }