static CodeExpression CreateExpressionFor (CustomAttributeArgument argument) { if (IsSystemType (argument.Type)) return new CodeTypeOfExpression (GetCodeTypeReference ((TypeReference) argument.Value)); return new CodePrimitiveExpression (argument.Value); }
static object GetValue(IProjectContent pc, IEntity member, CustomAttributeArgument argument) { if (argument.Value is TypeReference) return CreateType(pc, member, (TypeReference)argument.Value); else return argument.Value; }
private CustomAttributeArgument CopyCustomAttributeArg(CustomAttributeArgument yourArgument) { var type = FixTypeReference(yourArgument.Type); var value = yourArgument.Value; if (value is CustomAttributeArgument) { value = CopyCustomAttributeArg((CustomAttributeArgument) value); } else if (value is TypeReference) { value = FixTypeReference((TypeReference) value); } return new CustomAttributeArgument(type, value); }
public static void CopyCustomAttributeNamedArguments(Collection<Mono.Cecil.CustomAttributeNamedArgument> source, Collection<Mono.Cecil.CustomAttributeNamedArgument> target, ReferenceResolver resolver) { foreach (var namedArgument in source) { var argumentType = resolver.ReferenceType(namedArgument.Argument.Type); CustomAttributeArgument argument = new CustomAttributeArgument(argumentType, namedArgument.Argument.Value); target.Add(new Mono.Cecil.CustomAttributeNamedArgument(namedArgument.Name, argument)); } }
static bool TryGetPropertyArgument (ICustomAttribute attribute, string name, out CustomAttributeArgument argument) { foreach (var namedArg in attribute.Properties) { if (namedArg.Name == name) { argument = namedArg.Argument; return true; } } argument = default (CustomAttributeArgument); return false; }
void AddObsoleteAttribute(AttributeData attributeData, Collection<CustomAttribute> customAttributes) { var customAttribute = new CustomAttribute(ObsoleteConstructorReference); var message = ConvertToMessage(attributeData); var messageArgument = new CustomAttributeArgument(ModuleDefinition.TypeSystem.String, message); customAttribute.ConstructorArguments.Add(messageArgument); var isError = GetIsError(attributeData); var isErrorArgument = new CustomAttributeArgument(ModuleDefinition.TypeSystem.Boolean, isError); customAttribute.ConstructorArguments.Add(isErrorArgument); customAttributes.Add(customAttribute); }
public void AddAttribute(Type attributeType, Type[] ctorParamTypes, object[] paramArguments) { var internalAttributeCtor = attributeType.GetConstructor(ctorParamTypes); var internalAttributeCtorRef = TargetModule.Import(internalAttributeCtor); var internalAttribute = new CustomAttribute(internalAttributeCtorRef); for (int i = 0; i < ctorParamTypes.Count(); i++) { var paramType = internalAttributeCtorRef.Parameters[i].ParameterType; var internalAttributeArgument = new CustomAttributeArgument(paramType, paramArguments[i]); internalAttribute.ConstructorArguments.Add(internalAttributeArgument); } TargetModule.Assembly.CustomAttributes.Add(internalAttribute); }
protected void AddCustomAttribute(MethodDefinition methodDefinition, ModuleDefinition moduleWithAttributeType, CustomAttributeArgument ctorArgument) { ArgumentUtility.CheckNotNull ("methodDefinition", methodDefinition); ArgumentUtility.CheckNotNull ("moduleWithAttributeType", moduleWithAttributeType); var attributeCtor = MakeCtorAndReference ( methodDefinition.DeclaringType.Module, moduleWithAttributeType); if (attributeCtor.Parameters.Count != 1) throw new InvalidOperationException ("There is no custom attribute ctor available that takes one parameter!"); var customAttribute = new CustomAttribute (attributeCtor); customAttribute.ConstructorArguments.Add (ctorArgument); if (!methodDefinition.CustomAttributes.Any (att => att.Constructor.FullName == attributeCtor.FullName)) methodDefinition.CustomAttributes.Add (customAttribute); }
private IEnumerable<ProjectReference> GetCustomAttributeArgumentReferences( CustomAttributeArgument customAttributeArgument, AuditEntryParameters parameters) { TypeDefinition typeDefinition = customAttributeArgument.Value is TypeReference ? (customAttributeArgument.Value as TypeReference).Resolve() : customAttributeArgument.Type.Resolve(); if (typeDefinition != null && !parameters.IsTypeChecked(typeDefinition)) { ProjectReference projectReference = parameters.FindProjectReference(typeDefinition.Scope); if (projectReference != null) yield return projectReference; foreach(var pr in m_interfacesTypeWorker.Execute(typeDefinition, parameters)) yield return pr; foreach(var pr in m_classTypeHierarchyWorker.Execute(typeDefinition, parameters)) yield return pr; parameters.AddToCheckedTypes(typeDefinition); } }
protected CustomAttributeArgument FixCustomAttributeArgument(ModuleDefinition module, CustomAttributeArgument argument) { var value = argument.Value; if (value is TypeReference) value = module.Import(value as TypeReference); if (value is CustomAttributeArgument[]) { var arguments = value as CustomAttributeArgument[]; for (var i = 0; i < arguments.Length; i++) arguments[i] = FixCustomAttributeArgument(module, arguments[i]); } // Used for wrapped CustomAttributeArgument[] if (argument.Type.Module == null) argument.Type = module.TypeSystem.LookupType(argument.Type.Namespace, argument.Type.Name); return new CustomAttributeArgument(module.Import(argument.Type), value); }
private CustomAttributeArgument FixPlatformVersion(CustomAttributeArgument caa) { return new CustomAttributeArgument(FixPlatformVersion(caa.Type), caa.Value); }
private void WriteArrayValues(CustomAttributeArgument[] array) { for (int i = 0; i < array.Length; i++) { if (array[i].Value is bool) { WriteBooleanLiteral((bool)array[i].Value); } else if (array[i].Value is string) { WriteStringLiteral((string)array[i].Value); } else if (array[i].Value is CustomAttributeArgument[]) { WriteArrayValues(array[i].Value as CustomAttributeArgument[]); } else { WriteLiteral(array[i].Value.ToString()); } if (i < array.Length - 1) { WriteSpace(); } } }
void MarkIfType (CustomAttributeArgument argument) { var at = argument.Type; if (at.IsArray) { var et = at.GetElementType (); if (et.Namespace != "System" || et.Name != "Type") return; MarkType (et); if (argument.Value == null) return; foreach (var cac in (CustomAttributeArgument[]) argument.Value) MarkWithResolvedScope ((TypeReference) cac.Value); } else if (at.Namespace == "System" && at.Name == "Type") { MarkType (argument.Type); MarkWithResolvedScope ((TypeReference) argument.Value); } }
public IConstantValue ReadConstantValue(CustomAttributeArgument arg) { object value = arg.Value; if (value is CustomAttributeArgument) { // Cecil uses this representation for boxed values arg = (CustomAttributeArgument)value; value = arg.Value; } ITypeReference type = ReadTypeReference(arg.Type); CustomAttributeArgument[] array = value as CustomAttributeArgument[]; if (array != null) { // TODO: write unit test for this // TODO: are multi-dimensional arrays possible as well? throw new NotImplementedException(); } TypeReference valueType = value as TypeReference; if (valueType != null) value = ReadTypeReference(valueType); return new SimpleConstantValue(type, value); }
public static string ToString(CustomAttributeArgument argument) { return argument.Value.ToString(); }
void MarkIfType (CustomAttributeArgument argument) { if (argument.Type.FullName != "System.Type") return; MarkType (argument.Type); MarkType ((TypeReference) argument.Value); }
void addCustomAttributeArgument(CustomAttributeArgument arg) { pushMember(arg.Type); }
void AnalyzeCustomAttributeArgs(CustomAttributeArgument arg) { if (arg.Value is TypeReference) { TypeReference typeRef = arg.Value as TypeReference; bool has = false; foreach (var i in ivtMap) if (i.Key.Name.Name == typeRef.Scope.Name) { has = true; break; } if (has) { IAnnotationProvider type = (arg.Value as TypeReference).Resolve(); if (type.Annotations[RenRef] != null) (type.Annotations[RenRef] as List<IReference>).Add(new CustomAttributeTypeReference(arg.Value as TypeReference)); } } else if (arg.Value is CustomAttributeArgument[]) foreach (var i in arg.Value as CustomAttributeArgument[]) AnalyzeCustomAttributeArgs(i); }
private static void UpdateAssembly( HashSet<IAssemblyInfo> modified, IAssemblyInfo assemblyInfo ) { foreach( var info in GetInternalVisibleToAttributes( assemblyInfo ).ToArray() ) { if( info.ConstructorArguments.Count != ExpectedArgumentsNumber ) { Log.WarnFormat( "Constructor of InternalVisibleToAttribute has not expected number of arguments. Actual: {0}, expected: {1}.", info.ConstructorArguments.Count, ExpectedArgumentsNumber ); continue; } var argument = GetConstructorArgument( assemblyInfo, info ); if( argument == null ) { continue; } var originalAssemblyName = ( string )argument.Value.Value; var signedAssembly = GetMatchingSignedAssembly( modified, originalAssemblyName ); if( signedAssembly == null ) { Log.WarnFormat( "InternalVisibleToAttribute for assembly '{0}' will be removed.", originalAssemblyName ); assemblyInfo.Assembly.CustomAttributes.Remove( info ); continue; } var assemblyName = string.Format( "{0}, PublicKey={1}", signedAssembly.Assembly.Name.Name, GetPublicKeyToken( signedAssembly ) ); var newArgument = new CustomAttributeArgument( argument.Value.Type, assemblyName ); info.ConstructorArguments.Clear(); info.ConstructorArguments.Add( newArgument ); } }
public void SwitchFramework() { // _asm.MainModule.RuntimeVersion = ""; for (var x = 0; x < _asm.CustomAttributes.Count; x++) { if (_asm.CustomAttributes[x].AttributeType.Name == "TargetFrameworkAttribute") { _asm.CustomAttributes[x].ConstructorArguments[0] = new CustomAttributeArgument(_asm.MainModule.Import(typeof(String)), ".NETFramework,Version=v4.5"); var cs = new CustomAttributeArgument(_asm.MainModule.Import(typeof(String)), ".NET Framework 4.5"); _asm.CustomAttributes[x].Properties[0] = new CustomAttributeNamedArgument("FrameworkDisplayName", cs); } } }
private CustomAttributeArgument Fix(CustomAttributeArgument arg, IGenericParameterProvider context) { CustomAttributeArgument ret = new CustomAttributeArgument(Fix(arg.Type, context), FixCustomAttributeValue(arg.Value, context)); return ret; }
private static Expression ConvertArgumentValue(CustomAttributeArgument parameter) { var type = parameter.Type.Resolve(); if (type != null && type.IsEnum) { return MakePrimitive(Convert.ToInt64(parameter.Value), type); } else if (parameter.Value is TypeReference) { return new TypeOfExpression() { Type = ConvertType((TypeReference)parameter.Value), }; } else { return new PrimitiveExpression(parameter.Value); } }
public static string ToString(CustomAttributeArgument[] arguments) { var result = new StringBuilder(); if (arguments != null) { for (var i = 0; i < arguments.Length; i++) { if (i > 0) { result.Append(ItemSeparator); result.Append(" "); } result.Append(arguments[i].Value); } } return result.ToString(); }
private CustomAttributeArgument Fix(CustomAttributeArgument arg) { CustomAttributeArgument ret = new CustomAttributeArgument(Fix(arg.Type), FixCustomAttributeValue(arg.Value)); return ret; }
internal bool AreSame(CustomAttributeArgument a, CustomAttributeArgument b) { if (!AreSame(a.Type, b.Type)) return false; if (a.Value == b.Value) return true; if (a.Value == null) return false; if (!a.Value.Equals(b.Value)) return false; return true; }
private static CodeExpression CreateInitialiserExpression(CustomAttributeArgument attributeArgument) { if (attributeArgument.Value is CustomAttributeArgument) { return CreateInitialiserExpression((CustomAttributeArgument) attributeArgument.Value); } if (attributeArgument.Value is CustomAttributeArgument[]) { var initialisers = from argument in (CustomAttributeArgument[]) attributeArgument.Value select CreateInitialiserExpression(argument); return new CodeArrayCreateExpression(CreateCodeTypeReference(attributeArgument.Type), initialisers.ToArray()); } var type = attributeArgument.Type.Resolve(); var value = attributeArgument.Value; if (type.BaseType != null && type.BaseType.FullName == "System.Enum") { var originalValue = Convert.ToInt64(value); if (type.CustomAttributes.Any(a => a.AttributeType.FullName == "System.FlagsAttribute")) { //var allFlags = from f in type.Fields // where f.Constant != null // let v = Convert.ToInt64(f.Constant) // where v == 0 || (originalValue & v) != 0 // select (CodeExpression)new CodeFieldReferenceExpression(typeExpression, f.Name); //return allFlags.Aggregate((current, next) => new CodeBinaryOperatorExpression(current, CodeBinaryOperatorType.BitwiseOr, next)); // I'd rather use the above, as it's just using the CodeDOM, but it puts // brackets around each CodeBinaryOperatorExpression var flags = from f in type.Fields where f.Constant != null let v = Convert.ToInt64(f.Constant) where v == 0 || (originalValue & v) != 0 select type.FullName + "." + f.Name; return new CodeSnippetExpression(flags.Aggregate((current, next) => current + " | " + next)); } var allFlags = from f in type.Fields where f.Constant != null let v = Convert.ToInt64(f.Constant) where v == originalValue select new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(CreateCodeTypeReference(type)), f.Name); return allFlags.FirstOrDefault(); } if (type.FullName == "System.Type" && value is TypeReference) { return new CodeTypeOfExpression(CreateCodeTypeReference((TypeReference)value)); } if (value is string) { // CodeDOM outputs a verbatim string. Any string with \n is treated as such, so normalise // it to make it easier for comparisons value = Regex.Replace((string)value, @"\n", "\\n"); value = Regex.Replace((string)value, @"\r\n|\r\\n", "\\r\\n"); } return new CodePrimitiveExpression(value); }
/// <summary> /// CopyMethod copies a method definition into a new copy which can be added /// to another module. /// </summary> /// <param name="method">The MethodDefinition to copy</param> /// <returns>A copy of the MethodDefinition</returns> public static MethodDefinition CopyMethod(MethodDefinition method) { MethodDefinition newMethod = new MethodDefinition(method.Name, method.Attributes, method.ReturnType); foreach (ParameterDefinition param in method.Parameters) { ParameterDefinition newParam = new ParameterDefinition(param.Name, param.Attributes, param.ParameterType); newMethod.Parameters.Add(newParam); } foreach (CustomAttribute attr in method.CustomAttributes) { CustomAttribute newAttr = new CustomAttribute(attr.Constructor); foreach (CustomAttributeArgument arg in attr.ConstructorArguments) { CustomAttributeArgument newArg = new CustomAttributeArgument(arg.Type, arg.Value); newAttr.ConstructorArguments.Add(newArg); } foreach (CustomAttributeNamedArgument arg in attr.Fields) { CustomAttributeNamedArgument newArg = new CustomAttributeNamedArgument(arg.Name, new CustomAttributeArgument(arg.Argument.Type, arg.Argument.Value)); newAttr.Fields.Add(newArg); } newMethod.CustomAttributes.Add(newAttr); } if (method.Body != null) { if (newMethod.Body == null) newMethod.Body = new MethodBody(newMethod); foreach (Instruction inst in method.Body.Instructions) { newMethod.Body.Instructions.Add(inst); } foreach (VariableDefinition var in method.Body.Variables) { VariableDefinition newVar = new VariableDefinition(var.Name, var.VariableType); newMethod.Body.Variables.Add(newVar); } foreach (ExceptionHandler handler in method.Body.ExceptionHandlers) { ExceptionHandler newHandler = new ExceptionHandler(handler.HandlerType); newHandler.HandlerStart = handler.HandlerStart; newHandler.HandlerEnd = handler.HandlerEnd; newHandler.TryStart = handler.TryStart; newHandler.TryEnd = handler.TryEnd; newHandler.FilterStart = handler.FilterStart; newHandler.CatchType = handler.CatchType; newMethod.Body.ExceptionHandlers.Add(newHandler); } newMethod.Body.InitLocals = method.Body.InitLocals; } return newMethod; }
public JSExpression TranslateAttributeConstructorArgument ( TypeSystem typeSystem, TypeReference context, CustomAttributeArgument ca ) { if (ca.Value == null) { return JSLiteral.Null(ca.Type); } else if (ca.Value is CustomAttributeArgument) { // :| return TranslateAttributeConstructorArgument( typeSystem, context, (CustomAttributeArgument)ca.Value ); } else if (ca.Value is CustomAttributeArgument[]) { // Issue #141. WTF. var valueArray = (CustomAttributeArgument[])ca.Value; return new JSArrayExpression(typeSystem.Object, (from value in valueArray select TranslateAttributeConstructorArgument( typeSystem, context, value )).ToArray() ); } else if (ca.Type.FullName == "System.Type") { return new JSTypeOfExpression((TypeReference)ca.Value); } else if (TypeUtil.IsEnum(ca.Type)) { var longValue = Convert.ToInt64(ca.Value); var result = JSEnumLiteral.TryCreate( TypeInfo.GetExisting(ca.Type), longValue ); if (result != null) return result; else return JSLiteral.New(longValue); } else { try { return JSLiteral.New(ca.Value as dynamic); } catch (Exception) { throw new NotImplementedException(String.Format("Attribute arguments of type '{0}' are not implemented.", ca.Type.FullName)); } } }
public CustomAttributeNamedArgument (string name, CustomAttributeArgument argument) { Mixin.CheckName (name); this.name = name; this.argument = argument; }
private void DispatchCustomAttributeArgument(CustomAttributeArgument argument, string referencingEntityName) { var reference = argument.Value as TypeReference; if (reference == null) return; Visit(reference, referencingEntityName); }