コード例 #1
0
        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;
                }
            }
        }
コード例 #2
0
ファイル: ModifyExisting.cs プロジェクト: GregRos/Patchwork
        /// <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);
            }
        }
コード例 #3
0
 static void PopulateCustomAttributes(ICustomAttributeProvider type,
     CodeAttributeDeclarationCollection attributes)
 {
     PopulateCustomAttributes(type, attributes, ctr => ctr);
 }
コード例 #4
0
 /// <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());
 }
コード例 #5
0
ファイル: ApiGenerator.cs プロジェクト: jnm2/ApiApprover
 static void PopulateCustomAttributes(ICustomAttributeProvider type,
                                      CodeAttributeDeclarationCollection attributes,
                                      AttributeFilter attributeFilter)
 {
     PopulateCustomAttributes(type, attributes, ctr => ctr, attributeFilter);
 }
コード例 #6
0
 static void PopulateCustomAttributes(ICustomAttributeProvider type,
                                      CodeAttributeDeclarationCollection attributes,
                                      HashSet <string> excludeAttributes)
 {
     PopulateCustomAttributes(type, attributes, ctr => ctr, excludeAttributes);
 }
コード例 #7
0
 static IEnumerable <ExportFieldAttribute> GetExportFieldAttributes(Mono.Cecil.ICustomAttributeProvider p)
 {
     return(GetAttributes <ExportFieldAttribute> (p, a => ToExportFieldAttribute(a)));
 }
コード例 #8
0
ファイル: CecilExtension.cs プロジェクト: quabug/EntitiesBT
 public static IEnumerable <CustomAttribute> GetAttributesOf <T>([NotNull] this ICustomAttributeProvider provider) where T : Attribute
 {
     return(provider.HasCustomAttributes ?
            provider.CustomAttributes.Where(IsAttributeOf) :
            Enumerable.Empty <CustomAttribute>());
コード例 #9
0
ファイル: CecilHelper.cs プロジェクト: mdabbagh88/MethodCache
 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));
 }
コード例 #10
0
 internal static CustomAttribute GetCustomAttribute <T>(this ICustomAttributeProvider instance)
     where T : Attribute => instance.GetCustomAttributes <T>().FirstOrDefault();
コード例 #11
0
 internal static bool HasCustomAttribute <T>(this ICustomAttributeProvider instance)
     where T : Attribute
 {
     return(GetCustomAttribute <T>(instance) == null);
 }
コード例 #12
0
 public static IEnumerable <CustomAttribute> GetCustomAttributes <TAttr>(this Mono.Cecil.ICustomAttributeProvider cap)
 {
     return(cap.CustomAttributes.Where(x => x.AttributeType.Name == typeof(TAttr).Name).ToArray());
 }
コード例 #13
0
 static bool IsAsyncMethod(ICustomAttributeProvider method)
 {
     return(method.CustomAttributes.Any(a => a.AttributeType.FullName == "System.Runtime.CompilerServices.AsyncStateMachineAttribute"));
 }
コード例 #14
0
 /// <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);
 }
コード例 #15
0
 static bool IsAsyncMethod(ICustomAttributeProvider method)
 {
     return method.CustomAttributes.Any(a => a.AttributeType.FullName == "System.Runtime.CompilerServices.AsyncStateMachineAttribute");
 }
コード例 #16
0
        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;
        }
コード例 #17
0
ファイル: CecilHelper.cs プロジェクト: mdabbagh88/MethodCache
 public static bool ContainsAttribute(this Mono.Cecil.ICustomAttributeProvider methodDefinition,
                                      string attributeTypeName)
 {
     return(methodDefinition.CustomAttributes.Any(x => x.Constructor.DeclaringType.Name == attributeTypeName));
 }
コード例 #18
0
 static IEnumerable <RegisterAttribute> GetRegisterAttributes(Mono.Cecil.ICustomAttributeProvider p)
 {
     return(GetAttributes <RegisterAttribute> (p, a => ToRegisterAttribute(a)));
 }
コード例 #19
0
 public static bool TryGetAttribute(this ICustomAttributeProvider value, string attributeName,
                                    out CustomAttribute result)
 {
     result = value.CustomAttributes.SingleOrDefault(a => attributeName == a.AttributeType.Name);
     return(null != result);
 }
コード例 #20
0
 static IEnumerable <TAttribute> GetAttributes <TAttribute> (Mono.Cecil.ICustomAttributeProvider p, Func <CustomAttribute, TAttribute> selector)
 {
     return(p.GetCustomAttributes(typeof(TAttribute))
            .Select(selector));
 }
コード例 #21
0
 public static CustomAttribute GetNullGuardAttribute(this ICustomAttributeProvider value)
 {
     return(value.CustomAttributes.FirstOrDefault(a => a.AttributeType.Name == "NullGuardAttribute"));
 }
コード例 #22
0
 static bool IsExtensionMethod(ICustomAttributeProvider method)
 {
     return(method.CustomAttributes.Any(a => a.AttributeType.FullName == "System.Runtime.CompilerServices.ExtensionAttribute"));
 }
コード例 #23
0
 public static bool AllowsNull(this ICustomAttributeProvider value)
 {
     return
         (value.CustomAttributes.Any(
              a => a.AttributeType.Name == "AllowNullAttribute" || a.AttributeType.Name == "CanBeNullAttribute"));
 }
コード例 #24
0
ファイル: ModifyExisting.cs プロジェクト: GregRos/Patchwork
        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);
        }
コード例 #25
0
        public static bool ContainsAllowNullAttribute(this ICustomAttributeProvider definition)
        {
            var customAttributes = definition.CustomAttributes;

            return(customAttributes.Any(x => x.AttributeType.Name == "AllowNullAttribute"));
        }
コード例 #26
0
        public virtual bool IsImmutableType(ICustomAttributeProvider type)
        {
            string attrName = Translator.Bridge_ASSEMBLY + ".ImmutableAttribute";

            return(this.HasAttribute(type.CustomAttributes, attrName));
        }
コード例 #27
0
 public static bool IsCompilerGenerated(this ICustomAttributeProvider value)
 {
     return(value.CustomAttributes.Any(a => a.AttributeType.Name == "CompilerGeneratedAttribute"));
 }
コード例 #28
0
 static CodeAttributeDeclarationCollection CreateCustomAttributes(ICustomAttributeProvider type)
 {
     var attributes = new CodeAttributeDeclarationCollection();
     PopulateCustomAttributes(type, attributes);
     return attributes;
 }
コード例 #29
0
 public static bool IsAsyncStateMachine(this ICustomAttributeProvider value)
 {
     return(value.CustomAttributes.Any(a => a.AttributeType.Name == "AsyncStateMachineAttribute"));
 }
コード例 #30
0
 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);
     }
 }
コード例 #31
0
 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"));
 }
コード例 #32
0
 static bool IsExtensionMethod(ICustomAttributeProvider method)
 {
     return method.CustomAttributes.Any(a => a.AttributeType.FullName == "System.Runtime.CompilerServices.ExtensionAttribute");
 }
コード例 #33
0
        public void AddPreserveAttribute(ICustomAttributeProvider provider)
        {
            var preserveAttributeConstructor = typeof(UnityEngine.Scripting.PreserveAttribute).GetConstructor(Type.EmptyTypes);

            provider.CustomAttributes.Add(new CustomAttribute(Module.ImportReference(preserveAttributeConstructor)));
        }
コード例 #34
0
 /// <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());
 }
コード例 #35
0
 public static bool IsCompilerGenerated(this Mono.Cecil.ICustomAttributeProvider thing)
 => thing?.CustomAttributes.Any(a => a.AttributeType.Name == nameof(CompilerGeneratedAttribute)) ?? false;