public static MethodReference GetEmptyConstructor(Type type, ModuleDefinition currentModule, IList <Type> mscorlibArgumentTypes = null) { if (mscorlibArgumentTypes == null) { mscorlibArgumentTypes = new List <Type>(); } TypeReference typeRef = Utilities.GetCorlibTypeReference(type, currentModule); TypeDefinition attributeTypeDef = typeRef.Resolve(); if (attributeTypeDef != null) { foreach (MethodDefinition constr in attributeTypeDef.Methods) { if (constr.IsConstructor && ArgumentsMatch(constr.Parameters, mscorlibArgumentTypes)) { return(constr); } } throw new ArgumentOutOfRangeException(string.Format("Type {0} doesnt provide matching constructor.", type.FullName)); } else { MethodReference ctorRef = new MethodReference(".ctor", Utilities.GetCorlibTypeReference(typeof(void), currentModule), typeRef); ctorRef.Parameters.AddRange(GetMatchingArguments(mscorlibArgumentTypes, currentModule)); return(ctorRef); } }
private static void CreateAndAddCallingConventionFieldArgument(MethodDefinition method, CustomAttribute dllImportAttr) { /// this StdCall is the default calling conventon (according to http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.callingconvention.aspx) if (!method.PInvokeInfo.IsCallConvWinapi) { TypeReference callingConventionArgumentType = Utilities.GetCorlibTypeReference(typeof(System.Runtime.InteropServices.CallingConvention), method.DeclaringType.Module); /// set the integer value of the field, so that RenameEnumValues can pick it up and fix it accordinly. int callingConventionValue = 0; if (method.PInvokeInfo.IsCallConvFastcall) { callingConventionValue = 5; } else if (method.PInvokeInfo.IsCallConvThiscall) { callingConventionValue = 4; } else if (method.PInvokeInfo.IsCallConvStdCall) { callingConventionValue = 3; } else if (method.PInvokeInfo.IsCallConvCdecl) { callingConventionValue = 2; } else { //default case, shouldn't be reached callingConventionValue = 1; } CustomAttributeArgument callingConventionArgument = new CustomAttributeArgument(callingConventionArgumentType, callingConventionValue); CustomAttributeNamedArgument namedCallingConventionArgument = new CustomAttributeNamedArgument("CallingConvention", callingConventionArgument); dllImportAttr.Fields.Add(namedCallingConventionArgument); } }
private static IEnumerable <ParameterDefinition> GetMatchingArguments(IList <Type> mscorlibArgumentTypes, ModuleDefinition currentModule) { List <ParameterDefinition> result = new List <ParameterDefinition>(mscorlibArgumentTypes.Count); foreach (Type type in mscorlibArgumentTypes) { result.Add(new ParameterDefinition(Utilities.GetCorlibTypeReference(type, currentModule))); } return(result); }
public static CustomAttribute GetExportedTypeAttribute(ExportedType exportedType, ModuleDefinition module) { MethodReference ctor = Utilities.GetEmptyConstructor(typeof(System.Runtime.CompilerServices.TypeForwardedToAttribute), module, new[] { typeof(System.Type) }); CustomAttribute exportedTypeAttribute = new CustomAttribute(ctor); TypeReference systemType = Utilities.GetCorlibTypeReference(typeof(Type), module); TypeReference type = exportedType.CreateReference(); exportedTypeAttribute.ConstructorArguments.Add(new CustomAttributeArgument(systemType, type)); return(exportedTypeAttribute); }
private static void CreateAndAddCharSetFieldArgument(MethodDefinition method, CustomAttribute dllImportAttr) { System.Runtime.InteropServices.CharSet charSet = System.Runtime.InteropServices.CharSet.None; if (method.PInvokeInfo.IsCharSetAnsi) { charSet = System.Runtime.InteropServices.CharSet.Ansi; } if (method.PInvokeInfo.IsCharSetUnicode) { charSet = System.Runtime.InteropServices.CharSet.Unicode; } if (method.PInvokeInfo.IsCharSetAuto) { charSet = System.Runtime.InteropServices.CharSet.Auto; } TypeReference charSetArgumentType = Utilities.GetCorlibTypeReference(typeof(System.Runtime.InteropServices.CharSet), method.DeclaringType.Module); //Casted to int, so that it can be resolved to enum member by EnumValueToFieldCombinationMatcher.GetEnumFieldDefinitionByValue //invokd in WeiteAttributeNamedArgs CustomAttributeArgument charSetArgument = new CustomAttributeArgument(charSetArgumentType, (int)(charSet)); CustomAttributeNamedArgument namedCharSetArgument = new CustomAttributeNamedArgument("CharSet", charSetArgument); dllImportAttr.Fields.Add(namedCharSetArgument); }