예제 #1
0
        static public Type GetPropertyType(object obj, string propertyName)
        {
            PropertyInfo property   = null;
            object       value      = obj;
            Type         returnType = null;

            if (obj == null)
            {
                throw new ArgumentException("obj cannot be null");
            }

            using (LogGroup logGroup = LogGroup.Start("Retrieving property '" + propertyName + "' from type '" + obj.GetType(), LogLevel.Debug))
            {
                string[] parts = propertyName.Split('.');

                foreach (string part in parts)
                {
                    LogWriter.Debug("Stepping to property '" + part + "' of type '" + value.GetType().ToString() + "'.");

                    property = value.GetType().GetProperty(part);

                    if (property == null)
                    {
                        throw new ArgumentException("The property '" + part + "' wasn't found on the type '" + value.GetType().ToString() + "'.");
                    }

                    LogWriter.Debug("Property type: " + property.PropertyType.FullName);

                    value = property.GetValue(value, null);

                    returnType = property.PropertyType;

                    if (value == null)
                    {
                        LogWriter.Debug("[null]");
                    }
                }
            }

            if (property != null)
            {
                return(returnType);
            }
            //return Convert.ChangeType(value, property.PropertyType);
            //return Convert.ChangeType(property.GetValue(value, null), property.PropertyType);
            else
            {
                return(null);
            }
        }
예제 #2
0
        /// <summary>
        /// Starts a new log group.
        /// </summary>
        /// <param name="summary"></param>
        /// <param name="logLevel"></param>
        /// <param name="callingMethod"></param>
        /// <returns></returns>
        static public LogGroup Start(string summary, LogLevel logLevel, MethodBase callingMethod)
        {
            LogGroup newGroup = null;

            // Create the log group even if not enabled
            newGroup = new LogGroup(summary, callingMethod, logLevel);

            // Only set the parent if the new group is enabled
            if (callingMethod != null && new LogSupervisor().IsEnabled(callingMethod.DeclaringType.Name, logLevel))
            {
                newGroup.Parent = DiagnosticState.CurrentGroup;
            }

            newGroup.Start();

            return(newGroup);
        }
예제 #3
0
        static public void SetPropertyValue(object obj, string propertyName, object value)
        {
            PropertyInfo property = null;
            object       parent   = obj;

            using (LogGroup logGroup = LogGroup.Start("Setting value of property '" + propertyName + "' on type '" + obj.GetType() + "'.", LogLevel.Debug))
            {
                string[] parts = propertyName.Split('.');

                foreach (string part in parts)
                {
                    LogWriter.Debug("Stepping to property '" + part + "'.");
                    LogWriter.Debug("Value type: '" + (value == null ? "[null]" : value.GetType().ToString()) + "'.");

                    property = parent.GetType().GetProperty(part);

                    if (property.CanWrite)
                    {
                        if (property == null)
                        {
                            throw new ArgumentException("The property '" + part + "' wasn't found on the type '" + value.GetType().ToString() + "'.");
                        }

                        LogWriter.Debug("Property type: " + property.PropertyType.FullName);

                        // If it's the last part then set the value
                        if (Array.IndexOf(parts, part) == parts.Length - 1)
                        {
                            property.SetValue(parent, value, null);
                        }
                        // Otherwise step down a level
                        else
                        {
                            parent = property.GetValue(parent, null);
                        }
                    }
                }
            }
        }
예제 #4
0
 static public LogGroup StartGroup(string summary, LogLevel logLevel, MethodBase callingMethod)
 {
     return(LogGroup.Start(summary, logLevel, callingMethod));
 }
예제 #5
0
 static public LogGroup StartGroup(string summary, LogLevel logLevel)
 {
     return(LogGroup.Start(summary, logLevel));
 }
예제 #6
0
 static public LogGroup StartGroup(string summary)
 {
     return(LogGroup.Start(summary));
 }
예제 #7
0
        /// <summary>
        /// Starts a new log group.
        /// </summary>
        /// <param name="summary"></param>
        /// <param name="logLevel"></param>
        /// <param name="callingMethod"></param>
        /// <returns></returns>
        public static LogGroup Start(string summary, LogLevel logLevel, MethodBase callingMethod)
        {
            LogGroup newGroup = null;

            // Create the log group even if not enabled
            newGroup = new LogGroup(summary, callingMethod, logLevel);

            // Only set the parent if the new group is enabled
            if (callingMethod != null && new LogSupervisor().IsEnabled(callingMethod.DeclaringType.Name, logLevel))
            {
                newGroup.Parent = DiagnosticState.CurrentGroup;
            }

            newGroup.Start();

            return newGroup;
        }