Пример #1
0
        public MapPropertyAttribute(string name = null, MissingAction missingAction = MissingAction.Throw, object defaultValue = null, Type converterType = null)
        {
            Name          = name;
            MissingAction = missingAction;
            DefaultValue  = defaultValue;

            if (converterType != null && !IsValidConverterType(converterType))
            {
                throw new Exception("not a valid converter type");
            }
            ConverterType = converterType;
        }
Пример #2
0
        /// <summary>
        /// Loads an action from an XmlElement
        /// </summary>
        /// <param name="element">the xml to convert</param>
        /// <param name="variables">
        /// Program.Variables, passed to the Action.ctor
        /// </param>
        /// <param name="core"></param>
        /// <returns></returns>
        public static Action FromXml([NotNull] XPathNavigator element, ICentipedeCore core)
        {
            //This is probably broken somewhere.

            IEnumerable <object[]> ctorArgs = new List <object[]>
            {
                new object[] { core },
                new object[] { core.Variables, core },
                new object[] { "", core },
                new object[] { "", core.Variables, core },
            };
            Assembly asm;

            //if action is not a plugin:
            Action instance = null;

            if (element.LocalName.StartsWith(@"Centipede."))
            {
                asm = Assembly.GetEntryAssembly();
            }
            else
            {
                string location = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
                if (location != null)
                {
                    String asmPath = Path.Combine(location, Path.Combine(PluginFolder, element.SelectSingleNode(@"@Assembly").Value));

                    asm = File.Exists(asmPath) ? Assembly.LoadFile(asmPath) : null;
                }
                else
                {
                    throw new FileNotFoundException();
                }
            }

            if (asm != null)
            {
                Type actionType = asm.GetType(element.LocalName);

                if (actionType != null)
                {
                    var ctor = (from types in ctorArgs
                                select new
                    {
                        Ctor =
                            actionType.GetConstructor(types.Select(t => t.GetType())
                                                      .ToArray()),
                        Args = types
                    }).FirstOrDefault(o => o.Ctor != null);
                    if (ctor != null)
                    {
                        instance = (Action)ctor.Ctor.Invoke(ctor.Args);
                    }
                }
            }

            if (instance == null)
            {
                instance = new MissingAction(element.LocalName, core.Variables, core);
            }


            //if (instance == null)
            //{
            //    throw new ApplicationException();
            //}

            instance.PopulateMembersFromXml(element);

            return(instance);
        }