コード例 #1
0
        private SeedEntry ParseElement(XElement elem)
        {
            string typeName = elem.Attribute("Type").Value;

            Type type = Type.GetType(typeName);

            if (type == null)
            {
                return(null);
            }

            if (type.GetConstructor(Type.EmptyTypes) == null)
            {
                Logger.Instance.LogFormat(LogType.Error, this, Properties.Resources.SeedEntryTypeHasNoDefaultConstructor, type.FullName);
                return(null);
            }

            object instance = Activator.CreateInstance(type);

            foreach (XElement prop in elem.Elements("Set"))
            {
                string expression = prop.Attribute("Expression").Value;
                string valueRaw   = prop.Value;

                PropertyInfo property = null;
                object       target   = null;
                if (!ObjectExpressionTools.GetPropertyFromExpression(instance, expression, false, out property, out target))
                {
                    Logger.Instance.LogFormat(LogType.Warning, this, Properties.Resources.SeedEntryPropertyNotFound, expression, type.Name);
                    break;
                }

                object value = Convert.ChangeType(valueRaw, property.PropertyType);

                if (!ObjectExpressionTools.TrySetValueFromExpression(instance, expression, value))
                {
                    Logger.Instance.LogFormat(LogType.Warning, this, Properties.Resources.SeedEntrySetPropertyFailed, expression, type.Name);
                    break;
                }
            }

            SeedEntry entry = new SeedEntry();

            entry.Name            = elem.Attribute("Name").Value;
            entry.UseIFormattable = bool.Parse(elem.Attribute("UseIFormattable").Value);
            entry.EntryType       = type;
            entry.Instance        = instance;
            return(entry);
        }
コード例 #2
0
        private IEnumerable <SeedEntry> LoadDatabaseFile()
        {
            string path = Path.Combine(Utilities.GetWorkingDirectory(), ObjectTestDatabaseFileName);

            if (File.Exists(path))
            {
                XDocument doc = XDocument.Load(path);
                if (!doc.IsXmlValid(Properties.Resources.ObjectTestDatabaseSchema))
                {
                    Logger.Instance.LogFormat(LogType.Error, this, Properties.Resources.SeedEntryParseError);
                    yield break;
                }

                foreach (XElement elem in doc.Root.Elements("Seed"))
                {
                    SeedEntry entry = ParseElement(elem);
                    if (entry != null)
                    {
                        yield return(entry);
                    }
                }
            }
        }
コード例 #3
0
        private SeedEntry ParseElement(XElement elem)
        {
            string typeName = elem.Attribute("Type").Value;

            Type type = Type.GetType(typeName);
            if (type == null)
            {
                return null;
            }

            if (type.GetConstructor(Type.EmptyTypes) == null)
            {
                Logger.Instance.LogFormat(LogType.Error, this, Properties.Resources.SeedEntryTypeHasNoDefaultConstructor, type.FullName);
                return null;
            }

            object instance = Activator.CreateInstance(type);

            foreach (XElement prop in elem.Elements("Set"))
            {
                string expression = prop.Attribute("Expression").Value;
                string valueRaw = prop.Value;

                PropertyInfo property = null;
                object target = null;
                if (!ObjectExpressionTools.GetPropertyFromExpression(instance, expression, false, out property, out target))
                {
                    Logger.Instance.LogFormat(LogType.Warning, this, Properties.Resources.SeedEntryPropertyNotFound, expression, type.Name);
                    break;
                }

                object value = Convert.ChangeType(valueRaw, property.PropertyType);

                if (!ObjectExpressionTools.TrySetValueFromExpression(instance, expression, value))
                {
                    Logger.Instance.LogFormat(LogType.Warning, this, Properties.Resources.SeedEntrySetPropertyFailed, expression, type.Name);
                    break;
                }
            }

            SeedEntry entry = new SeedEntry();
            entry.Name = elem.Attribute("Name").Value;
            entry.UseIFormattable = bool.Parse(elem.Attribute("UseIFormattable").Value);
            entry.EntryType = type;
            entry.Instance = instance;
            return entry;
        }