示例#1
0
        public static Unit CreateUnit(string name)
        {
            var pure_unit_name = GetUnitName(name);

            if (!UnitFiles.ContainsKey(pure_unit_name))
            {
                return(null);
            }

            var files = UnitFiles[pure_unit_name];
            var ext   = Path.GetExtension(name);

            if (!UnitTypes.ContainsKey(ext))
            {
                return(null);
            }

            var type       = UnitTypes[ext];
            var descriptor = GetUnitDescriptor(pure_unit_name);
            var context    = new UnitInstantiationContext();

            context.Substitutions["p"] = pure_unit_name;
            context.Substitutions["P"] = StringEscaper.Unescape(pure_unit_name);
            context.Substitutions["f"] = "/" + StringEscaper.Unescape(pure_unit_name);
            context.Substitutions["H"] = Environment.MachineName;

            var unit_parameter = GetUnitParameter(name);

            if (string.IsNullOrEmpty(unit_parameter))
            {
                unit_parameter = descriptor.DefaultInstance;
            }

            if (!string.IsNullOrEmpty(unit_parameter))
            {
                context.Substitutions["i"] = unit_parameter;
                context.Substitutions["I"] = StringEscaper.Unescape(unit_parameter);
                context.Substitutions["f"] = "/" + StringEscaper.Unescape(unit_parameter);
            }

            descriptor.InstantiateDescriptor(context);
            return((Unit)Activator.CreateInstance(type, name, descriptor));
        }
示例#2
0
        internal void InstantiateDescriptor(UnitInstantiationContext ctx)
        {
            var self_properties           = this.GetType().GetProperties();
            var properties_with_attribute = self_properties.Where(prop => prop.GetCustomAttributes(typeof(UnitPropertyAttribute), true).Any());

            foreach (var property in properties_with_attribute)
            {
                switch (property.GetValue(this))
                {
                case string str:
                    property.SetValue(this, ctx.Substitute(str));
                    break;

                case List <string> list:
                    for (int i = 0; i < list.Count; i++)
                    {
                        list[i] = ctx.Substitute(list[i]);
                    }
                    break;
                }
            }
        }