/// <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 AllGetSetPropertyTest() { var type = typeof(NormalPropertyClass); var members = type.GetMembers(); var property1 = members.Single(m => m.Name == nameof(NormalPropertyClass.PublicGetSet)); var property2 = members.Single(m => m.Name == nameof(NormalPropertyClass.PublicGet)); var property3 = members.Single(m => m.Name == nameof(NormalPropertyClass.PublicSet)); var x1 = TypeVisit.GetProperties(type, PropertyAccessOptions.Both).ToList(); var x2 = TypeVisit.GetProperties(type, PropertyAccessOptions.Getters).ToList(); var x3 = TypeVisit.GetProperties(type, PropertyAccessOptions.Setters).ToList(); x1.ShouldNotBeEmpty(); x2.ShouldNotBeEmpty(); x3.ShouldNotBeEmpty(); x1.Count().ShouldBe(1); x2.Count().ShouldBe(2); x3.Count().ShouldBe(2); x1[0].ShouldBe(property1); x2[0].ShouldBe(property1); x2[1].ShouldBe(property2); x3[0].ShouldBe(property1); x3[1].ShouldBe(property3); }
public void GenericTypeCreateInstanceWithoutParamsTest() { var instance = TypeVisit.CreateInstance <NormalWithAttrClass>(); instance.ShouldNotBeNull(); instance.GetType().ShouldBe(typeof(NormalWithAttrClass)); }
/// <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 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 }
public void DirectTypeCreateInstanceWithOneParamsTest() { var instance = TypeVisit.CreateInstance(typeof(NormalWithAttrClass), "test"); instance.ShouldNotBeNull(); instance.GetType().ShouldBe(typeof(NormalWithAttrClass)); ((NormalWithAttrClass)instance).Nice.ShouldBe("test"); }
public static IEnumerable <PropertyInfo> GetProperties <T>(this T x, PropertyAccessOptions accessOptions, params Expression <Func <T, object> >[] propertySelectors) { if (x is null) { throw new ArgumentNullException(nameof(x)); } return(TypeVisit.GetProperties(accessOptions, propertySelectors)); }
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); }
public void ExpressionCopierTest() { var original = new NormalWithAttrClass { Good = "Nice", Index = 99, Nice = "Good" }; var target = TypeVisit.DeepCopy(original, DeepCopyOptions.ExpressionCopier); Assert.Same(original, original); Assert.Same(target, target); Assert.NotSame(original, target); }
/// <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 static PropertyInfo GetProperty <T, TProperty>(this T x, Expression <Func <T, TProperty> > propertySelector, PropertyAccessOptions accessOptions = PropertyAccessOptions.Both) { if (x is null) { throw new ArgumentNullException(nameof(x)); } if (propertySelector is null) { throw new ArgumentNullException(nameof(propertySelector)); } return(TypeVisit.GetProperty(propertySelector, accessOptions)); }
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 }
public void ExtensionsSinglePropertyTest() { var type = typeof(NormalPropertyClass); var members = type.GetMembers(); var property1 = members.Single(m => m.Name == nameof(NormalPropertyClass.PublicGetSet)); var property2 = members.Single(m => m.Name == nameof(NormalPropertyClass.PublicGet)); var property3 = members.Single(m => m.Name == nameof(NormalPropertyClass.PublicSet)); var x4 = TypeVisit.GetProperty <NormalPropertyClass, string>(t => t.PublicGetSet); var x5 = TypeVisit.GetProperty <NormalPropertyClass, string>(t => t.PublicGetSet, PropertyAccessOptions.Getters); var x6 = TypeVisit.GetProperty <NormalPropertyClass, string>(t => t.PublicGetSet, PropertyAccessOptions.Setters); x4.ShouldNotBeNull(); x5.ShouldNotBeNull(); x6.ShouldNotBeNull(); x4.ShouldBe(property1); x5.ShouldBe(property1); x6.ShouldBe(property1); var x7 = TypeVisit.GetProperty <NormalPropertyClass, string>(t => t.PublicGet); var x8 = TypeVisit.GetProperty <NormalPropertyClass, string>(t => t.PublicGet, PropertyAccessOptions.Getters); Assert.Throws <ArgumentException>(() => TypeVisit.GetProperty <NormalPropertyClass, string>(t => t.PublicGet, PropertyAccessOptions.Setters)); x7.ShouldNotBeNull(); x8.ShouldNotBeNull(); x7.ShouldBe(property2); x8.ShouldBe(property2); var x9 = TypeVisit.GetProperty <NormalPropertyClass, string>(t => t.PublicSet); var x10 = TypeVisit.GetProperty <NormalPropertyClass, string>(t => t.PublicSet, PropertyAccessOptions.Setters); Assert.Throws <ArgumentException>(() => TypeVisit.GetProperty <NormalPropertyClass, string>(t => t.PublicSet, PropertyAccessOptions.Getters)); x9.ShouldNotBeNull(); x10.ShouldNotBeNull(); x9.ShouldBe(property3); x10.ShouldBe(property3); }
/// <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 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); }
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)); }
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); }
/// <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); }
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> /// Exclude all PropertyInfos that meet the given conditions from the PropertyInfo list, /// and return the remaining PropertyInfo.<br /> /// 从 PropertyInfo 列表中排除所有满足给定条件的 PropertyInfo,并返回其余 PropertyInfo。 /// </summary> /// <param name="properties"></param> /// <param name="expressions"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> /// <exception cref="ArgumentNullException"></exception> public static IEnumerable <PropertyInfo> Exclude <T>(this IEnumerable <PropertyInfo> properties, IEnumerable <Expression <Func <T, object> > > expressions) { return(TypeVisit.Exclude(properties, expressions)); }
/// <summary> /// Determine whether PropertyInfo is Visible and Virtual. /// </summary> /// <param name="property"></param> /// <returns></returns> /// <exception cref="ArgumentNullException"></exception> public static bool IsVisibleAndVirtual(this PropertyInfo property) { return(TypeVisit.IsVisibleAndVirtual(property)); }
/// <summary> /// Exclude all PropertyInfos that meet the given conditions from the PropertyInfo list, /// and return the remaining PropertyInfo.<br /> /// 从 PropertyInfo 列表中排除所有满足给定条件的 PropertyInfo,并返回其余 PropertyInfo。 /// </summary> /// <param name="properties"></param> /// <param name="shape"></param> /// <param name="expressions"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> public static IEnumerable <PropertyInfo> Exclude <T>(this IEnumerable <PropertyInfo> properties, T shape, params Expression <Func <T, object> >[] expressions) { return(TypeVisit.Exclude(properties, shape, expressions)); }
public static IEnumerable <PropertyInfo> GetProperties <T>(this T x, PropertyAccessOptions accessOptions = PropertyAccessOptions.Both) { return(TypeVisit.GetProperties(typeof(T), accessOptions)); }
/// <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 static T DeepCopy <T>(this T x, DeepCopyOptions options = DeepCopyOptions.DeepCopier) { return(TypeVisit.DeepCopy(x, options)); }
public static T DeepCopy <T>(this T x, DeepCopyContext context) { return(TypeVisit.DeepCopy(x, context)); }