Attribute Instantiate(CustomAttributeData attributeData) { var ctorArgs = GetConstructorArguments().ToArray(); var ctorArgTypes = attributeData.ConstructorArguments.Select(ci => ci.ArgumentType).ToArray(); var attribute = (Attribute)Activator.CreateInstance(attributeData.AttributeType, Reflector.ConvertArguments(ctorArgs, ctorArgTypes)); var ati = attribute.GetType(); foreach (var namedArg in attributeData.NamedArguments) { (ati.GetRuntimeProperty(namedArg.MemberName)).SetValue(attribute, namedArg.TypedValue.Value, index: null); } return(attribute); }
internal static IEnumerable <IAttributeInfo> GetCustomAttributes(Type type, string assemblyQualifiedAttributeTypeName) { Type attributeType = Reflector.GetType(assemblyQualifiedAttributeTypeName); return(GetCustomAttributes(type, attributeType, GetAttributeUsage(attributeType))); }
/// <inheritdoc/> public ITypeInfo GetType(string typeName) { var type = Assembly.GetType(typeName); return(type == null ? null : Reflector.Wrap(type)); }
Attribute Instantiate(CustomAttributeData attributeData) { var ctorArgs = GetConstructorArguments().ToArray(); Type[] ctorArgTypes = Reflector.EmptyTypes; if (ctorArgs.Length > 0) { ctorArgTypes = new Type[attributeData.ConstructorArguments.Count]; for (int i = 0; i < ctorArgTypes.Length; i++) { ctorArgTypes[i] = attributeData.ConstructorArguments[i].ArgumentType; } } var attribute = (Attribute)Activator.CreateInstance(attributeData.AttributeType, Reflector.ConvertArguments(ctorArgs, ctorArgTypes)); var ati = attribute.GetType(); for (int i = 0; i < attributeData.NamedArguments.Count; i++) { var namedArg = attributeData.NamedArguments[i]; (ati.GetRuntimeProperty(namedArg.MemberName)).SetValue(attribute, GetTypedValue(namedArg.TypedValue), index: null); } return(attribute); }
/// <inheritdoc/> public IReadOnlyCollection <_ITypeInfo> GetGenericArguments() => Type .GenericTypeArguments .Select(t => Reflector.Wrap(t)) .CastOrToReadOnlyCollection();
/// <summary> /// Initializes a new instance of the <see cref="ReflectionParameterInfo"/> class. /// </summary> /// <param name="parameterInfo">The parameter to be wrapped.</param> public ReflectionParameterInfo(ParameterInfo parameterInfo) { ParameterInfo = Guard.ArgumentNotNull(parameterInfo); parameterType = new(() => Reflector.Wrap(ParameterInfo.ParameterType)); }
Attribute Instantiate(CustomAttributeData attributeData) { var ctorArgs = GetConstructorArguments().ToArray(); Type[] ctorArgTypes = Reflector.EmptyTypes; if (ctorArgs.Length > 0) { ctorArgTypes = new Type[attributeData.ConstructorArguments.Count]; for (int i = 0; i < ctorArgTypes.Length; i++) { ctorArgTypes[i] = attributeData.ConstructorArguments[i].ArgumentType; } } var attribute = (Attribute)Activator.CreateInstance(attributeData.AttributeType, Reflector.ConvertArguments(ctorArgs, ctorArgTypes)); var ati = attribute.GetType(); for (int i = 0; i < attributeData.NamedArguments.Count; i++) { var namedArg = attributeData.NamedArguments[i]; var typedValue = GetTypedValue(namedArg.TypedValue); var memberName = namedArg.MemberName; var propInfo = ati.GetRuntimeProperty(memberName); if (propInfo != null) { propInfo.SetValue(attribute, typedValue); } else { var fieldInfo = ati.GetRuntimeField(memberName); if (fieldInfo != null) { fieldInfo.SetValue(attribute, typedValue); } else { throw new ArgumentException($"Could not find property or field named '{memberName}' on instance of '{Attribute.GetType().FullName}'", nameof(attributeData)); } } } return(attribute); }
/// <summary> /// Get the traits from a method. /// </summary> /// <param name="member">The member (method, field, etc.) to get the traits for.</param> /// <returns>A list of traits that are defined on the method.</returns> public static IReadOnlyList <KeyValuePair <string, string> > GetTraits(MemberInfo member) { var messageSink = new NullMessageSink(); var result = new List <KeyValuePair <string, string> >(); foreach (var traitAttributeData in member.CustomAttributes) { var traitAttributeType = traitAttributeData.AttributeType; if (!typeof(ITraitAttribute).GetTypeInfo().IsAssignableFrom(traitAttributeType.GetTypeInfo())) { continue; } var discovererAttributeData = FindDiscovererAttributeType(traitAttributeType.GetTypeInfo()); if (discovererAttributeData == null) { continue; } var discoverer = ExtensibilityPointFactory.GetTraitDiscoverer(messageSink, Reflector.Wrap(discovererAttributeData)); if (discoverer == null) { continue; } var traits = discoverer.GetTraits(Reflector.Wrap(traitAttributeData)); if (traits != null) { result.AddRange(traits); } } return(result); }
/// <inheritdoc /> protected override async Task RunTestsOnMethodAsync(IMessageBus messageBus, Type classUnderTest, object[] constructorArguments, MethodInfo methodUnderTest, List <BeforeAfterTestAttribute> beforeAfterAttributes, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource) { var executionTime = 0M; try { var testMethod = Reflector.Wrap(methodUnderTest); var dataAttributes = testMethod.GetCustomAttributes(typeof(DataAttribute)); foreach (var dataAttribute in dataAttributes) { var discovererAttribute = dataAttribute.GetCustomAttributes(typeof(DataDiscovererAttribute)).First(); var args = discovererAttribute.GetConstructorArguments().Cast <string>().ToList(); var discovererType = Reflector.GetType(args[1], args[0]); var discoverer = ExtensibilityPointFactory.GetDataDiscoverer(discovererType); foreach (object[] dataRow in discoverer.GetData(dataAttribute, testMethod)) { var methodToRun = methodUnderTest; ITypeInfo[] resolvedTypes = null; if (methodToRun.IsGenericMethodDefinition) { resolvedTypes = ResolveGenericTypes(testMethod, dataRow); methodToRun = methodToRun.MakeGenericMethod(resolvedTypes.Select(t => ((IReflectionTypeInfo)t).Type).ToArray()); } executionTime += await RunTestWithArgumentsAsync(messageBus, classUnderTest, constructorArguments, methodToRun, dataRow, GetDisplayNameWithArguments(DisplayName, dataRow, resolvedTypes), beforeAfterAttributes, aggregator, cancellationTokenSource); if (cancellationTokenSource.IsCancellationRequested) { return; } } } } catch (Exception ex) { if (!messageBus.QueueMessage(new TestStarting(this, DisplayName))) { cancellationTokenSource.Cancel(); } else { if (!messageBus.QueueMessage(new TestFailed(this, DisplayName, executionTime, null, ex.Unwrap()))) { cancellationTokenSource.Cancel(); } } if (!messageBus.QueueMessage(new TestFinished(this, DisplayName, executionTime, null))) { cancellationTokenSource.Cancel(); } } }
Attribute Instantiate(CustomAttributeData attributeData) { var ctorArgs = GetConstructorArguments().ToArray(); var ctorArgTypes = attributeData.Constructor.GetParameters().Select(p => p.ParameterType).ToArray(); var attribute = (Attribute)Activator.CreateInstance(attributeData.Constructor.ReflectedType, Reflector.ConvertArguments(ctorArgs, ctorArgTypes)); foreach (var namedArg in attributeData.NamedArguments) { ((PropertyInfo)namedArg.MemberInfo).SetValue(attribute, namedArg.TypedValue.Value, index: null); } return(attribute); }
/// <inheritdoc/> public IEnumerable <ITypeInfo> GetGenericArguments() { return(Type.GetTypeInfo().GenericTypeArguments .Select(t => Reflector.Wrap(t)) .ToList()); }