Пример #1
0
        public static void Write(this IPropertyBag2 bag, IEnumerable <KeyValuePair <string, object> > properties)
        {
            if (bag == null)
            {
                throw new ArgumentNullException(nameof(bag));
            }

            if (properties == null)
            {
                return;
            }

            foreach (var kv in properties)
            {
                var i = GetIndex(bag, kv.Key);
                if (i < 0) // ?
                {
                    continue;
                }

                // read info
                var values = new object[1];
                var props  = new PROPBAG2[1];
                props[0].pstrName = kv.Key;
                bag.GetPropertyInfo(i, 1, props, out int _).ThrowOnError();
                var value = props[0].ChangeType(kv.Value);
                values[0] = value;
                bag.Write(1, props, values).ThrowOnError();
            }
        }
Пример #2
0
        public static void Write(this IPropertyBag2 propertyBag2, string propertyName, object?value)
        {
            PROPBAG2 propBag = new PROPBAG2();

            propBag.pstrName = Marshal.StringToCoTaskMemUni(propertyName);
            try
            {
                propertyBag2.Write(1, new[] { propBag }, new[] { value });
            }
            finally
            {
                Marshal.FreeCoTaskMem(propBag.pstrName);
            }
        }