RandomExtensionsNextObjectT(Random, GenerateObjectSettings) Method
Returns a random object of type T or the default value of T
if T cannot be instantiated with the provided settings.
Namespace: KGySoft.CoreLibrariesAssembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 8.1.0
public static T NextObject<T>(
this Random random,
GenerateObjectSettings settings = null
)
<ExtensionAttribute>
Public Shared Function NextObject(Of T) (
random As Random,
Optional settings As GenerateObjectSettings = Nothing
) As T
public:
[ExtensionAttribute]
generic<typename T>
static T NextObject(
Random^ random,
GenerateObjectSettings^ settings = nullptr
)
[<ExtensionAttribute>]
static member NextObject :
random : Random *
?settings : GenerateObjectSettings
(* Defaults:
let _settings = defaultArg settings null
*)
-> 'T
- random Random
- The Random instance to use.
- settings GenerateObjectSettings (Optional)
- The settings to use or to use the default settings.
Default value: .
- T
- The type of the object to be created. If TryResolveInterfacesAndAbstractTypes property
in settings is , then it can be also an interface or abstract type;
however, if no implementation or usable constructor found, then a value will be returned.
TAn instance of
T or
if the type cannot be
instantiated with the provided
settings See the
Remarks section for details.In Visual Basic and C#, you can call this method as an instance method on any object of type
Random. When you use instance method syntax to call this method, omit the first parameter. For more information, see
Extension Methods (Visual Basic) or
Extension Methods (C# Programming Guide).
The generated object is not guaranteed to be in a consistent format, especially if
AllowCreateObjectWithoutConstructor
property is
or
ObjectInitialization property is
Fields in
settings.
T can be basically any type as long as it has a default constructor or (in case of collections) a constructor with a parameter that can accept a collection.
If T is an interface or an abstract class you can set the TryResolveInterfacesAndAbstractTypes property to
use a random implementation of T. If no implementation is found among the loaded assemblies with a proper constructor, then the result will be .
If T is a non-sealed class you can set the AllowDerivedTypesForNonSealedClasses property to
allow to use a random derived class of T.
All types, which can be generated by the Next... methods of the RandomExtensions class, are supported.
Some other types have some special handling for better support:
- StringBuilder – The same behavior as for strings.
- Uri – The result will match the following pattern: http://<lowercase word-like string of length between 4 and 10>.<3 lower case letters>
- IntPtr – The same behavior as for 32 or 64 bit signed integers, based on the used platform.
- UIntPtr – The same behavior as for 32 or 64 bit unsigned integers, based on the used platform.
- KeyValuePairTKey, TValue – Using its parameterized constructor to create an instance.
- Assembly – A random loaded assembly will be picked.
- Type – A random type will be picked from one of the loaded assemblies.
- MemberInfo types – A random member will be picked from one of the types of the loaded assemblies.
- Delegate types – A dynamic method will be created for the specified delegate, which returns random objects both by return value and by the possible out parameters.
The generated delegates do not use the specified
random instance because in that case the
random instance could
never be reclaimed by the garbage collector. To avoid leaking memory generated delegates use an internal static
Random instance.
See the
Examples section of the
RandomExtensions class for some examples.