public void TestInitialize( ) { _fixture = new Fixture( ); _sutCreator = new SutCreator(new SutInstanceCreator(new ArgumentNullExceptionFinder( )), new SutLazyInstanceCreator(new ArgumentNullExceptionFinder( ), new CustomAttributeFinder( ))); _freezeAttribute = Substitute.For <ICustomAttributeData> ( ); _freezeAttribute.AttributeType.Returns(typeof(FreezeAttribute)); _customAttributesWithFreeze = new [] { _freezeAttribute }; _infoInt = CreateParameterInfo(typeof(int)); _infoLazyString = CreateParameterInfo(typeof(Lazy <string>)); _infoString = CreateParameterInfo(typeof(string)); _infoClass = CreateParameterInfo(typeof(SomethingElse)); _infoSut = CreateParameterInfo(typeof(Something)); _infoLazyClass = CreateParameterInfo(typeof(Lazy <Something>)); _infoSomething = CreateParameterInfo(typeof(ISomething)); _infoSomethingElseWithFreeze = CreateParameterInfo(typeof(ISomethingElse)); _infoSomethingElseWithFreeze.CustomAttributes.Returns(_customAttributesWithFreeze); _infoStringFreeze = CreateParameterInfo(typeof(string)); _infoStringFreeze.CustomAttributes.Returns(_customAttributesWithFreeze); }
public AttributeConstruction(ICustomAttributeData customAttributeData) { var constructorInfo = customAttributeData.Constructor; _constructorInfo = constructorInfo; _constructorArguments = customAttributeData.ConstructorArguments; _namedArguments = customAttributeData.NamedArguments; }
/// <summary> /// Determines if the <see cref="CustomAttributeData"/> allows multiple use. /// </summary> public static bool AllowsMultiple(this ICustomAttributeData customAttributeData) { ArgumentUtility.CheckNotNull("customAttributeData", customAttributeData); Assertion.IsTrue(customAttributeData.Constructor.DeclaringType != null); var attributeType = customAttributeData.Constructor.DeclaringType; var attributeUsageAttribute = attributeType.GetCustomAttributes <AttributeUsageAttribute> (inherit: true).Single(); return(attributeUsageAttribute.AllowMultiple); }
public IEnumerable <IPointcut> Build(ICustomAttributeData customAttributeData) { foreach (var argument in customAttributeData.NamedArguments) { Type pointcutType; if (s_namedArgumentsDictionary.TryGetValue(argument.MemberInfo.Name, out pointcutType)) { yield return((IPointcut)Activator.CreateInstance(pointcutType, (object[])argument.Value)); } } }
public void GetCustomAttributes_NewInstance() { var datas = new ICustomAttributeData[] { CustomAttributeDeclarationObjectMother.Create() }; _providerMock.Expect(mock => mock.GetCustomAttributeData(_randomInherit)).Return(datas).Repeat.Twice(); var attribute1 = CustomAttributeFinder.GetCustomAttributes(_providerMock, _randomInherit).Single(); var attribute2 = CustomAttributeFinder.GetCustomAttributes(_providerMock, _randomInherit).Single(); Assert.That(attribute1, Is.Not.SameAs(attribute2)); }
public void GetCustomAttributes_NewInstance() { var datas = new ICustomAttributeData[] { CustomAttributeDeclarationObjectMother.Create() }; _providerMock.Setup(mock => mock.GetCustomAttributeData(_randomInherit)).Returns(datas); var attribute1 = CustomAttributeFinder.GetCustomAttributes(_providerMock.Object, _randomInherit).Single(); var attribute2 = CustomAttributeFinder.GetCustomAttributes(_providerMock.Object, _randomInherit).Single(); Assert.That(attribute1, Is.Not.SameAs(attribute2)); _providerMock.Verify(mock => mock.GetCustomAttributeData(_randomInherit), Times.Exactly(2)); }
public void AddAttribute(IMutableMember member, ICustomAttributeData attributeData) { ArgumentUtility.CheckNotNull("member", member); ArgumentUtility.CheckNotNull("attributeData", attributeData); var attribute = new CustomAttributeDeclaration( attributeData.Constructor, attributeData.ConstructorArguments.ToArray(), attributeData.NamedArguments.Select(CreateNamedArgumentDeclaration).ToArray()); member.AddCustomAttribute(attribute); }
public Aspect Build(ICustomAttributeData attributeData) { var attribute = attributeData.CreateAttribute <AspectAttributeBase>(); // throw if no aspectattributebase var construction = new AttributeConstruction(attributeData); var pointcut = _pointcutBuilder.Build(attributeData); var priorityArgument = attributeData.NamedArguments.SingleOrDefault(x => x.MemberInfo.Name == "Priority"); var priority = priorityArgument == null ? 0 : (int)priorityArgument.Value; return(Build(attributeData.Type, attribute, construction, pointcut, priority)); }
private void ApplyViaCopyAttribute(MemberInfo copyAttributeSource, ICustomAttributeData copyAttributeData) { Assertion.IsTrue(copyAttributeData.Constructor.DeclaringType == typeof(CopyCustomAttributesAttribute)); string sourceName = GetFullMemberName(copyAttributeSource); var copyAttribute = (CopyCustomAttributesAttribute)copyAttributeData.CreateInstance(); MemberInfo copiedAttributesSource; try { copiedAttributesSource = copyAttribute.GetAttributeSource(UnifyTypeMemberTypes(copyAttributeSource.MemberType)); } catch (AmbiguousMatchException ex) { string message = string.Format("The CopyCustomAttributes attribute on {0} specifies an ambiguous attribute source: {1}", sourceName, ex.Message); throw new ConfigurationException(message, ex); } if (copiedAttributesSource == null) { string message = string.Format("The CopyCustomAttributes attribute on {0} specifies an unknown attribute source {1}.", sourceName, copyAttribute.AttributeSourceName); throw new ConfigurationException(message); } if (!AreCompatibleMemberTypes(copiedAttributesSource.MemberType, copyAttributeSource.MemberType)) { string message = string.Format("The CopyCustomAttributes attribute on {0} specifies an attribute source {1} of a different member kind.", sourceName, copyAttribute.AttributeSourceName); throw new ConfigurationException(message); } // A CopyCustomAttribute can specify the same type/member it itself is sitting on; this can be used to get non-inheritable attributes to be // copied to the target. (Normally, only inheritable attributes are copied.) // In this case, we must avoid parsing copy attributes again to avoid recursion. // In addition, we will add non-inheritable attributes as copy templates. Inheritable attributes are not added as copy templates, since they'll // be inherited anyway. var isCopyingFromSameMember = copiedAttributesSource.Equals(copyAttributeSource); var copiedAttributesData = GetCopiedAttributesData( copiedAttributesSource: copiedAttributesSource, copyAttribute: copyAttribute, includeCopyAttributes: !isCopyingFromSameMember, includeInheritableAttributes: !isCopyingFromSameMember); Apply(copiedAttributesSource, copiedAttributesData, true); }
public IPointcut Build(ICustomAttributeData customAttributeData) { var pointcuts = new List <IPointcut>(); foreach (var argument in customAttributeData.NamedArguments) { Type pointcutType; if (s_namedArgumentsDictionary.TryGetValue(argument.MemberInfo.Name, out pointcutType)) { var pointcut = pointcutType.CreateInstance <IPointcut> (argument.Value); pointcuts.Add(pointcut); } } return(new AllPointcut(pointcuts)); }
/// <summary> /// Creates the actual <see cref="Attribute"/> described by an instance of <see cref="CustomAttributeData"/>. /// </summary> /// <param name="customAttributeData">The data describing the attribute.</param> /// <returns>The attribute.</returns> public static Attribute CreateAttribute(this ICustomAttributeData customAttributeData) { ArgumentUtility.CheckNotNull("customAttributeData", customAttributeData); Assertion.IsTrue(customAttributeData.NamedArguments != null); var arguments = customAttributeData.ConstructorArguments.ToArray(); var attribute = (Attribute)customAttributeData.Constructor.Invoke(arguments); foreach (var namedArgument in customAttributeData.NamedArguments) { var argument = ConvertTypedArgumentToObject(namedArgument); SetMemberValue(namedArgument.MemberInfo, attribute, argument); } return(attribute); }
public static object CreateInstance(this ICustomAttributeData customAttributeData) { ArgumentUtility.CheckNotNull("customAttributeData", customAttributeData); var instance = customAttributeData.Constructor.Invoke(customAttributeData.ConstructorArguments.ToArray()); foreach (var namedArgument in customAttributeData.NamedArguments) { if (namedArgument.MemberInfo is FieldInfo) { ((FieldInfo)namedArgument.MemberInfo).SetValue(instance, namedArgument.Value); } else if (namedArgument.MemberInfo is PropertyInfo) { ((PropertyInfo)namedArgument.MemberInfo).SetValue(instance, namedArgument.Value, new object[0]); } else { throw new InvalidOperationException("customAttributeNamedArgument.MemberInfo can only be FieldInfo or PropertyInfo."); } } return(instance); }
public static T CreateAttribute <T> (this ICustomAttributeData customAttributeData) where T : Attribute { ArgumentUtility.CheckNotNull("customAttributeData", customAttributeData); return((T)CreateAttribute(customAttributeData)); }
/// <summary> /// Determines if the <see cref="CustomAttributeData"/> describes an <see cref="AspectAttribute"/>. /// </summary> public static bool IsAspectAttribute(this ICustomAttributeData customAttributeData) { return(typeof(AspectAttributeBase).IsAssignableFrom(customAttributeData.Constructor.DeclaringType)); }
public AttributeDefinition(IAttributableDefinition declaringDefinition, ICustomAttributeData data, bool isCopyTemplate) { _declaringDefinition = declaringDefinition; _data = data; _isCopyTemplate = isCopyTemplate; }