Exemple #1
0
        public static object getSearchObjects(ManagementBaseObject manageObject, Type type)
        {
            var    instance = Activator.CreateInstance(type);
            string propName = String.Empty;

            foreach (var propInfo in instance.GetType().GetProperties())
            {
                propName = getWMIPropName(propInfo);

                if (!String.IsNullOrEmpty(propName))
                {
                    var propertyType = propInfo.PropertyType;
                    var targetType   = IsNullableType(propertyType) ? Nullable.GetUnderlyingType(propertyType) : propertyType;

                    var propValue = manageObject.Properties[propName].Value;

                    if (propValue == null)
                    {
                        propInfo.SetValue(obj: instance, value: null);
                    }
                    else if (targetType == typeof(DateTime) || targetType == typeof(DateTimeOffset) || targetType == typeof(TimeSpan))
                    {
                        WMIProps prop     = propInfo.GetCustomAttribute <WMIProps>(inherit: false);
                        var      dateTime = ManagementDateTimeConverter.ToDateTime(propValue.ToString());

                        if (targetType == typeof(DateTime))
                        {
                            propInfo.SetValue(obj: instance, value: dateTime);
                        }
                        else if (targetType == typeof(DateTimeOffset))
                        {
                            propInfo.SetValue(obj: instance, value: new DateTimeOffset(dateTime));
                        }
                        else if (targetType == typeof(TimeSpan))
                        {
                            propInfo.SetValue(obj: instance, value: dateTime.TimeOfDay);
                        }
                    }
                    else
                    {
                        propInfo.SetValue(obj: instance, value: Convert.ChangeType(value: propValue, conversionType: propInfo.PropertyType));
                    }
                }
            }

            return(instance);
        }
Exemple #2
0
        private static string getWMIPropName(PropertyInfo propInfo)
        {
            string propName = String.Empty;

            if (checkForIgnorePors(propInfo))
            {
                WMIProps prop = propInfo.GetCustomAttribute <WMIProps>(inherit: false);
                if (prop == null)
                {
                    propName = propInfo.Name;
                }
                else
                {
                    propName = prop.Name;
                }
            }

            return(propName);
        }