private static void UpdateAttributesOnType( ICustomAttributeProvider type, StrongNameKeyPair snk ) { foreach( var attribute in type.CustomAttributes ) { var assemblyNameReferences = attribute.ConstructorArguments.Where( t => t.Type.FullName == TypeFullName ); foreach( var argument in assemblyNameReferences .Select( s => s.Value as TypeReference ) .Where( s => s != null ) .Select( s => s.Scope as AssemblyNameReference ) .Where( a => a != null ) .Where( a => !a.HasPublicKey ) .Where( a => a.PublicKeyToken == null || a.PublicKeyToken.Length == 0 ) ) { argument.PublicKey = snk.PublicKey; } } }
/// <summary> /// Copies the custom attributes, excluding any attributes from a patching assembly that haven't been declared /// and Patchwork attributes. Note that this can only be used after all types and methods have been declared. /// </summary> /// <param name="targetMember">The target member.</param> /// <param name="yourMember">Your member, the source of the attributes.</param> /// <param name="filter"></param> private void CopyCustomAttributes(ICustomAttributeProvider targetMember, ICustomAttributeProvider yourMember, Func<CustomAttribute, bool> filter = null) { filter = filter ?? (x => true); var filteredAttrs = from attr in yourMember.CustomAttributes let attrType = attr.AttributeType.Resolve() let attrAssembly = attrType.Module.Assembly let isFromPatchworkAttributes = attrType.Module.Assembly.FullName == typeof (PatchworkInfo).Assembly.FullName let isFromPatchworkOther = attrType.Module.Assembly.FullName == typeof (AssemblyPatcher).Assembly.FullName //attributes that are in Patchwork but not Patchwork.Attributes are never included where !isFromPatchworkOther //attributes from Patchwork.Attributes may be included depending on whether History is enabled. where EmbedHistory || !isFromPatchworkAttributes //attributes specifically designated NeverEmbed should not be embedded. where attrType.CustomAttributes.All( attrOnAttr => attrOnAttr.AttributeType.FullName != typeof (NeverEmbedAttribute).FullName) //attributes from a patching assembly are only included if they have the NewType attribute where !attrAssembly.IsPatchingAssembly() || attrType.Resolve().HasCustomAttribute<NewTypeAttribute>() //also, apply this custom filter: where filter(attr) select attr; foreach (var yourAttr in filteredAttrs) { var targetAttr = new CustomAttribute(FixMethodReference(yourAttr.Constructor)); targetAttr.ConstructorArguments.AddRange(yourAttr.ConstructorArguments.Select(CopyCustomAttributeArg)); var targetFieldArgs = from nArg in yourAttr.Fields let arg = CopyCustomAttributeArg(nArg.Argument) select new CustomAttributeNamedArgument(nArg.Name, arg); var targetPropArgs = from nArg in yourAttr.Properties let arg = CopyCustomAttributeArg(nArg.Argument) select new CustomAttributeNamedArgument(nArg.Name, arg); targetAttr.Fields.AddRange(targetFieldArgs); targetAttr.Properties.AddRange(targetPropArgs); targetMember.CustomAttributes.Add(targetAttr); } }
static void PopulateCustomAttributes(ICustomAttributeProvider type, CodeAttributeDeclarationCollection attributes) { PopulateCustomAttributes(type, attributes, ctr => ctr); }
/// <summary> /// Determines whether the custom attribute provider has the right custom attribute. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="memberDef">The member definition.</param> /// <returns></returns> public static bool HasCustomAttribute <T>(this ICustomAttributeProvider memberDef) { return(memberDef.GetCustomAttributes <T>().Any()); }
static void PopulateCustomAttributes(ICustomAttributeProvider type, CodeAttributeDeclarationCollection attributes, AttributeFilter attributeFilter) { PopulateCustomAttributes(type, attributes, ctr => ctr, attributeFilter); }
static void PopulateCustomAttributes(ICustomAttributeProvider type, CodeAttributeDeclarationCollection attributes, HashSet <string> excludeAttributes) { PopulateCustomAttributes(type, attributes, ctr => ctr, excludeAttributes); }
static IEnumerable <ExportFieldAttribute> GetExportFieldAttributes(Mono.Cecil.ICustomAttributeProvider p) { return(GetAttributes <ExportFieldAttribute> (p, a => ToExportFieldAttribute(a))); }
public static IEnumerable <CustomAttribute> GetAttributesOf <T>([NotNull] this ICustomAttributeProvider provider) where T : Attribute { return(provider.HasCustomAttributes ? provider.CustomAttributes.Where(IsAttributeOf) : Enumerable.Empty <CustomAttribute>());
public static void RemoveAttribute(this Mono.Cecil.ICustomAttributeProvider methodDefinition, string attributeTypeName) { methodDefinition.CustomAttributes.Where(x => x.Constructor.DeclaringType.Name == attributeTypeName) .ToList() .ForEach(x => methodDefinition.CustomAttributes.Remove(x)); }
internal static CustomAttribute GetCustomAttribute <T>(this ICustomAttributeProvider instance) where T : Attribute => instance.GetCustomAttributes <T>().FirstOrDefault();
internal static bool HasCustomAttribute <T>(this ICustomAttributeProvider instance) where T : Attribute { return(GetCustomAttribute <T>(instance) == null); }
public static IEnumerable <CustomAttribute> GetCustomAttributes <TAttr>(this Mono.Cecil.ICustomAttributeProvider cap) { return(cap.CustomAttributes.Where(x => x.AttributeType.Name == typeof(TAttr).Name).ToArray()); }
static bool IsAsyncMethod(ICustomAttributeProvider method) { return(method.CustomAttributes.Any(a => a.AttributeType.FullName == "System.Runtime.CompilerServices.AsyncStateMachineAttribute")); }
/// <summary> /// Determines whether the entity (or any base type) has the specified custom attribute /// assigned. /// </summary> /// <param name="source"></param> /// <param name="attrTypeFullName">The full attribute type name.</param> /// <param name="inherit">true to search this member's inheritance chain to find the attribute; otherwise, false.</param> /// <returns></returns> public static bool HasCustomAttribute(this Mono.Cecil.ICustomAttributeProvider source, string attrTypeFullName, bool inherit = false) { return(GetCustomAttribute(source, attrTypeFullName, inherit) != null); }
static bool IsAsyncMethod(ICustomAttributeProvider method) { return method.CustomAttributes.Any(a => a.AttributeType.FullName == "System.Runtime.CompilerServices.AsyncStateMachineAttribute"); }
private CustomAttribute AddAttribute(ICustomAttributeProvider interfacePropDef, Type attributeType) { var attr = Import(attributeType); var ctor = Import(attr.Resolve().Methods.OrderBy(x => x.Parameters.Count).First(x => x.IsConstructor)); var custAttr = new CustomAttribute(ctor); interfacePropDef.CustomAttributes.Add(custAttr); return custAttr; }
public static bool ContainsAttribute(this Mono.Cecil.ICustomAttributeProvider methodDefinition, string attributeTypeName) { return(methodDefinition.CustomAttributes.Any(x => x.Constructor.DeclaringType.Name == attributeTypeName)); }
static IEnumerable <RegisterAttribute> GetRegisterAttributes(Mono.Cecil.ICustomAttributeProvider p) { return(GetAttributes <RegisterAttribute> (p, a => ToRegisterAttribute(a))); }
public static bool TryGetAttribute(this ICustomAttributeProvider value, string attributeName, out CustomAttribute result) { result = value.CustomAttributes.SingleOrDefault(a => attributeName == a.AttributeType.Name); return(null != result); }
static IEnumerable <TAttribute> GetAttributes <TAttribute> (Mono.Cecil.ICustomAttributeProvider p, Func <CustomAttribute, TAttribute> selector) { return(p.GetCustomAttributes(typeof(TAttribute)) .Select(selector)); }
public static CustomAttribute GetNullGuardAttribute(this ICustomAttributeProvider value) { return(value.CustomAttributes.FirstOrDefault(a => a.AttributeType.Name == "NullGuardAttribute")); }
static bool IsExtensionMethod(ICustomAttributeProvider method) { return(method.CustomAttributes.Any(a => a.AttributeType.FullName == "System.Runtime.CompilerServices.ExtensionAttribute")); }
public static bool AllowsNull(this ICustomAttributeProvider value) { return (value.CustomAttributes.Any( a => a.AttributeType.Name == "AllowNullAttribute" || a.AttributeType.Name == "CanBeNullAttribute")); }
private void CopyCustomAttributesByImportAttribute(ICustomAttributeProvider targetMember, ICustomAttributeProvider yourMember, ImportCustomAttributesAttribute importAttribute) { if (importAttribute == null) return; var legalAttributes = importAttribute.AttributeTypes.Cast<TypeReference>().Select(x => x.Name); Func<CustomAttribute, bool> importOnly = attr => legalAttributes.Contains(attr.AttributeType.Name); CopyCustomAttributes(targetMember, yourMember, importOnly); }
public static bool ContainsAllowNullAttribute(this ICustomAttributeProvider definition) { var customAttributes = definition.CustomAttributes; return(customAttributes.Any(x => x.AttributeType.Name == "AllowNullAttribute")); }
public virtual bool IsImmutableType(ICustomAttributeProvider type) { string attrName = Translator.Bridge_ASSEMBLY + ".ImmutableAttribute"; return(this.HasAttribute(type.CustomAttributes, attrName)); }
public static bool IsCompilerGenerated(this ICustomAttributeProvider value) { return(value.CustomAttributes.Any(a => a.AttributeType.Name == "CompilerGeneratedAttribute")); }
static CodeAttributeDeclarationCollection CreateCustomAttributes(ICustomAttributeProvider type) { var attributes = new CodeAttributeDeclarationCollection(); PopulateCustomAttributes(type, attributes); return attributes; }
public static bool IsAsyncStateMachine(this ICustomAttributeProvider value) { return(value.CustomAttributes.Any(a => a.AttributeType.Name == "AsyncStateMachineAttribute")); }
static void PopulateCustomAttributes(ICustomAttributeProvider type, CodeAttributeDeclarationCollection attributes, Func<CodeTypeReference, CodeTypeReference> codeTypeModifier) { foreach (var customAttribute in type.CustomAttributes.Where(ShouldIncludeAttribute).OrderBy(a => a.AttributeType.FullName).ThenBy(a => ConvertAttrbuteToCode(codeTypeModifier, a))) { var attribute = GenerateCodeAttributeDeclaration(codeTypeModifier, customAttribute); attributes.Add(attribute); } }
public static bool IsIteratorStateMachine(this ICustomAttributeProvider value) { // Only works on VB not C# but it's something. return(value.CustomAttributes.Any(a => a.AttributeType.Name == "IteratorStateMachineAttribute")); }
static bool IsExtensionMethod(ICustomAttributeProvider method) { return method.CustomAttributes.Any(a => a.AttributeType.FullName == "System.Runtime.CompilerServices.ExtensionAttribute"); }
public void AddPreserveAttribute(ICustomAttributeProvider provider) { var preserveAttributeConstructor = typeof(UnityEngine.Scripting.PreserveAttribute).GetConstructor(Type.EmptyTypes); provider.CustomAttributes.Add(new CustomAttribute(Module.ImportReference(preserveAttributeConstructor))); }
/// <summary> /// Gets the custom attribute. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="provider">The provider.</param> /// <returns></returns> internal static T GetCustomAttribute <T>(this ICustomAttributeProvider provider) { return(provider.GetCustomAttributes <T>().FirstOrDefault()); }
public static bool IsCompilerGenerated(this Mono.Cecil.ICustomAttributeProvider thing) => thing?.CustomAttributes.Any(a => a.AttributeType.Name == nameof(CompilerGeneratedAttribute)) ?? false;