Example #1
0
 private static void DynObjAddProperty <T>(DynamicObject dynamicObj, PropertyData prop, string description, string category, bool isReadOnly)
 {
     if (prop.Value == null)
     {
         dynamicObj.AddPropertyNull <T>(prop.Name, prop.Name, description, category, isReadOnly, null);
     }
     else
     if (prop.IsArray)
     {
         dynamicObj.AddProperty(prop.Name, (T[])prop.Value, description, category, isReadOnly);
     }
     else
     {
         dynamicObj.AddProperty(prop.Name, (T)prop.Value, description, category, isReadOnly);
     }
 }
        public void XUnitTestDynamicObject()
        {
            var car = new DynamicObject();

            // Create properties
            car.Instance.Make         = "G35";
            car.Instance.Year         = 2014;
            car.Instance.CurrentSpeed = 45;

            // Add property in a more dynamic manor.
            car.AddProperty("TopSpeed", 180);

            var output = JsonConvert.SerializeObject(car);

            // {"Instance":{"Make":"G35","Year":2014,"CurrentSpeed":45,"TopSpeed":180}}
            Assert.Equal("{\"Instance\":{\"Make\":\"G35\",\"Year\":2014,\"CurrentSpeed\":45,\"TopSpeed\":180}}", output);
        }
        public static void AddDynamicProperties(IEnumerable <PropertyGridAttribute> attributes, DynamicObject dynamicObject)
        {
            if (attributes == null || dynamicObject == null)
            {
                return;
            }

            foreach (PropertyGridAttribute pga in attributes)
            {
                if (string.IsNullOrWhiteSpace(pga.Name))
                {
                    continue;
                }

                DynamicObjectProperty prop = dynamicObject.AddProperty(pga.Name, pga.Type, null);
                prop.SetValue(dynamicObject, pga.Value);
            }
        }
        public static void AddDynamicProperties(IEnumerable<PropertyGridAttribute> attributes, DynamicObject dynamicObject)
        {
            if (attributes == null || dynamicObject == null)
                return;

            foreach (PropertyGridAttribute pga in attributes)
            {
                if (string.IsNullOrWhiteSpace(pga.Name))
                    continue;

                DynamicObjectProperty prop = dynamicObject.AddProperty(pga.Name, pga.Type, null);
                prop.SetValue(dynamicObject, pga.Value);
            }
        }
Example #5
0
        internal object CreateDynamicObject()
        {
            var dynObj = new DynamicObject();

            foreach (var prop in WmiObject.Properties)
            {
                // skip array of bytes
                if (prop.Type == CimType.UInt8 && prop.IsArray)
                {
                    continue;
                }

                var classProp = WmiClass.Properties[prop.Name];
                var info      = new QualifiersInfo(classProp.Qualifiers);
                if (info.IsCimKey)
                {
                    continue;
                }
                var category    = prop.Type.ToString();
                var description = info.Description;
                var isReadOnly  = info.IsReadOnly;
                switch (prop.Type)
                {
                //     A signed 16-bit integer. This value maps to the System.Int16 type.
                case CimType.SInt16:
                    DynObjAddProperty <Int16>(dynObj, prop, description, category, isReadOnly);
                    break;

                //     A signed 32-bit integer. This value maps to the System.Int32 type.
                case CimType.SInt32:
                    DynObjAddProperty <Int32>(dynObj, prop, description, category, isReadOnly);
                    break;

                //     A floating-point 32-bit number. This value maps to the System.Single type.
                case CimType.Real32:
                    DynObjAddProperty <Single>(dynObj, prop, description, category, isReadOnly);
                    break;

                //     A floating point 64-bit number. This value maps to the System.Double type.
                case CimType.Real64:
                    DynObjAddProperty <Double>(dynObj, prop, description, category, isReadOnly);
                    break;

                //     A string. This value maps to the System.String type.
                case CimType.String:
                    DynObjAddProperty <String>(dynObj, prop, description, category, isReadOnly);
                    break;

                //     A Boolean. This value maps to the System.Boolean type.
                case CimType.Boolean:
                    DynObjAddProperty <Boolean>(dynObj, prop, description, category, isReadOnly);
                    break;

                //     An embedded object. Note that embedded objects differ from references in
                //     that the embedded object does not have a path and its lifetime is identical
                //     to the lifetime of the containing object. This value maps to the System.Object
                //     type.
                case CimType.Object:
                    DynObjAddProperty <Object>(dynObj, prop, description, category, isReadOnly);
                    break;

                //     A signed 8-bit integer. This value maps to the System.SByte type.
                case CimType.SInt8:
                    DynObjAddProperty <SByte>(dynObj, prop, description, category, isReadOnly);
                    break;

                //     An unsigned 8-bit integer. This value maps to the System.Byte type.
                case CimType.UInt8:
                    DynObjAddProperty <Byte>(dynObj, prop, description, category, isReadOnly);
                    break;

                //     An unsigned 16-bit integer. This value maps to the System.UInt16 type.
                case CimType.UInt16:
                    DynObjAddProperty <UInt16>(dynObj, prop, description, category, isReadOnly);
                    break;

                //     An unsigned 32-bit integer. This value maps to the System.UInt32 type.
                case CimType.UInt32:
                    DynObjAddProperty <UInt32>(dynObj, prop, description, category, isReadOnly);
                    break;

                //     A signed 64-bit integer. This value maps to the System.Int64 type.
                case CimType.SInt64:
                    DynObjAddProperty <Int64>(dynObj, prop, description, category, isReadOnly);
                    break;

                //     An unsigned 64-bit integer. This value maps to the System.UInt64 type.
                case CimType.UInt64:
                    DynObjAddProperty <UInt64>(dynObj, prop, description, category, isReadOnly);
                    break;

                //     A date or time value, represented in a string in DMTF date/time format: yyyymmddHHMMSS.mmmmmmsUUU,
                //     where yyyymmdd is the date in year/month/day; HHMMSS is the time in hours/minutes/seconds;
                //     mmmmmm is the number of microseconds in 6 digits; and sUUU is a sign (+ or
                //     -) and a 3-digit UTC offset. This value maps to the System.DateTime type.
                case CimType.DateTime:
                    if (prop.Value == null)
                    {
                        dynObj.AddPropertyNull <DateTime>(prop.Name, prop.Name, description, category, isReadOnly, null);
                    }
                    else
                    {
                        dynObj.AddProperty(prop.Name, WmiHelper.ToDateTime(prop.Value.ToString()), description, category, isReadOnly);
                    }
                    break;

                //     A reference to another object. This is represented by a string containing
                //     the path to the referenced object. This value maps to the System.Int16 type.
                case CimType.Reference:
                    DynObjAddProperty <Int16>(dynObj, prop, description, category, isReadOnly);
                    break;

                //     A 16-bit character. This value maps to the System.Char type.
                case CimType.Char16:
                    DynObjAddProperty <Char>(dynObj, prop, description, category, isReadOnly);
                    break;

                default:
                    string value = prop.Value == null ? null : prop.Value.ToString();
                    dynObj.AddProperty(String.Format(CultureInfo.InvariantCulture, "{0} : {1}", prop.Name, prop.Type), value, description, "Unknown", isReadOnly);
                    break;
                }
            }
            return(dynObj);
        }