示例#1
0
 static void client_OnMeasurementDelta(object sender, Delta d)
 {
     Console.WriteLine("{0}: {1:" + d.MetaData.Format + "} {2}", d.Path, d.Value, d.MetaData.Units);
 }
        /// <summary>
        /// Writes out all measurements in a given object recursively.
        /// </summary>
        /// <param name="target"></param>
        private void WriteOutMeasurements(object target, string path)
        {
            PropertyInfo[] infos;
            Measurement measurement;
            Group group;

            infos = target.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);

            // For each propery on the target
            foreach (PropertyInfo info in infos)
            {
                if (null != (measurement = (Measurement)info.GetCustomAttribute(typeof(Measurement))))
                {
                    Delta d = new Delta();
                    d.Path = path + measurement.Name;
                    d.Value = info.GetValue(target);
                    d.Timestamp = (long)(DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0)).TotalMilliseconds;
                    d.MetaData = measurement;

                    if (null != OnMeasurementDelta)
                    {
                        OnMeasurementDelta(this, d);
                    }
                }

                if (null != (group = (Group)info.GetCustomAttribute(typeof(Group))))
                {
                    WriteOutMeasurements(info.GetValue(target), path + group.Name + "/");
                }
            }
        }