/// <summary>
        /// Sets the value of the specified extension
        /// </summary>
        public static void Set <TTarget, TValue>(ref ExtensionSet <TTarget> set, Extension <TTarget, TValue> extension, TValue value) where TTarget : IExtendableMessage <TTarget>
        {
            IExtensionValue extensionValue;

            if (set == null)
            {
                extensionValue = extension.CreateValue();
                set            = new ExtensionSet <TTarget>();
                set.ValuesByNumber.Add(extension.FieldNumber, extensionValue);
            }
            else
            {
                if (!set.ValuesByNumber.TryGetValue(extension.FieldNumber, out extensionValue))
                {
                    extensionValue = extension.CreateValue();
                    set.ValuesByNumber.Add(extension.FieldNumber, extensionValue);
                }
            }

            ((ExtensionValue <TValue>)extensionValue).SetValue(value);
        }
Beispiel #2
0
        /// <summary>
        /// Sets the value of the specified extension. This will make a new instance of ExtensionSet if the set is null.
        /// </summary>
        public static void Set <TTarget, TValue>(ref ExtensionSet <TTarget> set, Extension <TTarget, TValue> extension, TValue value) where TTarget : IExtendableMessage <TTarget>
        {
            ProtoPreconditions.CheckNotNullUnconstrained(value, nameof(value));

            IExtensionValue extensionValue;

            if (set == null)
            {
                extensionValue = extension.CreateValue();
                set            = new ExtensionSet <TTarget>();
                set.ValuesByNumber.Add(extension.FieldNumber, extensionValue);
            }
            else
            {
                if (!set.ValuesByNumber.TryGetValue(extension.FieldNumber, out extensionValue))
                {
                    extensionValue = extension.CreateValue();
                    set.ValuesByNumber.Add(extension.FieldNumber, extensionValue);
                }
            }

            ((ExtensionValue <TValue>)extensionValue).SetValue(value);
        }