// This method is called at build time and should just provide other aspects. public IEnumerable <AspectInstance> ProvideAspects(object targetElement) { var targetType = (Type)targetElement; var introduceDataContractAspect = new CustomAttributeIntroductionAspect( new ObjectConstruction(typeof(DataContractAttribute).GetConstructor(Type.EmptyTypes))); var introduceDataMemberAspect = new CustomAttributeIntroductionAspect( new ObjectConstruction(typeof(DataMemberAttribute).GetConstructor(Type.EmptyTypes))); // Add the DataContract attribute to the type. yield return(new AspectInstance(targetType, introduceDataContractAspect)); // Add a DataMember attribute to every relevant property. foreach (var property in targetType.GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance)) { if (property.CanWrite && !property.IsDefined(typeof(NotDataMemberAttribute))) { yield return(new AspectInstance(property, introduceDataMemberAspect)); } } }
/// <summary> /// /// </summary> /// <param name="targetElement"></param> /// <returns></returns> public IEnumerable <AspectInstance> ProvideAspects(object targetElement) { var t = (Type)targetElement; var props = t.GetFields(BindingFlags.SetField | BindingFlags.Instance | BindingFlags.NonPublic); if (props.Length == 0) { yield return(null); } var objectConstruction = new ObjectConstruction(typeof(DataContractAttribute)); var ns = Zieschang.Net.Projects.PostsharpAspects.Utilities.Utilities.GetContractNamespace(t); objectConstruction.NamedArguments.Add("Namespace", ns); var introduceDataContractAttribute = new CustomAttributeIntroductionAspect(objectConstruction); var introduceDataMemberAttribute = new CustomAttributeIntroductionAspect(new ObjectConstruction(typeof(DataMemberAttribute))); yield return(new AspectInstance(t, introduceDataContractAttribute)); foreach (var prop in props) { if (!prop.IsDefined(typeof(NoDataMemberAttribute), true)) { yield return(new AspectInstance(prop, introduceDataMemberAttribute)); } } }
// This method is called at build time and should just provide other aspects. public IEnumerable <AspectInstance> ProvideAspects(object targetElement) { Type targetType = (Type)targetElement; var introduceServiceContractAspect = new CustomAttributeIntroductionAspect( new ObjectConstruction(typeof(ServiceContractAttribute) .GetConstructor(Type.EmptyTypes))); var introduceOperationContractAspect = new CustomAttributeIntroductionAspect( new ObjectConstruction(typeof(OperationContractAttribute) .GetConstructor(Type.EmptyTypes))); // Add the ServiceContract attribute to the type. yield return(new AspectInstance(targetType, introduceServiceContractAspect)); // Add a OperationContract attribute to every relevant method. var flags = BindingFlags.Public | BindingFlags.Instance; foreach (MethodInfo method in targetType.GetMethods(flags)) { if (!method.IsDefined(typeof(NotOperationContractAttribute), false)) { yield return(new AspectInstance(method, introduceOperationContractAspect)); } } }
/// <summary> /// The provide aspects. /// </summary> /// <param name="targetElement"> The target element. </param> /// <returns> The System.Collections.Generic.IEnumerable`1[T -> PostSharp.Aspects.AspectInstance]. </returns> /// <remarks> /// This method is called at build time and should just provide other aspects. /// </remarks> public IEnumerable<AspectInstance> ProvideAspects(object targetElement) { var targetMethod = (MethodBase)targetElement; var categoryAttributes = targetMethod.GetCustomAttributes(typeof(CategoryAttribute), false).Cast<CategoryAttribute>().ToArray(); var categoryName = string.Format("Specifications for {0}", GetTestedClassTypeName(targetMethod.DeclaringType)); if (!categoryAttributes.Any() || categoryAttributes.All(x => x.Name != categoryName)) { var categoryAttributeConstructorInfo = typeof(CategoryAttribute).GetConstructor(new[] { typeof(string) }); var introduceCategoryAspect = new CustomAttributeIntroductionAspect(new ObjectConstruction(categoryAttributeConstructorInfo, categoryName)); // Add the Category attribute to the type. yield return new AspectInstance(targetMethod, introduceCategoryAspect); } if (targetMethod.IsDefined(typeof(DescriptionAttribute), false)) { yield break; } var description = GetDescription(targetMethod); if (!string.IsNullOrEmpty(description)) { var descriptionAttributeConstructorInfo = typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }); var introduceDescriptionAspect = new CustomAttributeIntroductionAspect(new ObjectConstruction(descriptionAttributeConstructorInfo, description)); // Add the Description attribute to the type. yield return new AspectInstance(targetMethod, introduceDescriptionAspect); } }
public IEnumerable <AspectInstance> ProvideAspects(object targetElement) { var constructorInfo = typeof(System.Runtime.CompilerServices.ExtensionAttribute).GetConstructor(Type.EmptyTypes); var objectConstruction = new ObjectConstruction(constructorInfo); var aspectInstance = new CustomAttributeIntroductionAspect(objectConstruction); yield return(new AspectInstance(targetElement, aspectInstance)); }
private AspectInstance Create <T>(T target, string newName) { var x = new CustomAttributeIntroductionAspect( new ObjectConstruction(typeof(NewMethodName).GetConstructor(new Type[] { typeof(string) }), new object[] { newName }) ); return(new AspectInstance(target, x)); }
public IEnumerable <AspectInstance> ProvideAspects(object targetElement) { Type disabledType = (Type)targetElement; var introducedExclusion = new CustomAttributeIntroductionAspect( new ObjectConstruction(typeof(ExcludeFromCodeCoverageAttribute))); return(new[] { new AspectInstance(disabledType, introducedExclusion) }); }
/// <summary> /// This aspect also can attach DefaultValueAttribute to the marked elements (field / property) /// /// You need to set 'AttachDefaultValue' to 'true' to use this. /// /// No new DefaultValueAttribute will be attached if there is already one attached /// </summary> public IEnumerable <AspectInstance> ProvideAspects(object targetElement) { // if we do not have a value, post-compile checks failed if (!_hasValue) { yield break; } // define the attribute (if it wasn't defined before) if (_defaultValueAttributeIntroduction == null) { if (_valueType == null) { var constructorType = (_value != null) ? _value.GetType() : typeof(object); _defaultValueAttributeIntroduction = new CustomAttributeIntroductionAspect( new ObjectConstruction(typeof(DefaultValueAttribute).GetConstructor(new[] { constructorType }), _value)); } else { _defaultValueAttributeIntroduction = new CustomAttributeIntroductionAspect( new ObjectConstruction(typeof(DefaultValueAttribute).GetConstructor(new[] { typeof(Type), typeof(string) }), _valueType, _valueString)); } } var fieldInfo = targetElement as FieldInfo; var propertyInfo = targetElement as PropertyInfo; var locationInfo = targetElement as LocationInfo; if (locationInfo != null) { fieldInfo = locationInfo.FieldInfo; propertyInfo = locationInfo.PropertyInfo; } if (propertyInfo != null) { if (!propertyInfo.IsDefined(typeof(DefaultValueAttribute), false)) { yield return(new AspectInstance(propertyInfo, _defaultValueAttributeIntroduction)); } } if (fieldInfo != null) { if (!fieldInfo.IsDefined(typeof(DefaultValueAttribute), false)) { yield return(new AspectInstance(fieldInfo, _defaultValueAttributeIntroduction)); } } yield break; }
/// <summary> /// The provide aspects. /// </summary> /// <param name="targetElement"> The target element. </param> /// <returns> The System.Collections.Generic.IEnumerable`1[T -> PostSharp.Aspects.AspectInstance]. </returns> /// <remarks> /// This method is called at build time and should just provide other aspects. /// </remarks> public IEnumerable <AspectInstance> ProvideAspects(object targetElement) { var targetType = (Type)targetElement; if (targetType.IsGenericType) { // PostSharp doesn't manage to inject on generic types (generic type definitions?), so for now we ignore it. yield break; } if (!targetType.IsDefined(typeof(DescriptionAttribute), false)) { var contextDescription = GetNaturalLanguageContextName(targetType); var descriptionAttributeConstructorInfo = typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }); var introduceDescriptionAspect = new CustomAttributeIntroductionAspect(new ObjectConstruction(descriptionAttributeConstructorInfo, contextDescription)); // Add the Description attribute to the type. yield return(new AspectInstance(targetType, introduceDescriptionAspect)); } var localTestMethods = TypeInvestigationService.GetAllTestMethods(targetType) .Where(m => m.DeclaringType == targetType); foreach (var targetMethod in localTestMethods) { var categoryAttributes = targetMethod.GetCustomAttributes(typeof(CategoryAttribute), false).Cast <CategoryAttribute>().ToArray(); var categoryName = string.Format("Specifications for {0}", TypeInvestigationService.GetTestedClassTypeName(targetMethod.DeclaringType)); if (!categoryAttributes.Any() || categoryAttributes.All(x => x.Name != categoryName)) { var categoryAttributeConstructorInfo = typeof(CategoryAttribute).GetConstructor(new[] { typeof(string) }); var introduceCategoryAspect = new CustomAttributeIntroductionAspect(new ObjectConstruction(categoryAttributeConstructorInfo, categoryName)); // Add the Category attribute to the type. yield return(new AspectInstance(targetMethod, introduceCategoryAspect)); } if (!targetMethod.IsDefined(typeof(DescriptionAttribute), false)) { var description = GetDescription(targetMethod); if (!string.IsNullOrEmpty(description)) { var descriptionAttributeConstructorInfo = typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }); var introduceDescriptionAspect = new CustomAttributeIntroductionAspect(new ObjectConstruction(descriptionAttributeConstructorInfo, description)); // Add the Description attribute to the type. yield return(new AspectInstance(targetMethod, introduceDescriptionAspect)); } } } }
/// <summary> /// The provide aspects. /// </summary> /// <param name="targetElement"> The target element. </param> /// <returns> The System.Collections.Generic.IEnumerable`1[T -> PostSharp.Aspects.AspectInstance]. </returns> /// <remarks> /// This method is called at build time and should just provide other aspects. /// </remarks> public IEnumerable<AspectInstance> ProvideAspects(object targetElement) { var targetType = (Type)targetElement; if (targetType.IsGenericType) { // PostSharp doesn't manage to inject on generic types (generic type definitions?), so for now we ignore it. yield break; } if (!targetType.IsDefined(typeof(DescriptionAttribute), false)) { var contextDescription = GetNaturalLanguageContextName(targetType); var descriptionAttributeConstructorInfo = typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }); var introduceDescriptionAspect = new CustomAttributeIntroductionAspect(new ObjectConstruction(descriptionAttributeConstructorInfo, contextDescription)); // Add the Description attribute to the type. yield return new AspectInstance(targetType, introduceDescriptionAspect); } var localTestMethods = TypeInvestigationService.GetAllTestMethods(targetType) .Where(m => m.DeclaringType == targetType); foreach (var targetMethod in localTestMethods) { var categoryAttributes = targetMethod.GetCustomAttributes(typeof(CategoryAttribute), false).Cast<CategoryAttribute>().ToArray(); var categoryName = string.Format("Specifications for {0}", TypeInvestigationService.GetTestedClassTypeName(targetMethod.DeclaringType)); if (!categoryAttributes.Any() || categoryAttributes.All(x => x.Name != categoryName)) { var categoryAttributeConstructorInfo = typeof(CategoryAttribute).GetConstructor(new[] { typeof(string) }); var introduceCategoryAspect = new CustomAttributeIntroductionAspect(new ObjectConstruction(categoryAttributeConstructorInfo, categoryName)); // Add the Category attribute to the type. yield return new AspectInstance(targetMethod, introduceCategoryAspect); } if (!targetMethod.IsDefined(typeof(DescriptionAttribute), false)) { var description = GetDescription(targetMethod); if (!string.IsNullOrEmpty(description)) { var descriptionAttributeConstructorInfo = typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }); var introduceDescriptionAspect = new CustomAttributeIntroductionAspect(new ObjectConstruction(descriptionAttributeConstructorInfo, description)); // Add the Description attribute to the type. yield return new AspectInstance(targetMethod, introduceDescriptionAspect); } } } }
/// <summary> /// /// </summary> /// <param name="targetElement"></param> /// <returns></returns> public IEnumerable<AspectInstance> ProvideAspects(object targetElement) { var t = (Type)targetElement; var props = t.GetFields(BindingFlags.SetField | BindingFlags.Instance | BindingFlags.NonPublic); if (props.Length == 0) yield return null; var objectConstruction = new ObjectConstruction(typeof(DataContractAttribute)); var ns =Zieschang.Net.Projects.PostsharpAspects.Utilities.Utilities.GetContractNamespace(t); objectConstruction.NamedArguments.Add("Namespace", ns); var introduceDataContractAttribute = new CustomAttributeIntroductionAspect(objectConstruction); var introduceDataMemberAttribute = new CustomAttributeIntroductionAspect(new ObjectConstruction(typeof(DataMemberAttribute))); yield return new AspectInstance(t, introduceDataContractAttribute); foreach (var prop in props) { if (!prop.IsDefined(typeof(NoDataMemberAttribute), true)) yield return new AspectInstance(prop, introduceDataMemberAttribute); } }
// Этот метод вызывается во время сборки и нужен другим аспектам public IEnumerable <AspectInstance> ProvideAspects(object targetElement) { var targetType = (Type)targetElement; var datacontractIntroductionAspect = new CustomAttributeIntroductionAspect( new ObjectConstruction(typeof(DataContractAttribute).GetConstructor(Type.EmptyTypes))); var datamemberIntroductionAspect = new CustomAttributeIntroductionAspect( new ObjectConstruction(typeof(DataMemberAttribute).GetConstructor(Type.EmptyTypes))); // Добавим атрибут DataContract к типу yield return(new AspectInstance(targetElement, datacontractIntroductionAspect)); // Добавим атрибут DataMember к каждому соответствующему свойству foreach ( var property in targetType.GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance) .Where(property => property.CanWrite && !property.IsDefined(typeof(NotDataMemberAttribute), false))) { yield return(new AspectInstance(property, datamemberIntroductionAspect)); } }
/// <summary> /// Provides new aspects. /// </summary> /// <param name="targetElement">Code element (<see cref="T:System.Reflection.Assembly" />, <see cref="T:System.Type" />, /// <see cref="T:System.Reflection.FieldInfo" />, <see cref="T:System.Reflection.MethodBase" />, <see cref="T:System.Reflection.PropertyInfo" />, <see cref="T:System.Reflection.EventInfo" />, /// <see cref="T:System.Reflection.ParameterInfo" />, or <see cref="T:PostSharp.Reflection.LocationInfo" />) to which the current aspect has been applied.</param> /// <returns> /// A set of aspect instances. /// </returns> public IEnumerable<AspectInstance> ProvideAspects(object targetElement) { var targetType = (Type)targetElement; //This is to introduce a [DataContract] attribute var introduceDataContractAspect = new CustomAttributeIntroductionAspect( new ObjectConstruction( typeof(DataContractAttribute) .GetConstructor(Type.EmptyTypes))); //This is to introduce a [DataMember] attribute var introduceDataMemberAspect = new CustomAttributeIntroductionAspect( new ObjectConstruction( typeof(DataMemberAttribute) .GetConstructor(Type.EmptyTypes))); //Return an AspectInstance by applying the [DataContract] to the type. yield return new AspectInstance(targetType, introduceDataContractAspect); var properties = targetType.GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance); foreach (var property in properties) { //CanWrite set to true and does not have a NotADataMemberAttribute defined on it. if (property.CanWrite && !property.IsDefined(typeof(NotADataMemberAttribute), false)) { //Return an AspectInstance by applying the [DataMember] to the type's property. yield return new AspectInstance(property, introduceDataMemberAspect); } } }
/// <summary> /// Provides new aspects. /// </summary> /// <param name="targetElement">Code element (<see cref="T:System.Reflection.Assembly" />, <see cref="T:System.Type" />, /// <see cref="T:System.Reflection.FieldInfo" />, <see cref="T:System.Reflection.MethodBase" />, <see cref="T:System.Reflection.PropertyInfo" />, <see cref="T:System.Reflection.EventInfo" />, /// <see cref="T:System.Reflection.ParameterInfo" />, or <see cref="T:PostSharp.Reflection.LocationInfo" />) to which the current aspect has been applied.</param> /// <returns> /// A set of aspect instances. /// </returns> public IEnumerable <AspectInstance> ProvideAspects(object targetElement) { var targetType = (Type)targetElement; //This is to introduce a [DataContract] attribute var introduceDataContractAspect = new CustomAttributeIntroductionAspect( new ObjectConstruction( typeof(DataContractAttribute) .GetConstructor(Type.EmptyTypes))); //This is to introduce a [DataMember] attribute var introduceDataMemberAspect = new CustomAttributeIntroductionAspect( new ObjectConstruction( typeof(DataMemberAttribute) .GetConstructor(Type.EmptyTypes))); //Return an AspectInstance by applying the [DataContract] to the type. yield return(new AspectInstance(targetType, introduceDataContractAspect)); var properties = targetType.GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance); foreach (var property in properties) { //CanWrite set to true and does not have a NotADataMemberAttribute defined on it. if (property.CanWrite && !property.IsDefined(typeof(NotADataMemberAttribute), false)) { //Return an AspectInstance by applying the [DataMember] to the type's property. yield return(new AspectInstance(property, introduceDataMemberAspect)); } } }
public IEnumerable <AspectInstance> ProvideAspects(object targetElement) { var targetType = (Type)targetElement; // find all classes to attach DataContract attribute var dataContractAspect = new CustomAttributeIntroductionAspect( new ObjectConstruction(typeof(DataContractAttribute).GetConstructor(Type.EmptyTypes))); // find all properties to attach DataMemebr attribute var dataMemberAspect = new CustomAttributeIntroductionAspect( new ObjectConstruction(typeof(DataMemberAttribute).GetConstructor(Type.EmptyTypes))); yield return(new AspectInstance(targetType, dataContractAspect)); // attach to all proeprties that are public or declared or an instance foreach (var property in targetType.GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance)) { // here we are going to "use" our NotDataMemebrAttribute if (property.CanWrite && !property.IsDefined(typeof(NotADataMemberAttributeAspect), false)) { yield return(new AspectInstance(property, dataMemberAspect)); } } }
/// <summary> /// This aspect also can attach DefaultValueAttribute to the marked elements (field / property) /// /// You need to set 'AttachDefaultValue' to 'true' to use this. /// /// No new DefaultValueAttribute will be attached if there is already one attached /// </summary> public IEnumerable<AspectInstance> ProvideAspects(object targetElement) { // if we do not have a value, post-compile checks failed if (!_hasValue) { yield break; } // define the attribute (if it wasn't defined before) if (_defaultValueAttributeIntroduction == null) { if (_valueType == null) { var constructorType = (_value != null) ? _value.GetType() : typeof(object); _defaultValueAttributeIntroduction = new CustomAttributeIntroductionAspect( new ObjectConstruction(typeof(DefaultValueAttribute).GetConstructor(new[] { constructorType }), _value)); } else { _defaultValueAttributeIntroduction = new CustomAttributeIntroductionAspect( new ObjectConstruction(typeof(DefaultValueAttribute).GetConstructor(new[] { typeof(Type), typeof(string) }), _valueType, _valueString)); } } var fieldInfo = targetElement as FieldInfo; var propertyInfo = targetElement as PropertyInfo; var locationInfo = targetElement as LocationInfo; if (locationInfo != null) { fieldInfo = locationInfo.FieldInfo; propertyInfo = locationInfo.PropertyInfo; } if (propertyInfo != null) { if (!propertyInfo.IsDefined(typeof(DefaultValueAttribute), false)) { yield return new AspectInstance(propertyInfo, _defaultValueAttributeIntroduction); } } if (fieldInfo != null) { if (!fieldInfo.IsDefined(typeof(DefaultValueAttribute), false)) { yield return new AspectInstance(fieldInfo, _defaultValueAttributeIntroduction); } } yield break; }