public void SetCustomAttribute(CustomAttributeBuilder customBuilder) { string attrname = customBuilder.Ctor.ReflectedType.FullName; if (attrname == "System.Runtime.InteropServices.InAttribute") { attrs |= ParameterAttributes.In; return; } else if (attrname == "System.Runtime.InteropServices.OutAttribute") { attrs |= ParameterAttributes.Out; return; } else if (attrname == "System.Runtime.InteropServices.OptionalAttribute") { attrs |= ParameterAttributes.Optional; return; } else if (attrname == "System.Runtime.InteropServices.MarshalAsAttribute") { attrs |= ParameterAttributes.HasFieldMarshal; marshal_info = CustomAttributeBuilder.get_umarshal(customBuilder, false); /* FIXME: check for errors */ return; } else if (attrname == "System.Runtime.InteropServices.DefaultParameterValueAttribute") { /* MS.NET doesn't handle this attribute but we handle it for consistency */ CustomAttributeBuilder.CustomAttributeInfo cinfo = CustomAttributeBuilder.decode_cattr(customBuilder); /* FIXME: check for type compatibility */ SetConstant(cinfo.ctorArgs [0]); return; } if (cattrs != null) { CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1]; cattrs.CopyTo(new_array, 0); new_array [cattrs.Length] = customBuilder; cattrs = new_array; } else { cattrs = new CustomAttributeBuilder [1]; cattrs [0] = customBuilder; } }
/// <summary>Set a custom attribute using a custom attribute builder.</summary> /// <param name="customBuilder">An instance of a helper class to define the custom attribute. </param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="con" /> is null. </exception> public void SetCustomAttribute(CustomAttributeBuilder customBuilder) { string fullName = customBuilder.Ctor.ReflectedType.FullName; if (fullName == "System.Runtime.InteropServices.InAttribute") { this.attrs |= ParameterAttributes.In; return; } if (fullName == "System.Runtime.InteropServices.OutAttribute") { this.attrs |= ParameterAttributes.Out; return; } if (fullName == "System.Runtime.InteropServices.OptionalAttribute") { this.attrs |= ParameterAttributes.Optional; return; } if (fullName == "System.Runtime.InteropServices.MarshalAsAttribute") { this.attrs |= ParameterAttributes.HasFieldMarshal; this.marshal_info = CustomAttributeBuilder.get_umarshal(customBuilder, false); return; } if (fullName == "System.Runtime.InteropServices.DefaultParameterValueAttribute") { this.SetConstant(CustomAttributeBuilder.decode_cattr(customBuilder).ctorArgs[0]); return; } if (this.cattrs != null) { CustomAttributeBuilder[] array = new CustomAttributeBuilder[this.cattrs.Length + 1]; this.cattrs.CopyTo(array, 0); array[this.cattrs.Length] = customBuilder; this.cattrs = array; } else { this.cattrs = new CustomAttributeBuilder[1]; this.cattrs[0] = customBuilder; } }
public void SetCustomAttribute(CustomAttributeBuilder customBuilder) { if (customBuilder == null) { throw new ArgumentNullException("customBuilder"); } switch (customBuilder.Ctor.ReflectedType.FullName) { case "System.Runtime.CompilerServices.MethodImplAttribute": byte[] data = customBuilder.Data; int impla; // the (stupid) ctor takes a short or an int ... impla = (int)data [2]; impla |= ((int)data [3]) << 8; iattrs |= (MethodImplAttributes)impla; return; case "System.Runtime.InteropServices.DllImportAttribute": CustomAttributeBuilder.CustomAttributeInfo attr = CustomAttributeBuilder.decode_cattr(customBuilder); bool preserveSig = true; /* * It would be easier to construct a DllImportAttribute from * the custom attribute builder, but the DllImportAttribute * does not contain all the information required here, ie. * - some parameters, like BestFitMapping has three values * ("on", "off", "missing"), but DllImportAttribute only * contains two (on/off). * - PreserveSig is true by default, while it is false by * default in DllImportAttribute. */ pi_dll = (string)attr.ctorArgs[0]; if (pi_dll == null || pi_dll.Length == 0) { throw new ArgumentException("DllName cannot be empty"); } native_cc = System.Runtime.InteropServices.CallingConvention.Winapi; for (int i = 0; i < attr.namedParamNames.Length; ++i) { string name = attr.namedParamNames [i]; object value = attr.namedParamValues [i]; if (name == "CallingConvention") { native_cc = (CallingConvention)value; } else if (name == "CharSet") { charset = (CharSet)value; } else if (name == "EntryPoint") { pi_entry = (string)value; } else if (name == "ExactSpelling") { ExactSpelling = (bool)value; } else if (name == "SetLastError") { SetLastError = (bool)value; } else if (name == "PreserveSig") { preserveSig = (bool)value; } else if (name == "BestFitMapping") { BestFitMapping = (bool)value; } else if (name == "ThrowOnUnmappableChar") { ThrowOnUnmappableChar = (bool)value; } } attrs |= MethodAttributes.PinvokeImpl; if (preserveSig) { iattrs |= MethodImplAttributes.PreserveSig; } return; case "System.Runtime.InteropServices.PreserveSigAttribute": iattrs |= MethodImplAttributes.PreserveSig; return; case "System.Runtime.CompilerServices.SpecialNameAttribute": attrs |= MethodAttributes.SpecialName; return; case "System.Security.SuppressUnmanagedCodeSecurityAttribute": attrs |= MethodAttributes.HasSecurity; break; } if (cattrs != null) { CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1]; cattrs.CopyTo(new_array, 0); new_array [cattrs.Length] = customBuilder; cattrs = new_array; } else { cattrs = new CustomAttributeBuilder [1]; cattrs [0] = customBuilder; } }