SetConstant() private method

private SetConstant ( object defaultValue ) : void
defaultValue object
return void
 private void Visit(IParameterDefinition parameterDefinition, ParameterBuilder parameterBuilder) {
   if (parameterDefinition.HasDefaultValue) {
     parameterDefinition.DefaultValue.Dispatch(this);
     parameterBuilder.SetConstant(this.value);
   }
   foreach (var customAttribute in parameterDefinition.Attributes) {
     var customAttributeBuilder = this.GetCustomAttributeBuilder(customAttribute);
     parameterBuilder.SetCustomAttribute(customAttributeBuilder);
   }
   if (parameterDefinition.IsMarshalledExplicitly)
     parameterBuilder.SetCustomAttribute(GetMarshalAsAttribute(parameterDefinition.MarshallingInformation));
 }
Esempio n. 2
0
        public static void CopyParameterAttributes(ParameterInfo from, ParameterBuilder to) {
            if (from.IsDefined(typeof(ParamArrayAttribute), false)) {
                to.SetCustomAttribute(new CustomAttributeBuilder(
                    typeof(ParamArrayAttribute).GetConstructor(Type.EmptyTypes), ArrayUtils.EmptyObjects)
                );
            } else if (from.IsDefined(typeof(ParamDictionaryAttribute), false)) {
                to.SetCustomAttribute(new CustomAttributeBuilder(
                    typeof(ParamDictionaryAttribute).GetConstructor(Type.EmptyTypes), ArrayUtils.EmptyObjects)
                );
            }

            if ((from.Attributes & ParameterAttributes.HasDefault) != 0) {
                to.SetConstant(from.DefaultValue);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Provide implementation for setting default value 
        /// </summary>
        private static void SetDefaultValueInternal(IntPtr ipVariant, Type type, ParameterBuilder paramBuilder, FieldBuilder fieldBuilder, short typeVt)
        {
            if (type == null) throw new ArgumentNullException(nameof(type));
            // Use the element type for normalization if the type is a ByRef
            while (type.IsByRef)
                type = type.GetElementType();

            short defaultValueVt = Marshal.ReadInt16(ipVariant);
            object defaultValue = GetNormalizedDefaultValueFromVariant(ipVariant, type, defaultValueVt, typeVt);

            if (type.IsEnum && (defaultValueVt == (short)VarEnum.VT_I4 || defaultValueVt == (short)VarEnum.VT_UI4))
            {
                // 1) I can't dynamically create the enum in a ReflectionOnlyContext
                // 2) In pre-4.0, SetConstant(1) won't work for enum types.
                // After talking with team members we decided that this is a minor functionality that
                // we can live without in pre-4.0
                // return;
            }

            // Currently ParameterBuilder.SetConstant doesn't emit the custom attributes so certain types of constants
            // so we need to do that by ourselves
            CustomAttributeBuilder builder = null;
            if (type == typeof(Decimal))
            {
                DECIMAL realDecimal;
                IntPtr pDecimal = IntPtr.Zero;

                // Unfortunate we cannot directly access the fields in the decimal struct and we need to rely on the fact
                // that the internal representation of decimal & DECIMAL are the same, which will most likely remain true in the future
                // because CLR internally does the same thing
                Debug.Assert(sizeof(decimal) == Marshal.SizeOf(typeof(DECIMAL)));

                try
                {
                    pDecimal = Marshal.AllocCoTaskMem(sizeof(decimal));

                    // We convert it to unmanaged then back to managed to avoid unsafe code, so that we won't crash immediately in partial trust
                    Marshal.StructureToPtr(defaultValue, pDecimal, false);
                    realDecimal = (DECIMAL)Marshal.PtrToStructure(pDecimal, typeof(DECIMAL));
                }
                finally
                {
                    if (pDecimal != IntPtr.Zero)
                        Marshal.FreeCoTaskMem(pDecimal);
                }

                builder = CustomAttributeHelper.GetBuilderForDecimalConstant(
                    realDecimal.scale,
                    realDecimal.sign,
                    realDecimal.hi32,
                    realDecimal.mid32,
                    realDecimal.low32);
            }
            else if (type == typeof(DateTime) && defaultValueVt == (short)VarEnum.VT_DATE)
            {
                builder = CustomAttributeHelper.GetBuilderForDateTimeConstant(((DateTime)defaultValue).Ticks);
            }
            else if (defaultValueVt == (short)VarEnum.VT_UNKNOWN)
            {
                // Currently ParameterBuilder.SetConstant doesn't emit the IUnknownConstantAttribute
                // for IUnknown so we need to do that by ourselves
                builder = CustomAttributeHelper.GetBuilderForIUnknownConstant();
            }
            else if (defaultValueVt == (short)VarEnum.VT_DISPATCH)
            {
                // Currently ParameterBuilder.SetConstant doesn't emit the IDispatchConstantAttribute
                // for IDispatch so we need to do that by ourselves
                builder = CustomAttributeHelper.GetBuilderForIDispatchConstant();
            }

            if (builder != null)
            {
                if (paramBuilder != null)
                    paramBuilder.SetCustomAttribute(builder);
                if (fieldBuilder != null)
                    fieldBuilder.SetCustomAttribute(builder);
            }
            else
            {
                try
                {
                    if (paramBuilder != null)
                        paramBuilder.SetConstant(defaultValue);
                    if (fieldBuilder != null)
                        fieldBuilder.SetConstant(defaultValue);
                }
                catch (Exception)
                {
                    // Debug.Assert(type.IsEnum, "We should avoid failing for non-Enum default values");
                }
            }
        }