public override void Verify(Type type) { if (type == null) { throw new ArgumentNullException(nameof(type)); } var method = type .GetMethods() .SingleOrDefault(candidate => candidate.Name == "op_Implicit" && candidate.GetParameters().Length == 1 && candidate.GetParameters()[0].ParameterType == type && candidate.ReturnParameter.ParameterType == typeof(TResult)); if (method == null) { throw new ImplicitConversionOperatorException(type, typeof(TResult), $"The type '{type.Name}' does not define an implicit conversion operator to type '{typeof(TResult).Name}'."); } var value = Builder.Create <TResult>(); var builder = new CompositeSpecimenBuilder( new FrozenSpecimenBuilder <TResult>(value), Builder ); var instance = builder.CreateAnonymous(type); object result; try { result = method.Invoke(null, new[] { instance }); } catch (Exception exception) { throw new ImplicitConversionOperatorException(type, typeof(TResult), $"The implicit conversion operator to type '{typeof(TResult).Name}' of type '{type.Name}' threw an exception.", exception); } if (!((TResult)result).Equals(value)) { throw new ImplicitConversionOperatorException(type, typeof(TResult)); } }
public override void Verify(Type type) { if (type == null) { throw new ArgumentNullException(nameof(type)); } var method = type .GetMethods() .SingleOrDefault(candidate => candidate.Name == "To" + typeof(TResult).Name && candidate.GetParameters().Length == 0 && candidate.ReturnParameter.ParameterType == typeof(TResult)); if (method == null) { throw new ExplicitConversionMethodException(type, typeof(TResult), $"The type '{type.Name}' does not define an explicit conversion method to type '{typeof(TResult).Name}' called 'To{typeof(TResult).Name}()'."); } var value = Builder.Create <TResult>(); var builder = new CompositeSpecimenBuilder( new FrozenSpecimenBuilder <TResult>(value), Builder ); var instance = builder.CreateAnonymous(type); try { var result = (TResult)method.Invoke(instance, new object[0]); if (!result.Equals(value)) { throw new ExplicitConversionMethodException(type, typeof(TResult)); } } catch (Exception exception) { throw new ImplicitConversionOperatorException(type, typeof(TResult), $"The explicit conversion method to type '{typeof(TResult).Name}' of type '{type.Name}' threw an exception.", exception); } }