コード例 #1
0
        public static ClassFormatter FromClass <T>() where T : class
        {
            ClassFormatter      map        = new ClassFormatter();
            List <PropertyInfo> properties = typeof(T).GetProperties().ToList();

            foreach (PropertyInfo property in properties)
            {
                FormattingPropertyAttribute mapProperty = property.GetCustomAttribute <FormattingPropertyAttribute>();

                if (mapProperty != null)
                {
                    // property.Name.ToLower() => OriFormat.Jsonify(property.Name)
                    map.AddKey(property.Name, mapProperty.Name ?? property.Name.ToLower());
                }
            }
            return(map);
        }
コード例 #2
0
        public static string Format <T>(string frame, T obj) where T : class
        {
            ClassFormatter map = FromClass <T>();
            // get all properties in a class.
            List <PropertyInfo> properties = typeof(T).GetProperties().ToList();

            // check through all properties in the class specified.
            foreach (PropertyInfo property in properties)
            {
                // future task: find a way to replace only the singular replacement key, as opposed to replacing other
                // reference inserted when replacing.

                // use stringbuilder to separate the class into pieces?
                if (!string.IsNullOrWhiteSpace(map[property.Name]) && property.GetValue(obj) != null)
                {
                    // ignore array properties, as they must be manually handled.
                    //if (property.GetValue(obj).GetType().IsArray)
                    //continue;
                    frame = frame.Replace(map[property.Name], property.GetValue(obj).ToString());
                }
            }
            // return the final string in which specified values have been replaced.
            return(frame);
        }