LazyInitializer.EnsureInitialized 方法

定義

初始化目標型別 (如果尚未初始化)。Initializes a target type if it hasn't already been initialized.

多載

EnsureInitialized<T>(T)

如果目標參考型別尚未初始化,則使用該型別的無參數建構函式來進行初始化。Initializes a target reference type with the type's parameterless constructor if it hasn't already been initialized.

EnsureInitialized<T>(T, Func<T>)

如果目標型別尚未初始化,則使用指定的函式來初始化目標的參考型別。Initializes a target reference type by using a specified function if it hasn't already been initialized.

EnsureInitialized<T>(T, Boolean, Object)

如果目標參考型別或實值型別尚未初始化,則使用其無參數的建構函式來進行初始化。Initializes a target reference or value type with its parameterless constructor if it hasn't already been initialized.

EnsureInitialized<T>(T, Object, Func<T>)

若尚未初始化,則使用指定的函式初始化目標參考型別。Initializes a target reference type with a specified function if it has not already been initialized.

EnsureInitialized<T>(T, Boolean, Object, Func<T>)

如果目標型別尚未初始化,則使用指定的函式來初始化目標的參考或實值型別。Initializes a target reference or value type by using a specified function if it hasn't already been initialized.

EnsureInitialized<T>(T)

如果目標參考型別尚未初始化,則使用該型別的無參數建構函式來進行初始化。Initializes a target reference type with the type's parameterless constructor if it hasn't already been initialized.

public:
generic <typename T>
 where T : class static T EnsureInitialized(T % target);
public static T EnsureInitialized<T> (ref T target) where T : class;
static member EnsureInitialized : 'T -> 'T (requires 'T : null)
Public Shared Function EnsureInitialized(Of T As Class) (ByRef target As T) As T

類型參數

T

要初始化之參考的型別。The type of the reference to be initialized.

參數

target
T

若尚未初始化,則為要初始化的參考。A reference to initialize if it has not already been initialized. 若為 null,其會視為未初始化; 否則,會視為已初始化。If it is null, it is considered not initialized; otherwise, it's considered initialized.

傳回

T

初始化後的物件。The initialized object.

例外狀況

缺少存取型別 T之建構函式的使用權限。Permissions to access the constructor of type T were missing.

T 沒有無參數的建構函式。Type T does not have a parameterless constructor.

備註

這個方法只能用於參考型別。This method may only be used on reference types. 若要確保實數值型別的初始化,請參閱 EnsureInitialized的其他多載。To ensure initialization of value types, see other overloads of EnsureInitialized.

多個執行緒可以同時使用這個方法來初始化 targetThis method may be used concurrently by multiple threads to initialize target.

當多個執行緒同時存取這個方法時,可能會建立多個 T 實例,但是只有一個會儲存到 target 並傳回。In the event that multiple threads access this method concurrently, multiple instances of T may be created, but only one will be stored into target and returned. 在這種情況下,這個方法將不會處置未儲存的物件。In such an occurrence, this method will not dispose of the objects that were not stored. 如果必須處置這類物件,請使用會採用 valueFactory 的多載,並在未參考相同儲存物件的情況下處置物件。If such objects must be disposed, use an overload that takes a valueFactory and dispose of the object if it does not reference the same stored object.

另請參閱

EnsureInitialized<T>(T, Func<T>)

如果目標型別尚未初始化,則使用指定的函式來初始化目標的參考型別。Initializes a target reference type by using a specified function if it hasn't already been initialized.

public:
generic <typename T>
 where T : class static T EnsureInitialized(T % target, Func<T> ^ valueFactory);
public static T EnsureInitialized<T> (ref T target, Func<T> valueFactory) where T : class;
static member EnsureInitialized : 'T * Func<'T (requires 'T : null)> -> 'T (requires 'T : null)
Public Shared Function EnsureInitialized(Of T As Class) (ByRef target As T, valueFactory As Func(Of T)) As T

類型參數

T

要初始化之參考的型別。The type of the reference to be initialized.

參數

target
T

若尚未初始化,則為要初始化的參考。The reference to initialize if it hasn't already been initialized.

valueFactory
Func<T>

呼叫來初始化參考的函式。The function that is called to initialize the reference.

傳回

T

初始化後的物件。The initialized object.

例外狀況

T 沒有無參數的建構函式。Type T does not have a parameterless constructor.

valueFactory傳回 null (在 Visual Basic 中為 Nothing)。valueFactory returned null (Nothing in Visual Basic).

備註

這個方法只能用於參考型別,而且 valueFactory 可能不會傳回 null 參考(在 Visual Basic 中為任何內容)。This method may only be used on reference types, and valueFactory may not return a null reference (Nothing in Visual Basic). 若要確保實數值型別的初始化或允許 null 參考型別,請參閱 EnsureInitialized 的其他多載。To ensure initialization of value types or to allow null reference types, see other overloads of EnsureInitialized.

多個執行緒可以同時使用這個方法來初始化 targetThis method may be used concurrently by multiple threads to initialize target.

當多個執行緒同時存取這個方法時,可能會建立多個 T 實例,但是只有一個會儲存到 targetIn the event that multiple threads access this method concurrently, multiple instances of T may be created, but only one will be stored into target. 在這種情況下,這個方法將不會處置未儲存的物件。In such an occurrence, this method will not dispose of the objects that were not stored. 如果必須處置這類物件,則由呼叫端決定是否未使用物件,並適當地處置物件。If such objects must be disposed, it is up to the caller to determine if an object was not used and to then dispose of the object appropriately.

另請參閱

EnsureInitialized<T>(T, Boolean, Object)

如果目標參考型別或實值型別尚未初始化,則使用其無參數的建構函式來進行初始化。Initializes a target reference or value type with its parameterless constructor if it hasn't already been initialized.

public:
generic <typename T>
 static T EnsureInitialized(T % target, bool % initialized, System::Object ^ % syncLock);
public static T EnsureInitialized<T> (ref T target, ref bool initialized, ref object syncLock);
static member EnsureInitialized : 'T * bool * obj -> 'T
Public Shared Function EnsureInitialized(Of T) (ByRef target As T, ByRef initialized As Boolean, ByRef syncLock As Object) As T

類型參數

T

要初始化之參考的型別。The type of the reference to be initialized.

參數

target
T

要初始化 (如果尚未初始化) 的 T 類型參考或值。A reference or value of type T to initialize if it hasn't already been initialized.

initialized
Boolean

布林值的參考,這個值可判斷目標是否已初始化。A reference to a Boolean value that determines whether the target has already been initialized.

syncLock
Object

物件的參考,這個物件用來當做初始化 target 時的互斥鎖定。A reference to an object used as the mutually exclusive lock for initializing target. 如果 syncLocknull,則具現化新的物件。If syncLock is null, a new object will be instantiated.

傳回

T

初始化後的物件。The initialized object.

例外狀況

缺少存取型別 T之建構函式的使用權限。Permissions to access the constructor of type T were missing.

T 沒有無參數的建構函式。Type T does not have a parameterless constructor.

備註

如果 initialized 指定為 true,則不會進行進一步的初始化。If initialized is specified as true, then no further initialization occurs.

另請參閱

EnsureInitialized<T>(T, Object, Func<T>)

若尚未初始化,則使用指定的函式初始化目標參考型別。Initializes a target reference type with a specified function if it has not already been initialized.

public:
generic <typename T>
 where T : class static T EnsureInitialized(T % target, System::Object ^ % syncLock, Func<T> ^ valueFactory);
public static T EnsureInitialized<T> (ref T target, ref object syncLock, Func<T> valueFactory) where T : class;
static member EnsureInitialized : 'T * obj * Func<'T (requires 'T : null)> -> 'T (requires 'T : null)
Public Shared Function EnsureInitialized(Of T As Class) (ByRef target As T, ByRef syncLock As Object, valueFactory As Func(Of T)) As T

類型參數

T

要初始化之參考的型別。The type of the reference to be initialized.

參數

target
T

若尚未初始化,則為要初始化的參考。A reference to initialize if it has not already been initialized. 若為 null,其會視為未初始化; 否則,會視為已初始化。If it is null, it is considered not initialized; otherwise, it's considered initialized.

syncLock
Object

物件的參考,這個物件用來當做初始化 target 時的互斥鎖定。A reference to an object used as the mutually exclusive lock for initializing target. 如果 syncLocknull,則具現化新的物件。If syncLock is null, a new object will be instantiated.

valueFactory
Func<T>

初始化 target 時要叫用的方法。The method to invoke to initialize target.

傳回

T

初始化後的物件。The initialized object.

EnsureInitialized<T>(T, Boolean, Object, Func<T>)

如果目標型別尚未初始化,則使用指定的函式來初始化目標的參考或實值型別。Initializes a target reference or value type by using a specified function if it hasn't already been initialized.

public:
generic <typename T>
 static T EnsureInitialized(T % target, bool % initialized, System::Object ^ % syncLock, Func<T> ^ valueFactory);
public static T EnsureInitialized<T> (ref T target, ref bool initialized, ref object syncLock, Func<T> valueFactory);
static member EnsureInitialized : 'T * bool * obj * Func<'T> -> 'T
Public Shared Function EnsureInitialized(Of T) (ByRef target As T, ByRef initialized As Boolean, ByRef syncLock As Object, valueFactory As Func(Of T)) As T

類型參數

T

要初始化之參考的型別。The type of the reference to be initialized.

參數

target
T

要初始化 (如果尚未初始化) 的 T 類型參考或值。A reference or value of type T to initialize if it hasn't already been initialized.

initialized
Boolean

布林值的參考,這個值可判斷目標是否已初始化。A reference to a Boolean value that determines whether the target has already been initialized.

syncLock
Object

物件的參考,這個物件用來當做初始化 target 時的互斥鎖定。A reference to an object used as the mutually exclusive lock for initializing target. 如果 syncLocknull,則具現化新的物件。If syncLock is null, a new object will be instantiated.

valueFactory
Func<T>

呼叫來初始化參考或值的函式。The function that is called to initialize the reference or value.

傳回

T

初始化後的物件。The initialized object.

例外狀況

缺少存取型別 T之建構函式的使用權限。Permissions to access the constructor of type T were missing.

T 沒有無參數的建構函式。Type T does not have a parameterless constructor.

備註

如果 initialized 指定為 true,則不會進行進一步的初始化。If initialized is specified as true, then no further initialization occurs.

另請參閱

適用於