public static IValidationContext Resolve(Type declaringType, string[] properties, object instance) { var t = typeof(ValidationContext <>).MakeGenericType(declaringType); #if NETFRAMEWORK var @params = new object[] { instance, new PropertyChain(), new MemberNameValidatorSelector(properties) }; if (instance is null) { var ctor = t.GetConstructor(new[] { declaringType, typeof(PropertyChain), typeof(IValidatorSelector) }); return(ctor !.Invoke(@params).AsOrDefault <IValidationContext>()); } else { return(TypeVisit.CreateInstance <IValidationContext>(t, @params)); } #else var args = new List <ArgumentDescriptor> { new ArgumentDescriptor("instanceToValidate", instance, declaringType), new ArgumentDescriptor("propertyChain", new PropertyChain(), typeof(PropertyChain)), new ArgumentDescriptor("validatorSelector", new MemberNameValidatorSelector(properties), typeof(IValidatorSelector)) }; return(TypeVisit.CreateInstance <IValidationContext>(t, args)); #endif }
/// <summary> /// Create an instance of the specified type.<br /> /// 创建指定类型的实例。 /// </summary> /// <param name="type"></param> /// <param name="argument"></param> /// <param name="arguments"></param> /// <returns></returns> public static object CreateInstance(this Type type, ArgumentDescriptor argument, params ArgumentDescriptor[] arguments) { var descriptors = arguments.ToList(); descriptors.Insert(0, argument); return(TypeVisit.CreateInstance(type, descriptors)); }
public void GenericTypeCreateInstanceWithoutParamsTest() { var instance = TypeVisit.CreateInstance <NormalWithAttrClass>(); instance.ShouldNotBeNull(); instance.GetType().ShouldBe(typeof(NormalWithAttrClass)); }
/// <summary> /// Register IObjectPoolManagedModel type /// </summary> /// <param name="managedModelType"></param> /// <returns></returns> /// <exception cref="ArgumentException"></exception> public static bool Register(Type managedModelType) { if (managedModelType is null) { throw new ArgumentNullException(nameof(managedModelType)); } if (DefaultManagedModelType(managedModelType)) { throw new ArgumentException($"The default type '{nameof(ObjectPoolManagedModel)}' does not provide registration."); } if (_managedModels.ContainsKey(managedModelType)) { throw new ArgumentException("The type has been registered."); } if (!IsValidManagedModel(managedModelType)) { throw new ArgumentException("This type is not a valid IObjectPoolManagedModel instance."); } var instance = TypeVisit.CreateInstance(managedModelType) as IObjectPoolManagedModel; return(_managedModels.TryAdd(managedModelType, instance)); }
public void DirectTypeCreateInstanceWithOneParamsTest() { var instance = TypeVisit.CreateInstance(typeof(NormalWithAttrClass), "test"); instance.ShouldNotBeNull(); instance.GetType().ShouldBe(typeof(NormalWithAttrClass)); ((NormalWithAttrClass)instance).Nice.ShouldBe("test"); }
public void GenericTypeCreateInstanceWithTwoParamsTest() { var instance = TypeVisit.CreateInstance <NormalWithAttrClass>("test", 2); instance.ShouldNotBeNull(); instance.GetType().ShouldBe(typeof(NormalWithAttrClass)); instance.Nice.ShouldBe("test"); instance.Index.ShouldBe(2); }
/// <summary> /// Create an exception and raise. /// </summary> /// <typeparam name="TException">Special type TException.</typeparam> /// <param name="assertion">Predicate.</param> /// <param name="exceptionParams">Parameters for exception.</param> public static void Raise <TException>(bool assertion, params object[] exceptionParams) where TException : Exception { if (assertion) { return; } var exception = TypeVisit.CreateInstance <TException>(exceptionParams); ExceptionHelper.PrepareForRethrow(exception); }
public void DirectTypeCreateInstanceWithDeclareGenericTypeTest() { var instance1 = TypeVisit.CreateInstance <NormalWithAttrClass>(typeof(NormalWithAttrClass)); var instance2 = TypeVisit.CreateInstance <NormalWithAttrClass>(typeof(NormalWithAttrClass), "test"); var instance3 = TypeVisit.CreateInstance <NormalWithAttrClass>(typeof(NormalWithAttrClass), "test", 2); var instance4 = TypeVisit.CreateInstance <NormalWithAttrClass>(typeof(NormalWithAttrClass), 2, "test"); instance1.ShouldNotBeNull(); instance2.ShouldNotBeNull(); instance3.ShouldNotBeNull(); instance4.ShouldBeNull(); }
public FluentValidator(Type typeOfValidator) : base("FluentValidationWrappedValidator") { if (typeOfValidator is null) { throw new ArgumentNullException(nameof(typeOfValidator)); } if (!typeOfValidator.IsDerivedFrom <FluentValidation.IValidator>()) { throw new ArgumentException("This type must derived from 'FluentValidation.IValidator'.", nameof(typeOfValidator)); } _validatorImpl = TypeVisit.CreateInstance <FluentValidation.IValidator>(typeOfValidator); _typeOfValidator = typeOfValidator; }
public void DirectTypeCreateInstanceWithDynamicParamsTest() { var paramsZero = new List <ArgumentDescriptor>(); var paramsOne = new List <ArgumentDescriptor> { new("value", "test", typeof(string)), }; var paramsTwo = new List <ArgumentDescriptor> { new("value", "test", typeof(string)), new("index", 1, typeof(int)) }; var paramsThree = new List <ArgumentDescriptor> { new("index", 1, typeof(int)), new("value", "test", typeof(string)) }; var paramsFour = new List <ArgumentDescriptor> { new("index", 1, typeof(int)) }; var instance0 = TypeVisit.CreateInstance(typeof(NormalWithAttrClass), paramsZero); var instance1 = TypeVisit.CreateInstance(typeof(NormalWithAttrClass), paramsOne); var instance2 = TypeVisit.CreateInstance(typeof(NormalWithAttrClass), paramsTwo); var instance3 = TypeVisit.CreateInstance(typeof(NormalWithAttrClass), paramsThree); var instance4 = TypeVisit.CreateInstance(typeof(NormalWithAttrClass), paramsFour); instance0.ShouldNotBeNull(); instance1.ShouldNotBeNull(); instance2.ShouldNotBeNull(); instance3.ShouldNotBeNull(); instance4.ShouldNotBeNull(); ((NormalWithAttrClass)instance0).Nice.ShouldBeNullOrEmpty(); ((NormalWithAttrClass)instance0).Index.ShouldBe(0); ((NormalWithAttrClass)instance1).Nice.ShouldBe("test"); ((NormalWithAttrClass)instance1).Index.ShouldBe(0); ((NormalWithAttrClass)instance2).Nice.ShouldBe("test"); ((NormalWithAttrClass)instance2).Index.ShouldBe(1); ((NormalWithAttrClass)instance3).Nice.ShouldBe("test"); ((NormalWithAttrClass)instance3).Index.ShouldBe(1); ((NormalWithAttrClass)instance4).Nice.ShouldBeNullOrEmpty(); ((NormalWithAttrClass)instance4).Index.ShouldBe(1); }
/// <summary> /// Resolve a validator based on a given type. /// </summary> /// <param name="type"></param> /// <returns></returns> public virtual IValidator Resolve(Type type) { var d = typeof(AggregationValidator <>); var v = d.MakeGenericType(type); #if !NETFRAMEWORK var args = new List <ArgumentDescriptor> { new("projectManager", _projectManager, typeof(IValidationProjectManager)), new("objectResolver", _objectResolver, typeof(IVerifiableObjectResolver)), new("customValidatorManager", _customValidatorManager, typeof(ICustomValidatorManager)), new("options", _options, typeof(ValidationOptions)) }; return(TypeVisit.CreateInstance <IValidator>(v, args)); #else return(TypeVisit.CreateInstance <IValidator>(v, _projectManager, _objectResolver, this, _options)); #endif }
/// <summary> /// Create an exception and raise. /// </summary> /// <typeparam name="TException">Special type TException.</typeparam> /// <param name="assertion">Predicate.</param> /// <param name="options">Cosmos exception options.</param> public static void Raise <TException>(bool assertion, ExceptionOptions options) where TException : CosmosException { if (assertion) { return; } Exception exception; if (options is null) { exception = new ArgumentNullException(nameof(options)); } else { exception = TypeVisit.CreateInstance <TException>(options); } ExceptionHelper.PrepareForRethrow(exception); }
public static ObjectCallerBase Ctor(Type type) { if (SlimDeterminer.Check(type, out var createFromSlimService)) { return(createFromSlimService); } var callerType = typeof(CompatibleObjectCaller <>).MakeGenericType(type); var accessor = type.CreateTypeAccessor(true); if (!ObjectMemberCache.TryTouch(type, out var members)) { members = type.GetObjectMembers(); ObjectMemberCache.Cache(type, members); } var @params = new object[] { accessor, members }; return(TypeVisit.CreateInstance <ObjectCallerBase>(callerType, @params)); }
public static unsafe ObjectCallerBase Ctor(Type type) { ObjectCallerBase caller; if (TupleServiceTypeHelper.IsSupportedTupleType(type)) { var callerType = typeof(TupleServiceSlimObjectCaller <>).MakeGenericType(type); #if NET452 || NET462 var accessor = type.CreateTypeAccessor(true); var @params = new object[] { accessor }; caller = TypeVisit.CreateInstance <ObjectCallerBase>(callerType, @params); #else caller = TypeVisit.CreateInstance <ObjectCallerBase>(callerType); #endif } else { throw new InvalidOperationException("Is not a valid tuple type."); } return(caller); }
protected void CreateAndCacheExceptionInstance(Dictionary <string, IArgDescriptionVal> exceptionParams) { if (CachedException is null) { var options = new ExceptionBuildingOptions(TargetType, exceptionParams) .AddArg(ExceptionArgConstants.INNER, _innerException, x => x != null) .AddArg(ExceptionArgConstants.INNER_EXCEPTION, _innerException, x => x != null) .AddArg(ExceptionArgConstants.MESSAGE, _message, x => !string.IsNullOrWhiteSpace(x)) .AddArg(ExceptionArgConstants.PARAM_NAME, _paramName, x => !string.IsNullOrWhiteSpace(x)) .AddArg(ExceptionArgConstants.ACTUAL_VALUE, _actualValue, x => x is not null) .AddArg(ExceptionArgConstants.ERROR_CODE, _errorCode, x => x > 0); if (exceptionParams is not null) { _additionalOps?.Invoke(exceptionParams, options); } #if !NETFRAMEWORK CachedException = TypeVisit.CreateInstance(options.ExceptionType, options.ArgumentDescriptors); #else CachedException = TypeVisit.CreateInstance(options.ExceptionType, options.ArgumentDescriptors.ToArray()); #endif } }
public void DirectTypeCreateInstanceWithWrongDynamicParamsTest() { var paramsFive = new List <ArgumentDescriptor> { new("indexZee", 1, typeof(int)), new("indexWuu", 'c', typeof(char)) }; var paramsSix = new List <ArgumentDescriptor> { new("index", 1, typeof(int)), new("index", 2, typeof(int)), }; var paramsSeven = new List <ArgumentDescriptor> { new("index", "1", typeof(string)), new("value", 2, typeof(int)), }; var instance5 = TypeVisit.CreateInstance(typeof(NormalWithAttrClass), paramsFive); var instance6 = TypeVisit.CreateInstance(typeof(NormalWithAttrClass), paramsSix); var instance7 = TypeVisit.CreateInstance(typeof(NormalWithAttrClass), paramsSeven); instance5.ShouldNotBeNull(); instance6.ShouldNotBeNull(); instance7.ShouldNotBeNull(); ((NormalWithAttrClass)instance5).Nice.ShouldBeNullOrEmpty(); ((NormalWithAttrClass)instance5).Index.ShouldBe(0); ((NormalWithAttrClass)instance6).Nice.ShouldBeNullOrEmpty(); ((NormalWithAttrClass)instance6).Index.ShouldBe(2); ((NormalWithAttrClass)instance7).Nice.ShouldBeNullOrEmpty(); ((NormalWithAttrClass)instance7).Index.ShouldBe(0); }
public static IValidationContext Resolve(Type declaringType, object instance) { var t = typeof(ValidationContext <>).MakeGenericType(declaringType); #if NETFRAMEWORK var @params = new object[] { instance }; if (instance is null) { var ctor = t.GetConstructor(new[] { declaringType }); return(ctor !.Invoke(@params).AsOrDefault <IValidationContext>()); } else { return(TypeVisit.CreateInstance <IValidationContext>(t, @params)); } #else var args = new List <ArgumentDescriptor> { new ArgumentDescriptor("instanceToValidate", instance, declaringType) }; return(TypeVisit.CreateInstance <IValidationContext>(t, args)); #endif }
/// <summary> /// Create an exception and raise. /// </summary> /// <typeparam name="TException">Special type T.</typeparam> /// <param name="assertion">Predicate.</param> /// <param name="message">Error message.</param> /// <param name="innerException"></param> public static void Raise <TException>(bool assertion, string message, Exception innerException) where TException : Exception { if (assertion) { return; } Exception exception; if (string.IsNullOrEmpty(message)) { exception = new ArgumentNullException(nameof(message)); } else { #if !NETFRAMEWORK exception = Create <TException>().Message(message).InnerException(innerException).Build(); #else exception = TypeVisit.CreateInstance <TException>(message, innerException); #endif } ExceptionHelper.PrepareForRethrow(exception); }
/// <summary> /// Create an instance of the specified type.<br /> /// 创建指定类型的实例。 /// </summary> /// <param name="type"></param> /// <param name="arguments"></param> /// <returns></returns> public static object CreateInstance(this Type type, IEnumerable <ArgumentDescriptor> arguments) { return(TypeVisit.CreateInstance(type, arguments)); }
public void DirectTypeCreateInstanceWithTwoParamsWithWrongSortTest() { var instance = TypeVisit.CreateInstance(typeof(NormalWithAttrClass), 2, "test"); instance.ShouldBeNull(); }
public void GenericTypeCreateInstanceWithTwoParamsWithWrongSortTest() { var instance = TypeVisit.CreateInstance <NormalWithAttrClass>(2, "test"); instance.ShouldBeNull(); }
/// <summary> /// Create an instance of the specified type.<br /> /// 创建指定类型的实例。 /// </summary> /// <param name="type"></param> /// <param name="args"></param> /// <returns></returns> public static object CreateInstance(this Type type, params object[] args) { return(TypeVisit.CreateInstance(type, args)); }